adding a second trail

How can I do it?

/**

**/

import java.awt.*;

import java.applet.Applet;

import java.net.URL;

import java.util.ArrayList;

import java.awt.event.*;

publicclass QASSextends Applet

{

publicfinalstaticint DELAY = 500;/* the thread delay is in milliseconds so we can see what happens */

private Thread thr;/* this thread runs the queen*/

publicvoid init ()

{

/* Labels and titles */

Label lblTitle =new Label ("A Knights Path", Label.CENTER);

lblTitle.setFont (new Font ("Arial", Font.BOLD, 25));

add (lblTitle);

Label lblTitle2 =new Label ("Press the button to Start", Label.CENTER);

lblTitle.setFont (new Font ("Arial", Font.BOLD, 10));

add (lblTitle2);

/*create a chessboard that draws lines as to where the queen has been on her travels*/

final ChessBoard chessbo =new ChessBoard (this);

add (chessbo);/* adds to the applet as we go */

/* This creates a button for the users to get the Queen to move */

final Choice ch =new Choice ();

ch.add ("");

ch.add ("");

/* creates a button so that the user can start the applet */

final Button btn =new Button ("Press here to make the Knight move");

// this creates the buttons action listener

ActionListener al;

al =new ActionListener ()

{

publicvoid actionPerformed (ActionEvent e)

{

Runnable r;

r =new Runnable ()

{

int [] boardPositions1 =

{

7, 47, 43, 27, 29, 21, 17, 57

/* i have an array list where the top left square is

/* position 1 and so on, flowing from left to right,

/* down to 64 in the bottom right square*/

};

publicvoid run ()

{

chessbo.reset ();

// thr != null is the check which stops the applet if the user moves away from the webpage it is on.

for (int i = 0; i < boardPositions1.length &&

thr !=null; i++)

{

chessbo.moveQueen (boardPositions1 [i]);

try

{

Thread.sleep (DELAY);

}

catch (InterruptedException e2)

{

}

}

ch.setEnabled (true);

btn.setEnabled (true);

}

};

ch.setEnabled (false);

btn.setEnabled (false);

thr =new Thread (r);

thr.start ();

}

};

btn.addActionListener (al);

// Add the Button object to the applet's Panel container.

add (btn);

}

// Stop the applet.

publicvoid stop ()

{

/* this Stops the applet if the page focus is lost */

thr =null;

}

}

finalclass ChessBoardextends Canvas

