I asked a question last week about Gui and Timers… well after lots of research i found nothing that would work with my code… If anyone could take my code and somehow manipulate it to work that would be amazing… I want my code to display a box and then making it overlap with a box with the same color as the back ground and then a new box being displayed right beside it with a delay
first code:
import java.awt.Graphics;
import javax.swing.JPanel;
import java.awt.Color;
public class MovingSomething extends JPanel
{
public void paintComponent (final Graphics g)
{
int i = 20;
int cons = 50;
int red = 40;
int green = 50;
g.setColor(Color.GREEN);
g.fillRect(50, 50, 100, 100);
while (i >= 0)
{
i -= 1;
g.setColor(Color.RED);
g.fillRect(red, cons, 100, 100);
red += 10;
g.setColor(Color.GREEN);
g.fillRect(green, cons, 100, 100);
green += 10;
}
}
}
Just for your little help to start with, I am posting this code. Though Please do try to learn from it and ask valid questions, that might will arise, to get a taste of the whole thingy. If you had searched it a little bit harder, no doubt, you could have reached this wonderful Doc, that explains exactly your needs in this example.
Code to programatically repaint the component whenever the user clicks or drags the mouse
LATEST EDIT :
Please try this modified code, the CustomPanel Class is same as before :