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