{

privatefinalstatic Color SQUARECOLOR =new Color (99, 99, 99);// Color of black squares in RGB notation

privatefinalstaticint SQUAREDIM = 30;// Chessboard Square Dimension

privatefinalstaticint BOARDDIM = 8 * SQUAREDIM + 2;// Chessboard Dimension which includes the black outline

privateint xboard;// Left coordinate of chessboard top-left corner

privateint yboard;// Top coordinate of chessboard top-left corner

privateint bwidth;// The board width

privateint bheight;// The board height

private Image imagebuffer;// Image buffer

private Graphics imGra;// Graphics context & image buffer

private Image imQueen;// Queen's image

privateint queenWidth;// Queen image width

privateint queenHeight;// Queen image height

private ArrayList trail;// Coordinates of the Queen's trail

privateint xcord;// right co-ordinate of queen origin (bottom right)

privateint ycord;// bottom co-ordinate of queen origin (bottom right)

// getImage and getDocumentBase methods are now called

Applet appl;

//this constructs chessbaord

ChessBoard (Applet appl)

{

bwidth = BOARDDIM+1;

bheight = BOARDDIM+1;

// centre the chessboard

xboard = (bwidth - BOARDDIM) / 2 + 1;

yboard = (bheight - BOARDDIM) / 2 + 1;

// makes sure image loads before its height and width are taken

MediaTracker mdtrac =new MediaTracker (this);

// Load queen's image.

imQueen = appl.getImage (appl.getDocumentBase (),"queentrans.gif");

mdtrac.addImage (imQueen, 0);

try

{

mdtrac.waitForID (0);

}

catch (InterruptedException e){}

// height and width are taken

queenWidth = imQueen.getWidth (appl);

queenHeight = imQueen.getHeight (appl);

xcord = xboard + (SQUAREDIM - queenWidth) / 2 + 1;

ycord = yboard + (SQUAREDIM - queenHeight) / 2 + 1;

// this holds the trail of the queen as she moves across the board

trail =new ArrayList ();

// Saves the applet reference for later use

this.appl = appl;

}

publicvoid addNotify ()

{

// creates canvas peer

super.addNotify ();

// Creates the image buffer.

imagebuffer = createImage (bwidth, bheight);

// Retrieve graphics context

imGra = imagebuffer.getGraphics ();

}

// This method is called when positioning the components

public Dimension getPreferredSize ()

{

returnnew Dimension (bwidth, bheight);

}

publicvoid moveQueen (int boardPosition)

{

int rebasedBoardPosition = boardPosition-1;

int col = rebasedBoardPosition % 8;

int row = rebasedBoardPosition / 8;

xcord = xboard + col * SQUAREDIM + (SQUAREDIM - queenWidth) / 2 + 1;

ycord = yboard + row * SQUAREDIM + (SQUAREDIM - queenHeight) / 2 + 1;

trail.add (new Point (xcord + queenWidth / 2, ycord + queenHeight / 2));

repaint ();

}

// chessboard and then queen are painted

publicvoid paint (Graphics graph)

{

// Paints the chessboard

paintChessBoard (imGra, xboard, yboard);

// Paints the queem

paintQueen (imGra, xcord, ycord);

// Paints the queen's trail behind her

paintTrail (imGra);

// Draws the contents of the image buffer

graph.drawImage (imagebuffer, 0, 0,this);

}

// Paints the chessboard (x, y) is the upper-left corner of the chess board

void paintChessBoard (Graphics graph,int x,int y)

{

// Paints the chessboard outline

graph.setColor (Color.black);

graph.drawRect (x, y, 8 * SQUAREDIM + 1, 8 * SQUAREDIM + 1);

// Paints the checks

for (int row = 0; row < 8; row++)

{

graph.setColor (((row & 1) != 0) ? SQUARECOLOR : Color.red);

for (int col = 0; col < 8; col++)

{

graph.fillRect (x + 1 + col * SQUAREDIM, y + 1 + row * SQUAREDIM,

SQUAREDIM, SQUAREDIM);

graph.setColor ((graph.getColor () == SQUARECOLOR) ? Color.red :

SQUARECOLOR);

}

}

}

// Paint the queen (x, y) is the upper-left corner of the chessboard

void paintQueen (Graphics graph,int x,int y)

{

graph.drawImage (imQueen, x, y, appl);

}

// Paint the queen's trail behind her

void paintTrail (Graphics graph)

{

graph.setColor (Color.black);

int len = trail.size ();

if (len == 0)

return;

Point poi = (Point) trail.get (0);

int xcord = poi.x;

int ycord = poi.y;

for (int i = 1; i < len; i++)

{

poi = (Point) trail.get (i);

graph.drawLine (xcord, ycord, poi.x, poi.y);

xcord = poi.x;

ycord = poi.y;

}

}

// clear the Array list to reset the applet

publicvoid reset ()

{

trail.clear ();

}

}

[14887 byte] By [akirrua] at [2007-10-3 3:28:23]
# 1
Do what?
CeciNEstPasUnProgrammeura at 2007-7-14 21:21:56 > top of Java-index,Java Essentials,New To Java...
# 2
Well, atm the program shows the trail of a knight from square to square on the board. I wan't to add a second trail (with and array) so I can give it diff coordinates.Thx
akirrua at 2007-7-14 21:21:56 > top of Java-index,Java Essentials,New To Java...
# 3
Do I create a second board?
akirrua at 2007-7-14 21:21:56 > top of Java-index,Java Essentials,New To Java...
# 4

Make a List (such as an ArrayList) of trails. Then, pass a Trail into paintTrail:

void paintTrail(Graphics graph, Trail currentTrail)

{

// paint currentTrail, using existing algorithm

}

In paint, make a loop to call paintTrail for every trail you have.

Currently, you would pass a List into paintTrail, because you don't have a Trail class:

void paintTrail(Graphics graph, List currentTrail)

MLRona at 2007-7-14 21:21:56 > top of Java-index,Java Essentials,New To Java...