# Need help... how to get the line moving?

• October 6th, 2010, 12:13 PM
thaiman
Need help... how to get the line moving?
Hi, I've done a program with 3 shapes: a circle, an rectangle and a line. These objects are supposed to move around in the box, but only the circle and the rectangle does. The line is stuck in one end. How can i get the line to move around in the box?

I think i need to do something with the x2 and y2 points in the Line-class but what?

Code :

```import java.awt.*;     public class Line extends Shape {   private double x2; private double y2;   public Line (double x, double y, Color color, double x2, double y2){ super(x, y, color); this.x2 = x2; this.y2 = y2; }       public double getX2(){ return x2; }       public double getY2(){ return y2; }     public void paint (Graphics g){ g.setColor(Color.red); g.drawLine((int) getX(), (int) getY(), (int) x2, (int) y2); }     }```

Code :

```import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle;   abstract public class Shape {   private double x, y; private double dx, dy; private Color color; private Rectangle box;   public Shape(double x, double y, Color color) { this.x = x; this.y = y; this.color = color; }   public double getX() { return x; }   public double getY() { return y; }   public double getDX() { return dx; }   public double getDY() { return dy; }   public Color getColor() { return color; }   public Rectangle getBoundingBox() { return box; }     public void setBoundingBox(Rectangle box) { this.box = box; }     public void setVelocity(double dx, double dy) { this.dx = dx; this.dy = dy; }   /** Move this shape (dx, dy) */ public void move() { x += dx; y += dy; constrain(); }   /** Keep this shape inside the bounding box. * Override this method i sub classes. */ protected void constrain() {   double x0 = box.x, y0 = box.y; double x1 = x0 + box.width; double y1 = y0 + box.height;   // If outside box, change direction if(x < x0) dx = Math.abs(dx); if(x > x1) dx = -Math.abs(dx); if(y < y0) dy = Math.abs(dy); if(y > y1) dy = -Math.abs(dy); }   /** Paint this shape (abstract method) */ abstract public void paint(Graphics g); }```

Code :

```import java.awt.*; import javax.swing.*;   /** * BouncePanel represents a canvas (drawing area) for objects of * type Ball. * A thread-object periodically updates the canvas (in the run method), * by moving and repainting the objects (the animation). */   public class BouncePanel extends JPanel implements Runnable {   private int width = 400, height = 300; private Thread thread; private Line line; private Circle circle; private Rect rect;   public BouncePanel() {   Rectangle box = new Rectangle(width, height);   Color color = new Color(0); // Create and initialize objects of (sub types of) Shape // . . . // . . . FillableShape.setFilled(true);   line = new Line(40.0, 30.0, color, 50.0, 50.0); circle = new Circle(0.0, 0.0, color, FillableShape.filled, 10.0); rect = new Rect(50.0, 80.0, color, FillableShape.filled, 15.0, 25.0);   line.setBoundingBox(box); circle.setBoundingBox(box); rect.setBoundingBox(box);   line.setVelocity(2.7, 3.5); circle.setVelocity(1.7, 2.8); rect.setVelocity(4.5, 2.5);   // Set the the dimensions of the drawing area setPreferredSize(new Dimension(width, height));     // Create the thread-object, responsible for the animation thread = new Thread(this); thread.start(); }     /** Define what to draw. Called by repaint(). */ public void paintComponent(Graphics g) { super.paintComponent(g);   line.paint(g); circle.paint(g); rect.paint(g);   }   /** Update the position of the ball and repaint. * This method is executed by the thread-object. */ public void run() { while(true) {     if (width != getWidth() || height != getHeight()){ width = getWidth(); height = getHeight();}   Rectangle box = new Rectangle(width, height);     line.setBoundingBox(box); circle.setBoundingBox(box); rect.setBoundingBox(box);   line.move(); circle.move(); rect.move();   repaint(); // Calls paintComponent(g)   // Sleep for 20 ms before next update try { Thread.sleep(20); } catch(InterruptedException e) {}   } }   private static final long serialVersionUID = 1L; }```

Code :

```import javax.swing.*;   public class Bounce { public static void main(String[] args) {   // Create a window (frame) JFrame frame = new JFrame("Bounce with Shapes");   // Create a canvas (BouncePanel) and add it to the window BouncePanel panel = new BouncePanel(); frame.getContentPane().add(panel);   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } }```

Code :

```import java.awt.Color; import java.awt.Graphics;   public class Circle extends FillableShape {   private double diameter;   public Circle (double x, double y, Color color, boolean filled, double diameter){ super(x, y, color, filled); this.diameter = diameter; }   public double getDiameter(){ return diameter; }   public void paint(Graphics g){ g.setColor(Color.orange); g.drawOval((int)getX(), (int)getY(), (int)diameter, (int)diameter); if(filled == true){ g.fillOval((int)getX(), (int)getY(), (int)diameter, (int)diameter); } }       //public void setFilled(Graphics g) { //if(filled==true) g.fillOval((int) x, (int) y, (int)diameter, (int)diameter); //}     }```

Code :

```import java.awt.Color; import java.awt.Graphics;   public class Rect extends FillableShape {   private double width, height;   public Rect (double x, double y, Color color, boolean filled, double width, double height){   super(x, y, color, filled); this.width=width; this.height=height; }   public double getWidth(){ return width; }   public double getHeight(){ return height; }   public void paint(Graphics g){ g.setColor(Color.green); g.drawRect((int) getX(), (int) getY(), (int) width, (int) height);   if(filled == true){ g.fillRect((int) getX(), (int) getY(), (int) width, (int) height); } } }```
• October 6th, 2010, 04:38 PM
Norm
Re: Need help... how to get the line moving?
Quote:

How can i get the line to move around
Look at the code that draws the line and change its x,y locations to where you want the line to be drawn.

Try debugging the program by adding print outs of all the variables used to control the line's position so you can see where they are being set and where you need to change the code to have correct values for the line to move.
• October 7th, 2010, 09:48 AM
thaiman
Re: Need help... how to get the line moving?
Quote:

Originally Posted by Norm
Look at the code that draws the line and change its x,y locations to where you want the line to be drawn.

Try debugging the program by adding print outs of all the variables used to control the line's position so you can see where they are being set and where you need to change the code to have correct values for the line to move.

Code :

```import java.awt.*;     public class Line extends Shape {   private double x2=0; private double y2=0, dx2, dy2;   public Line (double x, double y, Color color, double x2, double y2){ super(x, y, color); this.x2 = x2; this.y2 = y2; }   public double getX2(){ x2=super.getX()+30; return x2; }   public double getY2(){ y2=super.getY()+30; return y2; }   public void paint (Graphics g){ g.setColor(Color.red); g.drawLine((int) getX(), (int) getY(), (int) getX2(), (int) getY2()); }     }```

I have done like this and this makes the line move, but now im not using the constructor-value. When i try with x2=super.getX()+x2 and y2=super.getY()+y2 instead, the whole line goes bananas. This is the only solution in my noob-mind.
• October 7th, 2010, 10:04 AM
Norm
Re: Need help... how to get the line moving?
Try debugging the program by adding print outs of all the variables used to control the line's position so you can see where they are being set and where you need to change the code to have correct values for the line to move.