
This image changes every X milliseconds. A timer is redrawing all of the squares.
This is the code I have so far:
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;
import java.util.Random;
import javax.swing.Timer;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Canvas;
import javax.swing.JPanel;
public class RandomColoredBoxes extends JPanel
{
private Timer timer;
private final static int SLEEP = 110;
public RandomColoredBoxes()
{
setBackground(Color.BLACK);
setVisible(true);
ActionListener paintCaller = new ActionListener(){
public void actionPerformed(ActionEvent event)
{
repaint();
}
};
timer = new Timer(SLEEP, paintCaller);
timer.start();
}
public void paintComponent( Graphics window )
{
super.paintComponent(window);
window.setColor(Color.RED);
window.setFont(new Font("TAHOMA",Font.BOLD,12));
window.drawString("Graphics Lab Lab11k ", 20, 40);
window.drawString("Drawing boxes with nested loops ", 20, 80);
drawBoxes(window);
}
public void drawBoxes(Graphics window)
{
//for loop to to across the x - getWidth() might be useful
//for loop to go down the y - getHeight() might be useful
//draw random colored boxes
}
}
This is the code for the Graphics Runner:
import javax.swing.JFrame;
public class GraphicsRunner extends JFrame
{
private static final int WIDTH = 800;
private static final int HEIGHT = 600;
public GraphicsRunner()
{
super("Graphics Runner");
setSize(WIDTH,HEIGHT);
//getContentPane().add(new ColoredBoxes());
getContentPane().add(new RandomColoredBoxes());
setVisible(true);
}
public static void main( String args[] )
{
GraphicsRunner run = new GraphicsRunner();
}
}
Please help! I have no idea how to proceed.

New Topic/Question
Reply




MultiQuote





|