Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Need help... how to get the line moving?

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?


    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);
    	}
     
     
    }



    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);
    }


    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;
    }


    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);	
    	}
    }


    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);
    	//}
     
     
    }



    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);
    		}
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,973
    Thanks
    51
    Thanked 2,269 Times in 2,241 Posts

    Default Re: Need help... how to get the line moving?

    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.

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help... how to get the line moving?

    Quote Originally Posted by Norm View Post
    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.

    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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,973
    Thanks
    51
    Thanked 2,269 Times in 2,241 Posts

    Default 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.

Similar Threads

  1. Moving average
    By bondage in forum Loops & Control Statements
    Replies: 0
    Last Post: May 7th, 2010, 04:20 AM
  2. Problem in navigation in a txt file with specific direction
    By wendybird79 in forum Collections and Generics
    Replies: 1
    Last Post: May 10th, 2009, 07:54 AM
  3. Reading a file line by line using the Scanner class
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM
  4. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM