|
im trying to design an application that displays an animation of a horizontal line segment moving across the screen from left to right, eventually passing across a vertical line.
im a little confused on which variables i should set up. ..right now my variables are..
private Timer timer;
private final int DELAY=20;
private int x,y,moveX,moveY;
//and this is where im confused....do i need variables for the horizontal and vertical lines? and do i need variables x1,y1,x2,y2 to draw my these line segments?
|
|
|
It depands on how you want to do this. If the vertical line is fixed then you can leave it inside the paintComponent method.
protected void paintComponent(Graphics g)
...
g2.draw(new Line2D.Double(w/2, 0, w/2, h));
|
|
If you want to make it an instance variable you could.
Line2D vertLine;
...
public Constructor()
vertLine = new Line2D.Double(x1, y1, x2, y2);
...
protected void paintComponent(Graphics g)
...
g2.draw(vertLine);
|
|
For the horizontal line: if you use a Line2D then it will keep track of its endpoints (P1 and P2 and/or x1, x2, y1, y2, see api). You can access these, add the increment and use in the Line2D "setLine" method. If you would rather use the Graphics method drawLine then you will need to declare the x1, y1, x2, y2 variables as instance variables (class scope). And the amount of horizontal travel, say "xInc" is required. You could make this an instance variable of the JPanel class that draws the animation or, if you use an inner/nested or outer class for the timers ActionListener you could declare the increment as an instance variable within it.
|
|
|
|
|
|
|
|