Re: create moving points with respect to time
What do you mean by "one by one"?
Is it this:
draw shape 1
wait some
draw shape 2
wait some
etc
Your code does NOT follow the design I recommended earlier:
1) Do the drawing in the paintComponent method using the Graphics object passed to the method
2) compute the positions in the Time listener method and call repaint()
Re: create moving points with respect to time
Thanks for ur reply. Yes I want to draw a shape, wait, draw another and wait and so on.
Actually I got different kind of output from my experiment today, where I don't have to calculate the locations. I just have to enter my result in x and y positions. So, I didnot write any method to calculate locations.
Now everything worked. but the only thing is to draw shape, wait, draw next shape, wait and so on... and after drawing the last shape, I want to erase all the shapes drawn before.
Please help. Thanks.
Re: create moving points with respect to time
You need to post your code and explain what the problem is with it.
The last code you posted was wrong. It did not draw in the paintComponent method.
Re: create moving points with respect to time
Hi..
I have created instance of a class LadderContruction in my code. That class has the paintcomponent method. That paint component method draws a ladder in my JFrame. that is fine.
The code I post here draws just the shapes on this ladder that I created in previous paint component method. Everything is working fine.
Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
public class TimerRun extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = -6910867316879215818L;
private JPanel container;
LadderConstruction lc= new LadderConstruction();
RungCoordinates rc = new RungCoordinates();
private Timer timer1 = new Timer(2000, this);
//private Timer timer2 = new Timer(1000, this);
int x = rc.r5x;
int y= rc.rY;
int yl = rc.lY;
int x1 = rc.l7x;
int x2 = rc.l8x;
int x3 = rc.r10x;
int x4 = rc.l13x;
int x5 = rc.r15x;
int x6 = rc.l18x;
int x7 = rc.r21x;
public TimerRun() {
//set initial delay to 1000 milliseconds
timer1.setInitialDelay(1000);
timer1.getDelay();
//timer2.setInitialDelay(2500);
//initialize window
container = new JPanel();
this.add(container);
this.add(lc);
//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1200,1200);
this.setVisible(true);
//start timer
timer1.start();
//timer2.start();
}
/**
* when timer begins this method draws rectangles from left to right
*/
public void actionPerformed(ActionEvent e) {
Graphics g = lc.getGraphics();
g.setColor(Color.CYAN);
g.drawRect(x, y, 10, 5);
g.fillRect(x, y, 10, 5); // R5 rung
g.drawRect(x1, yl, 10, 5);
g.fillRect(x1, yl, 10, 5); // L7 rung
g.setColor(Color.MAGENTA);
g.drawRect(x, y, 10, 5);
g.fillRect(x, y, 10, 5); // R5 2nd time
g.drawRect(x2, yl, 10, 5);
g.fillRect(x2, yl, 10, 5); // L8
g.drawRect(x3, y, 10, 5);
g.fillRect(x3, y, 10, 5); // R10
g.setColor(Color.RED);
g.drawRect(x, y, 10, 5);
g.fillRect(x, y, 10, 5); // R5 3rd time
g.setColor(Color.MAGENTA);
g.drawRect(x3, y, 10, 5);
g.fillRect(x3, y, 10, 5); // R10 2nd time
g.setColor(Color.MAGENTA);
g.drawRect(x2, yl, 10, 5);
g.fillRect(x2, yl, 10, 5); // L8 2nd time
g.setColor(Color.CYAN);
g.drawRect(x4, yl, 10, 5);
g.fillRect(x4, yl, 10, 5); // L13
g.setColor(Color.RED);
g.drawRect(x3, y, 10, 5);
g.fillRect(x3, y, 10, 5); // R10 3nd time
g.setColor(Color.MAGENTA);
g.drawRect(x4, yl, 10, 5);
g.fillRect(x4, yl, 10, 5); // L13 2nd time
g.setColor(Color.RED);
g.drawRect(x4, yl, 10, 5);
g.fillRect(x4, yl, 10, 5); // L13 3rd time
g.setColor(Color.CYAN);
g.drawRect(x5, y, 10, 5);
g.fillRect(x5, y, 10, 5); // R15 rung
g.setColor(Color.YELLOW);
g.drawRect(x4, yl, 10, 5);
g.fillRect(x4, yl, 10, 5); // L13 4th time
g.setColor(Color.MAGENTA);
g.drawRect(x5, y, 10, 5);
g.fillRect(x5, y, 10, 5); // R15 2nd time
g.setColor(Color.CYAN);
g.drawRect(x6, yl, 10, 5);
g.fillRect(x6, yl, 10, 5); // L18 rung
g.setColor(Color.RED);
g.drawRect(x5, y, 10, 5);
g.fillRect(x5, y, 10, 5); // R15 3rd time
g.setColor(Color.MAGENTA);
g.drawRect(x6, yl, 10, 5);
g.fillRect(x6, yl, 10, 5); // L18 2nd time
g.setColor(Color.CYAN);
g.drawRect(x7, y, 10, 5);
g.fillRect(x7, y, 10, 5); // R21 rung
timer1.stop();
}
public static void main(String[] args){
TimerRun display = new TimerRun();
}
}
and I added thread.sleep(1000)
after drawing each shape. So they are drawn one by one. But after finishing the drawing, I want to disappear all the shapes. For this, I need some syntax related help. Thank you.
Re: create moving points with respect to time
Let me repeat this, you seem to have missed it all the other times I said it:
You should do all the drawing in the paintComponent method.