|
Re: Help with Java Applet Needed
|
Posted: May 26, 2004 4:28 PM
|
|
Here is your working code. Your code had minor problems. In fact, you have only painting problem.
1. From paintComponent() method of main applet you were calling update() of canvas. You don't need to do that. Canvas would have repainted itself. 2. You don't need to pass graphics object which is for main applet to Canvas in it its update() method call as described in STEP 1.
3. By correcting mistakes in STEP 1 and 2 , I was able to run your applet. It works fine.
4. Why do you mix swing and awt components? I mean, why did you use canvas at all. You can use JPanel instead. Mixing swing and awt components has paiting problems sometimes.
5. In the following code, I have replaced the Canvas by JPanel. The following code is yours with the folloing changes a) See the paintComponent() method of main applet b) Canvas replaced by JPanel c) Commented update() method of old Canvas
package test;
// Philip Foulkes - 603F2534 - May 2004
// A simple Applet of a variation of a Langton's Ant
// If line 85 (setLayout (new BorderLayout()) is taken out the code works fine but the presentation is not so great.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class LangtonsAntPanel extends JPanel
{
private static final Color GRAY = Color.GRAY;
private static final Color WHITE = Color.WHITE;
private static final Color RED = Color.RED;
private static final int NORTH = 0;
private static final int EAST = 1;
private static final int SOUTH = 2;
private static final int WEST = 3;
private int height = 20; //no of blocks high
private int width = 20; //no of blocks wide
private static final int BLOCKSIZE = 20; //the size of each block
private AntCanvas antCanvas;
private Color[][] grid; //store the color of each square
private Ant ant; //represent the ant
private Timer timer; //timer
private int DELAY = 200; //timer delay
//buttons
private JPanel buttonPanel;
private JButton nextButton, runButton, stopButton, resetButton;
//lables and textfields
private JPanel inputPanel;
private JLabel gridSizeLabel, redSquaresLabel, whiteSquaresLabel;
private JTextField gridSizeTextField, redSquaresTextField, whiteSquaresTextField;
public LangtonsAntPanel ()
{
//*****************************************************
//buttonPanel
buttonPanel = new JPanel ();
buttonPanel.setLayout(new GridLayout(1,4));
nextButton = new JButton ("Next");
nextButton.addActionListener (new NextButtonListener());
buttonPanel.add (nextButton);
runButton = new JButton ("Run");
runButton.addActionListener (new RunButtonListener());
buttonPanel.add (runButton);
stopButton = new JButton ("Stop");
stopButton.addActionListener (new StopButtonListener());
buttonPanel.add (stopButton);
resetButton = new JButton ("Reset");
resetButton.addActionListener (new ResetButtonListener());
buttonPanel.add (resetButton);
//*****************************************************
//*****************************************************
//inputPanel
inputPanel = new JPanel (new GridLayout (2, 3));
gridSizeLabel = new JLabel ("Grid Size");
redSquaresLabel = new JLabel ("No. of Red Squares");
whiteSquaresLabel = new JLabel ("No. of White Squares");
gridSizeTextField = new JTextField (new Integer(height).toString());
redSquaresTextField = new JTextField ("0");
whiteSquaresTextField = new JTextField ("0");
inputPanel.add (gridSizeLabel);
inputPanel.add (redSquaresLabel);
inputPanel.add (whiteSquaresLabel);
inputPanel.add (gridSizeTextField);
inputPanel.add (redSquaresTextField);
inputPanel.add (whiteSquaresTextField);
//*****************************************************
antCanvas = new AntCanvas (width, height);
setLayout (new BorderLayout());
add (buttonPanel, BorderLayout.NORTH);
add (antCanvas, BorderLayout.CENTER);
add (inputPanel, BorderLayout.SOUTH);
timer = new Timer (DELAY, new GoAntListener());
}
public void paintComponent (Graphics g)
{
super.paintComponent(g);
//antCanvas.update(g);
//antCanvas.repaint();
}
//changes the size of the canvas
//the size is obtained from the JTextField
private void changeCanvasSize ()
{
int dim = new Integer(gridSizeTextField.getText()).intValue();
height = width = dim;
antCanvas = new AntCanvas (dim, dim);
}
private class AntCanvas extends JPanel//Canvas
{
AntCanvas(int w,int h)
{
grid = new Color[w][h];
for (int r = 0; r < grid.length; r++)
{
for (int c = 0; c < grid[r].length; c++)
{
grid[r][c] = GRAY;
}
}
randomColors();
ant = new Ant ();
//random initial x position of ant
ant.x = (int)(Math.random() * width);
ant.x *= BLOCKSIZE;
ant.x += (BLOCKSIZE / 2);
//random initial y position of ant
ant.y = (int)(Math.random() * height);
ant.y *= BLOCKSIZE;
ant.y += (BLOCKSIZE / 2);
//random initial direction of the ant
ant.dir = (int)(Math.random() * 4);
}
//inserts red and white blocks in random spaces.
//the number of squares is determined from the JTextFields
private void randomColors ()
{
int red = new Integer(redSquaresTextField.getText()).intValue();
int white = new Integer(whiteSquaresTextField.getText()).intValue();
//draw red squares
for (int i = 0; i < red;)
{
int r = (int)(Math.random() * width);
int c = (int)(Math.random() * height);
if (grid[r][c] != RED)
{
grid[r][c] = RED;
i++;
}
if (i >= (height * width))
i = red;
}
//draw white squares
for (int i = 0; i < white;)
{
int r = (int)(Math.random() * width);
int c = (int)(Math.random() * height);
if (grid[r][c] != WHITE && grid[r][c] != RED)
{
grid[r][c] = WHITE;
i++;
}
if (i >= ((height * width) - red))
i = white;
}
}
public void paintComponent (Graphics g)
{
//draw grid
for (int r = 0; r < grid.length; r++)
{
for (int c = 0; c < grid[r].length; c++)
{
g.setColor (grid[r][c]);
g.fillRect (r * BLOCKSIZE, c * BLOCKSIZE, BLOCKSIZE, BLOCKSIZE);
}
}
//draw ant
ant.draw(g);
}
/*
public void update (Graphics g)
{
paint(g);
}
*/
}
private class Ant
{
public int x; //x co-ord of ant
public int y; //y co-ord of ant
public int dir; //direction of ant
private static final int DIAM = BLOCKSIZE; //diameter of ant
//change the dir of the ant and give the new color
//of the curret block
public void changeDirection ()
{
Color col = grid[(ant.x - BLOCKSIZE / 2) / BLOCKSIZE][(ant.y - BLOCKSIZE / 2) / BLOCKSIZE];
if (col == GRAY)
{
if (dir == WEST)
dir = NORTH;
else
dir++;
col = WHITE;
}
else if (col == WHITE)
{
if (dir == NORTH)
dir = WEST;
else
dir--;
col = RED;
}
else if (col == RED)
{
if (dir == NORTH || dir == SOUTH)
dir++;
else
{
if (dir == EAST)
dir = WEST;
else if (dir == WEST)
dir = EAST;
}
col = GRAY;
}
grid[(ant.x - BLOCKSIZE / 2) / BLOCKSIZE][(ant.y - BLOCKSIZE / 2) / BLOCKSIZE] = col;
}
//move the ant in the appropriate direction
public void move ()
{
switch (dir)
{
case NORTH: y -= BLOCKSIZE; break;
case EAST: x += BLOCKSIZE; break;
case SOUTH: y += BLOCKSIZE; break;
case WEST: x -= BLOCKSIZE; break;
}
//check to see if ant goes over the edge of the canvas
if (x > width * BLOCKSIZE)
x = BLOCKSIZE/2;
else if (x < 0)
x = (width * BLOCKSIZE) - (BLOCKSIZE/2);
if (y > height * BLOCKSIZE)
y = BLOCKSIZE/2;
else if (y < 0)
y = (height * BLOCKSIZE) - (BLOCKSIZE/2);
}
//draw ant
public void draw (Graphics g)
{
g.setColor (Color.black);
g.fillOval (x - (DIAM/2), y - (DIAM/2), DIAM, DIAM);
}
}
private class GoAntListener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
//change the direction of the ant and change the color of the square
ant.changeDirection();
ant.move();
repaint();
}
}
private class NextButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
//change the direction of the ant and change the color of the square
ant.changeDirection();
ant.move();
repaint();
}
}
private class RunButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
timer.start();
}
}
private class StopButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
timer.stop();
}
}
private class ResetButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
timer.stop();
changeCanvasSize();
repaint();
}
}
}
|
|