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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MovingSquare
{
private int x = 0;
private int y = 100;
private final int WIDTH = 100;
private CustomPanel canvas;
private Timer drawingTimer;
private ActionListener timerAction =
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent ae)
{
if ((x + WIDTH == 500))
{
x = 0;
canvas.setValues(x, y, Color.BLUE);
}
else
{
x += WIDTH;
canvas.setValues(x, y, Color.BLUE);
}
}
};
private void displayGUI()
{
JFrame frame = new JFrame("Moving Sqaure");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
canvas = new CustomPanel();
frame.setContentPane(canvas);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
drawingTimer = new Timer(1000, timerAction);
drawingTimer.start();
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new MovingSquare().displayGUI();
}
});
}
}
class CustomPanel extends JPanel
{
private final int WIDTH = 500;
private final int HEIGHT = 500;
private final int WSQUARE = 100;
private final int HSQUARE = 50;
private int x = 0;
private int y = 100;
private Color cSquare = Color.BLUE;
/*
* This is where we updating the state
* of different variables needed, and
* thus calling repaint.
*/
public void setValues(int x, int y, Color color)
{
cSquare = color;
repaint(this.x, this.y, WSQUARE, HSQUARE);
this.x = x;
this.y = y;
repaint(x, y, WSQUARE, HSQUARE);
}
/*
* Always make this one customary
* habbit, to override this method
* when you extending a JComponent.
*/
@Override
public Dimension getPreferredSize()
{
return (new Dimension(WIDTH, HEIGHT));
}
/*
* This is where the actual Painting
* Portion of the whole thingy will
* reside. Better is, not to put any
* calculations in this part, just
* update the state at some another
* location and convey it to repaint
* as needed.
*/
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(cSquare);
g.fillRect(x, y, WSQUARE, HSQUARE);
}
}
LATEST EDIT :
Please try this modified code, the CustomPanel Class is same as before :
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class MovingSquare
{
private int x = 0;
private int y = 100;
private final int WIDTH = 100;
private final int HEIGHT = 100;
private Random random;
private CustomPanel canvas;
private Timer drawingTimer;
private ActionListener timerAction =
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent ae)
{
/*if ((x + WIDTH > 500) && (y + HEIGHT > 500))
{
x = random.nextInt(500 - WIDTH);
canvas.setValues(x, y, Color.BLUE);
}
else
{
x += WIDTH;
canvas.setValues(x, y, Color.BLUE);
}*/
x = random.nextInt(500 - WIDTH);
y = random.nextInt(500 - HEIGHT);
canvas.setValues(x, y, new Color(
random.nextFloat(), random.nextFloat()
, random.nextFloat(), random.nextFloat()));
}
};
public MovingSquare()
{
random = new Random();
}
private void displayGUI()
{
JFrame frame = new JFrame("Moving Sqaure");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
canvas = new CustomPanel();
frame.setContentPane(canvas);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
drawingTimer = new Timer(1000, timerAction);
drawingTimer.start();
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new MovingSquare().displayGUI();
}
});
}
}