java.awt.Graphics cannot be applied

Hi I'm creating a app that draws shapes and bounces them around the screen. I've followed and they had draw/paint methods to set colours/fills and draws the shape. the code below is not the whole app just the parts that is giving me trouble. I'm getting a draw(java.awt.Graphics) cannot be applied to () in both my Rectangle and Circle classes and I don't know why. : (

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.geom.*;

import java.awt.Color;

publicabstractclass Shape{

public String shapeName, shapeColour;

publicdouble xValue, yValue, sizeValue1, sizeValue2;

publicboolean fillCheck;

public Ellipse2D.Double cir;

public Rectangle2D.Double rec;

public Point2D.Double p;

publicint sWidth;

publicint sHeight;

publicint xV,yV;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public Shape(String shape, String colour,int xPos,int yPos,int size1,int size2,boolean filled)

{

shapeName = shape;

shapeColour = colour;

xValue = (double)xPos;

yValue = (double)yPos;

sizeValue1 = (double)size1;

sizeValue2 = (double)size2;

fillCheck = filled;

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

publicvoid setDirection()

{

int dir = (int)(8*Math.random());

switch(dir)

{

case 1:

xV=0; yV=-1;

break;

case 2:

xV=1; yV=-1;

break;

case 3:

xV=1; yV=0;

break;

case 4:

xV=1; yV=1;

break;

case 5:

xV=0; yV=1;

break;

case 6:

xV=-1; yV=1;

break;

case 7:

xV=-1; yV=0;

break;

case 8:

xV=-1; yV=-1;

break;

}

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//public abstract void createShape();

//public abstract void move(int xPos, int yPos, int screenW, int screenH);//not using cos it created can't override errors

//public abstract void drawShape();

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

}//end of Shape class

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Color;

import java.awt.geom.*;

import java.awt.Point;

publicclass Rectangleextends Shape{

int xV,yV;// shape velcoties

public Rectangle(String shape, String colour,int xPos,int yPos,int size1,int size2,boolean filled){

super(shape, colour, xPos, yPos, size1, size2, filled);

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

publicvoid createShape(){

p =new Point2D.Double(xValue, yValue);

double width = sizeValue1;

double height = sizeValue2;

rec =new Rectangle2D.Double(p.x, p.y, width, height);

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

publicvoid move(int xPos,int yPos,int screenW,int screenH){

xValue = (double)xPos;

yValue = (double)yPos;

sWidth = screenW;

sHeight = screenH;

if(xPos<=0 || (xPos >= sWidth-sizeValue1))

{xV=-xV;}

elseif(yPos<=0 || (yPos >= sHeight-sizeValue2))

{xV=-xV;}

//rect.setPosition(xPos+xV, yPos+yV);

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

publicvoid draw(Graphics g){

Graphics2D g2D = (Graphics2D)g;

System.out.println("Colour is set to "+shapeColour);

if(shapeColour.equals("red")){

g2D.setColor(Color.red);

}

elseif(shapeColour.equals("blue")){

g2D.setColor(Color.BLUE);

}

elseif(shapeColour.equals("green")){

g2D.setColor(Color.GREEN);

}

elseif(shapeColour.equals("yellow")){

g2D.setColor(Color.YELLOW);

}

if(fillCheck ==true)

{System.out.println("The shape will be filled");g2D.fill(rec);}

System.out.println("Drawing...");

g2D.draw(rec);

System.out.println("Done");

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

}//end of Rectangle class

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Color;

import java.awt.geom.*;

import java.awt.Point;

publicclass Circleextends Shape{

public Circle(String shape, String colour,int xPos,int yPos,int size1,int size2,boolean filled){

super(shape, colour, xPos, yPos, size1, size2, filled);// send to shape super class

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

publicvoid createShape(){

p =new Point2D.Double(xValue, yValue);// create a new point type double

double width = sizeValue1;// set width

double height = sizeValue2;// and height values type double

cir =new Ellipse2D.Double(p.x, p.y, width, height);// create new ellipse

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

publicvoid move(int xPos,int yPos,int screenW,int screenH){

xValue = (double)xPos;// cast from int to double

yValue = (double)yPos;

sWidth = screenW;

sHeight = screenH;

if(xPos<=0 || (xPos >= sWidth-sizeValue1))// if the shape is at the left or right of the screen

{xV = -xV;}// set xV to negative (--=+)

elseif(yPos<=0 || (yPos >= sHeight-sizeValue2))// if the shape is at the top or bottom of the screen

{xV=-xV;}// set yV to negative (--=+)

//cir.setPosition(xPos+xV, yPos+yV)

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

publicvoid draw(Graphics g){

Graphics2D g2D = (Graphics2D)g;//cast graphics to graphics2D

System.out.println("Colour is set to "+shapeColour);// debugger

if(shapeColour.equals("red")){// if sting equals ...

g2D.setColor(Color.red);//set to colour ...

}

elseif(shapeColour.equals("blue")){

g2D.setColor(Color.BLUE);

}

elseif(shapeColour.equals("green")){

g2D.setColor(Color.GREEN);

}

elseif(shapeColour.equals("yellow")){

g2D.setColor(Color.YELLOW);

}

if(fillCheck ==true)// check if fill is true

{System.out.println("The shape will be filled");//debugger

g2D.fill(cir);}// fill the shape

System.out.println("Drawing...");//debugger

g2D.draw(cir);// then draw the shape

System.out.println("Done");//debugger

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

}//ens of Circle class

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

import javax.swing.JPanel;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Color;

import java.awt.geom.*;

import java.awt.Point;

class Viewextends JPanel{

publicint screenW = getSize().width;

publicint screenH = getSize().height;

publicboolean stop;

publicstatic Rectangle rect;

publicstatic Circle circ;

publicstaticint j;

public View(Bounce theApp){

this.theApp = theApp;

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

publicstaticvoid createShapes()

{

for(j=0; j<5; j++)

{

if(Bounce.shape[j][0] ==null){break;}

if(Bounce.shape[j][0].equals("rectangle")){

System.out.println(Bounce.shape[j][0]);

rect =new Rectangle(Bounce.shape[j][0], Bounce.shape[j][1], Bounce.position[j][0], Bounce.position[j][1], Bounce.position[j][2], Bounce.position[j][3], Bounce.colourFill[j]);

rect.createShape();

rect.draw();

}

if(Bounce.shape[j][0].equals("circle")){

System.out.println(Bounce.shape[j][0]);

circ =new Circle(Bounce.shape[j][0], Bounce.shape[j][1], Bounce.position[j][0], Bounce.position[j][1], Bounce.position[j][2], Bounce.position[j][3], Bounce.colourFill[j]);

circ.createShape();

circ.draw();

}

}

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

publicvoid bounce()

{stop =false;

while(stop ==false)

{

if(Bounce.position[j][0]==0)

j=0;

rect.move(Bounce.position[j][0], Bounce.position[j][1], screenW, screenH);

circ.move(Bounce.position[j][0], Bounce.position[j][1], screenW, screenH);

repaint();

j++;

}

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

privatestatic Bounce theApp;

}//end of View class

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

This has been bugging me for a couple of days so hope someone can help. thanks

[19916 byte] By [Zhela] at [2007-10-2 9:06:39]
# 1

I copied the Shape, Rectangle and Circle class code and compiled it with no errors. I can't compile the View class because it needs the Bounce class. Since you did not post the full error message, I don't know how to advise you. If you copy and paste the the full error, some one may be able to provide a better response.

atmguya at 2007-7-16 23:13:43 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 2

So heres all the code for the app.

import java.awt.Toolkit;

import java.awt.Dimension;

import java.awt.Color;

import java.awt.BorderLayout;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.io.*;

import javax.swing.SwingUtilities;

import javax.swing.JOptionPane;

public class Bounce {

public static void main(String[] args)

{

theApp = new Bounce();

SwingUtilities.invokeLater(

new Runnable(){

public void run(){

theApp.createGUI();

}

});

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public void createGUI(){

window = new BounceGui("Bouncer");

Toolkit kit = window.getToolkit();

winSize = kit.getScreenSize();

window.setBounds(0, 0, winSize.width, winSize.height-30);

view = new View(this);

view.setBackground(Color.BLACK);

window.getContentPane().add(view, BorderLayout.CENTER);

window.setVisible(true);

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public static void loadFile(String filename)

{

// loads shape info to arrays (getLine)

String line;// calls getShapeData()

try{

RandomAccessFile r = new RandomAccessFile(filename, "r");

while((line = r.readLine()) !=null)

{

line = line.toLowerCase();

getShapeData(line);

i++;

}

}catch(IOException e){

JOptionPane.showMessageDialog(null, "Error - 101, \n"+ e,

"IO Exception", JOptionPane.ERROR_MESSAGE);}

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public static void saveFile(String filename)

{

String s,cf;

BufferedWriter bw = null;

try

{

bw = new BufferedWriter(new FileWriter(filename, false));

for(i=0;i<5;i++)

{

if(shape[i][0]==null)

break;

if(colourFill[i]==true)

{

cf="yes";

}else

cf="no";

s="Shape:"+shape[i][0]+",Position:"+position[i][0]+","+position[i][1]+",Size:"+position[i][2]

+","+position[i][3]+",Colour:"+shape[i][1]+",Fill:"+cf;

bw.write(s);

bw.newLine();

bw.flush();

}

bw.close();

}catch(IOException e){}

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public static void getShapeData(String line)

{

int start = 0;// accepts string and finds the shape data by calling getNextIdentifier(),

int finish = 0;// getShape(), getPosition() and getColourFill()

int s=0, p=0, f=0;

start = getNextIdentifier(line, ':');//finds the end of shape:

finish = getNextIdentifier(line, ',');//finds the end of the type of shape

getShape(line, start, finish, s);//passes the info so it can work out what is to be stored

s++;

line = line.substring(finish);//cut the string at the position of last identifier

start = getNextIdentifier(line, ':');//finds the end of position:

finish = getNextIdentifier(line, ',');//finds the end of the x co-ord

getPosition(line, start, finish, p);//passes the info so it can work out what is to be stored

p++;

line = line.substring(finish);// cut the string at the position of last identifier

start = 0;//start at 0 cos string has just been cut

finish = getNextIdentifier(line, ',');// finds the end of the y co-ord

getPosition(line, start, finish, p);// finds and stores

p++;

line = line.substring(finish);//cuts the line

start = getNextIdentifier(line, ':');//finds the end of size:

finish = getNextIdentifier(line, ',');//inds the end of the first size var

getPosition(line, start, finish, p);// stores in array

p++;

line = line.substring(finish);//cuts the line again

start = 0;//line just been cut so start at 0

finish = getNextIdentifier(line, ',');//find the end of the last size var

getPosition(line, start, finish, p);//and store again

p++;

line = line.substring(finish);

start = getNextIdentifier(line, ':');//finds the end of colour:

finish = getNextIdentifier(line, ',');//finds the end of the colour var

getShape(line, start, finish, s);//and saves

line = line.substring(finish);//final cut

start = getNextIdentifier(line, ':');//finds the end of fill:

getColourFill(line, start);// saves to array

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public static String[][] getShape(String line, int start, int finish, int s)//reads a string and returns shape[0] and shape[1]

{

int S=s;

shape[i][S]=line.substring(start, finish-1);// extract the string

return shape;// return the array

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public static int[][] getPosition(String line, int start, int finish, int p)// reads a string and returns int position[][] with (x,y) and size

{

int P=p;

int value;

String temp;

temp=line.substring(start, finish-1);

position[i][P]= Integer.valueOf(temp);// extract the sting

return position;// return array

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public static boolean[] getColourFill(String line, int start)// reads a string and returns colourFill[] with boolean true/false

{

String temp;

temp=line.substring(start);// extract string

if(temp.equals("no"))

{

colourFill[i]=false;

}

else if(temp.equals("yes"))

{

colourFill[i]=true;

}

return colourFill;

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public static int getNextIdentifier(String line, char ch)

{

int index;

index=line.indexOf(ch);

index=index+1;

return index;

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public static Bounce theApp;

public static BounceGui window;

public static View view;

public Dimension winSize;

public static String[][] shape = new String[5][2];

public static int[][] position = new int[5][4];

public static boolean[] colourFill = new boolean[5];

public static int i = 0;

}//end of Bounce class

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

import java.awt.Toolkit;

import java.awt.Dimension;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.util.Observable;

import java.lang.String;

import javax.swing.JComponent;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.Box;

import javax.swing.border.Border;

import javax.swing.border.TitledBorder;

import javax.swing.border.EtchedBorder;

import javax.swing.border.CompoundBorder;

import javax.swing.border.BevelBorder;

import javax.swing.JToolBar;

import javax.swing.JMenuBar;

import javax.swing.JMenu;

import javax.swing.JMenuItem;

import javax.swing.JButton;

import javax.swing.JRadioButton;

import javax.swing.JOptionPane;

import javax.swing.JCheckBox;

import javax.swing.JLabel;

import javax.swing.JComboBox;

import javax.swing.JTextField;

import javax.swing.BorderFactory;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public class BounceGui extends JFrame implements ActionListener{

public BounceGui(String title){

setTitle(title);

setJMenuBar(menuBar);

setDefaultCloseOperation(EXIT_ON_CLOSE);

fileMenu = new JMenu("File");

shapeMenu = new JMenu("Shape");

shapeMenu.setEnabled(false);

aboutMenu = new JMenu("About");

loadItem = new JMenuItem("Load");

loadItem.addActionListener(this);

saveItem = new JMenuItem("Save");

saveItem.addActionListener(this);

closeItem = new JMenuItem("Close");

closeItem.addActionListener(this);

formatItem = new JMenuItem("Format Shape");

formatItem.addActionListener(this);

displayItem = new JMenuItem("Display Shapes");

displayItem.addActionListener(this);

runItem = new JMenuItem("Run");

runItem.addActionListener(this);

stopItem = new JMenuItem("Stop");

stopItem.addActionListener(this);

aboutItem = new JMenuItem("About Bounce V.2.34");

aboutItem.addActionListener(this);

fileMenu.add(loadItem);

fileMenu.add(saveItem);

fileMenu.addSeparator();

fileMenu.add(closeItem);

shapeMenu.add(formatItem);

shapeMenu.add(displayItem);

shapeMenu.addSeparator();

shapeMenu.add(runItem);

shapeMenu.add(stopItem);

aboutMenu.add(aboutItem);

menuBar.add(fileMenu);

menuBar.add(shapeMenu);

menuBar.add(aboutMenu);

displayButton = new JButton("Display Shapes");

displayButton.addActionListener(this);

displayButton.setEnabled(false);

displayButton.setBorder(BorderFactory.createRaisedBevelBorder());

runButton = new JButton("Run");

runButton.addActionListener(this);

runButton.setEnabled(false);

runButton.setBorder(BorderFactory.createRaisedBevelBorder());

stopButton = new JButton("Stop");

stopButton.addActionListener(this);

stopButton.setEnabled(false);

stopButton.setBorder(BorderFactory.createRaisedBevelBorder());

toolBar.addSeparator();

toolBar.add(displayButton);

toolBar.addSeparator();

toolBar.add(runButton);

toolBar.add(stopButton);

toolBar.setFloatable(false);

toolBar.setBorder(BorderFactory.createEtchedBorder());

getContentPane().add(toolBar, BorderLayout.NORTH);

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public void createFormatGUI(){

fWindow = new formatGUI("Format Shape");

Toolkit kit = fWindow.getToolkit();

Dimension winSize = kit.getScreenSize();

fWindow.setBounds(winSize.width/4, winSize.height/4, winSize.width/2, winSize.height/2);

fWindow.getContentPane();

fWindow.pack();

fWindow.setVisible(true);

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

private JMenuBar menuBar = new JMenuBar();

private JToolBar toolBar = new JToolBar();

public static JMenu fileMenu, shapeMenu, bounceMenu, aboutMenu;

public static JMenuItem loadItem, saveItem, closeItem, formatItem, displayItem, runItem, stopItem, aboutItem;

public static JButton displayButton, runButton, stopButton;

public static formatGUI fWindow;

public String filename = "data.txt";

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public void actionPerformed(ActionEvent e){

if(e.getSource() == loadItem){

Bounce.loadFile(filename);

shapeMenu.setEnabled(true);

displayButton.setEnabled(true);

}

if(e.getSource() == saveItem){

Bounce.saveFile(filename);

}

if(e.getSource() == closeItem){

System.exit(0);

}

if(e.getSource() == formatItem){

createFormatGUI();

}

if(e.getSource() == aboutItem){

JOptionPane.showMessageDialog(null, "Bounce Designed by: Zhel Corp, Software Dept. 2005\nUBU: 03007270",

"About Bounce V.2.34", JOptionPane.INFORMATION_MESSAGE);

}

if(e.getSource() == displayItem){

View.createShapes();

runButton.setEnabled(true);

stopButton.setEnabled(true);

}

if(e.getSource() == displayButton){

View.createShapes();

runButton.setEnabled(true);

stopButton.setEnabled(true);

}

}

}//end of bounceGUI class

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

class BounceConstants{

// colour constants

// So I can use these to set/change the shapes/colours

public final static int RED = 101;

public final static int BLUE = 102;

public final static int GREEN = 103;

public final static int YELLOW = 104;

// shape constants

public final static int SQUARE = 201;

public final static int CIRCLE = 202;

public final static int TRIANGLE = 203;

}//end of BounceConstants class

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

import java.awt.Toolkit;

import java.awt.Dimension;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.lang.String;

import javax.swing.JComponent;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.Box;

import javax.swing.border.Border;

import javax.swing.border.TitledBorder;

import javax.swing.border.EtchedBorder;

import javax.swing.border.CompoundBorder;

import javax.swing.border.BevelBorder;

import javax.swing.JButton;

import javax.swing.JRadioButton;

import javax.swing.JOptionPane;

import javax.swing.JCheckBox;

import javax.swing.JLabel;

import javax.swing.JComboBox;

import javax.swing.JTextField;

import javax.swing.BorderFactory;

public class formatGUI extends JFrame implements ActionListener{

public formatGUI(String title)

{int j;

setTitle(title);

setDefaultCloseOperation(DISPOSE_ON_CLOSE);

Box top = Box.createHorizontalBox();

String[] names = new String[5];

for(j=0;j<names.length;j++)

{

if(Bounce.shape[j][0]==null)

break;

names[j]=Bounce.shape[j][1]+", "+Bounce.shape[j][0];

}

shapeList = new JComboBox(names);

shapeList.addActionListener(this);

top.add(shapeList);

top.setBorder(BorderFactory.createEtchedBorder());

red = new JRadioButton("Red");

red.addActionListener(this);

blue = new JRadioButton("Blue");

blue.addActionListener(this);

green = new JRadioButton("Green");

green.addActionListener(this);

yellow = new JRadioButton("Yellow");

yellow.addActionListener(this);

fill = new JCheckBox("Fill");

fill.addActionListener(this);

Box left = Box.createVerticalBox();

left.setBorder(new TitledBorder(new EtchedBorder(),"Color"));

left.add(Box.createVerticalStrut(25));

left.add(red);

left.add(Box.createVerticalStrut(25));

left.add(blue);

left.add(Box.createVerticalStrut(25));

left.add(green);

left.add(Box.createVerticalStrut(25));

left.add(yellow);

left.add(Box.createVerticalStrut(25));

left.add(fill);

JPanel bottom = new JPanel();

bottom.setBorder(new CompoundBorder(

BorderFactory.createLineBorder(Color.black, 1),

BorderFactory.createBevelBorder(BevelBorder.RAISED)));

fOk = new JButton("Ok");

fOk.addActionListener(this);

fCancel = new JButton("Cancel");

fCancel.addActionListener(this);

fApply = new JButton("Apply");

fApply.addActionListener(this);

bottom.add(fOk);

bottom.add(Box.createGlue());

bottom.add(fCancel);

bottom.add(Box.createGlue());

bottom.add(fApply);

Box centre = Box.createHorizontalBox();

centre.add(left);

getContentPane().add(top, BorderLayout.NORTH);

getContentPane().add(centre, BorderLayout.CENTER);

getContentPane().add(bottom, BorderLayout.SOUTH);

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//Define objects and variables

private JRadioButton red;

private JRadioButton blue;

private JRadioButton green;

private JRadioButton yellow;

private JCheckBox fill;

private JComboBox shapeList;

private JButton fOk;

private JButton fCancel;

private JButton fApply;

private int i;

private int colour;

private boolean isFilled;

private String array;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

private void setArrays(int colour, boolean isFilled){

setShapeArray(colour);

setFillArray(isFilled);

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

private String[][] setShapeArray(int colour){

//sets the colour

switch(colour){

case BounceConstants.RED:

Bounce.shape[i][1]="red";

break;

case BounceConstants.BLUE:

Bounce.shape[i][1]="blue";

break;

case BounceConstants.GREEN:

Bounce.shape[i][1]="green";

break;

case BounceConstants.YELLOW:

Bounce.shape[i][1]="yellow";

break;

}

return Bounce.shape;

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

private boolean[] setFillArray(boolean isFilled){

Bounce.colourFill[i] = isFilled;

return Bounce.colourFill;

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

private void getArrayIndex(String array)

{

int s;

String colour, shape;

s = array.indexOf(",");

colour=array.substring(0,s);

shape=array.substring(s+2);

getIndex(colour, shape);

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

private void getIndex(String colour, String shape)

{

for(i=0; i><5; i++)

{

if((Bounce.shape[i][0].equals(shape))&(Bounce.shape[i][1].equals(colour)))

break;

}

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

private void configUI()

{

configColour();

configFill();

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

private void configColour()

{

String colour = Bounce.shape[i][1];

if(colour.equals("red"))

{

red.setSelected(true);

blue.setSelected(false);// make sure only one option is selected

green.setSelected(false);

yellow.setSelected(false);

}

if(colour.equals("blue"))

{

red.setSelected(false);

blue.setSelected(true);// make sure only one option is selected

green.setSelected(false);

yellow.setSelected(false);

}

if(colour.equals("green"))

{

red.setSelected(false);

blue.setSelected(false);// make sure only one option is selected

green.setSelected(true);

yellow.setSelected(false);

}

if(colour.equals("yellow"))

{

red.setSelected(false);

blue.setSelected(false);// make sure only one option is selected

green.setSelected(false);

yellow.setSelected(true);

}

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

private void configFill()

{

if(Bounce.colourFill[i]==true){

fill.setSelected(true);

}else

fill.setSelected(false);

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public void actionPerformed(ActionEvent e){

if(e.getSource() == shapeList){

JComboBox cb = (JComboBox)e.getSource();

array = (String)cb.getSelectedItem();

getArrayIndex(array);

configUI();

}

if(e.getSource() == fOk){

// re-paint

BounceGui.fWindow.setVisible(false);// see below

dispose();

}

if(e.getSource() == fCancel){

BounceGui.fWindow.setVisible(false);//here the window is made inviable

dispose();// then all the resorses locked up are freed

}

if(e.getSource() == fApply){

setArrays(colour, isFilled);// change the arrays

}

if(e.getSource() == red){

colour=BounceConstants.RED;//set colour to Red

blue.setSelected(false);// make sure only one option is selected

green.setSelected(false);

yellow.setSelected(false);

}

if(e.getSource() == blue){

colour=BounceConstants.BLUE;//set colour to Blue

red.setSelected(false);// make sure only one option is selected

green.setSelected(false);

yellow.setSelected(false);

}

if(e.getSource() == green){

colour=BounceConstants.GREEN;//set colour to Green

red.setSelected(false);// make sure only one option is selected

blue.setSelected(false);

yellow.setSelected(false);

}

if(e.getSource() == yellow){

colour=BounceConstants.YELLOW;//set colour to Yellow

red.setSelected(false);// make sure only one option is selected

blue.setSelected(false);

green.setSelected(false);

}

if(e.getSource() == fill){

isFilled = true;

}

}

}// end of formatGUI class

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.geom.*;

import java.awt.Color;

public abstract class Shape{

public String shapeName, shapeColour;

public double xValue, yValue, sizeValue1, sizeValue2;

public boolean fillCheck;

public Ellipse2D.Double cir;

public Rectangle2D.Double rec;

public Point2D.Double p;

public int sWidth;

public int sHeight;

public int xV,yV;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public Shape(String shape, String colour, int xPos, int yPos, int size1, int size2, boolean filled)

{

shapeName = shape;

shapeColour = colour;

xValue = (double)xPos;

yValue = (double)yPos;

sizeValue1 = (double)size1;

sizeValue2 = (double)size2;

fillCheck = filled;

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public void setDirection()

{

int dir = (int)(8*Math.random());

switch(dir)

{

case 1:

xV=0; yV=-1;

break;

case 2:

xV=1; yV=-1;

break;

case 3:

xV=1; yV=0;

break;

case 4:

xV=1; yV=1;

break;

case 5:

xV=0; yV=1;

break;

case 6:

xV=-1; yV=1;

break;

case 7:

xV=-1; yV=0;

break;

case 8:

xV=-1; yV=-1;

break;

}

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//public abstract void createShape();

//public abstract void move(int xPos, int yPos, int screenW, int screenH);//not using cos it created can't override errors

//public abstract void drawShape();

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

}//end of Shape class

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Color;

import java.awt.geom.*;

import java.awt.Point;

public class Rectangle extends Shape{

int xV,yV; // shape velcoties

public Rectangle(String shape, String colour, int xPos, int yPos, int size1, int size2, boolean filled){

super(shape, colour, xPos, yPos, size1, size2, filled);

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public void createShape(){

p = new Point2D.Double(xValue, yValue);

double width = sizeValue1;

double height = sizeValue2;

rec = new Rectangle2D.Double(p.x, p.y, width, height);

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public void move(int xPos, int yPos, int screenW, int screenH){

xValue = (double)xPos;

yValue = (double)yPos;

sWidth = screenW;

sHeight = screenH;

if(xPos<=0 || (xPos >= sWidth-sizeValue1))

{xV=-xV;}

else if(yPos<=0 || (yPos >= sHeight-sizeValue2))

{xV=-xV;}

//rect.setPosition(xPos+xV, yPos+yV);

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public void draw(Graphics g){

Graphics2D g2D = (Graphics2D)g;

System.out.println("Colour is set to "+shapeColour);

if(shapeColour.equals("red")){

g2D.setColor(Color.red);

}

else if(shapeColour.equals("blue")){

g2D.setColor(Color.BLUE);

}

else if(shapeColour.equals("green")){

g2D.setColor(Color.GREEN);

}

else if(shapeColour.equals("yellow")){

g2D.setColor(Color.YELLOW);

}

if(fillCheck == true)

{System.out.println("The shape will be filled");g2D.fill(rec);}

System.out.println("Drawing...");

g2D.draw(rec);

System.out.println("Done");

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

}//end of Rectangle class

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Color;

import java.awt.geom.*;

import java.awt.Point;

public class Circle extends Shape{

public Circle(String shape, String colour, int xPos, int yPos, int size1, int size2, boolean filled){

super(shape, colour, xPos, yPos, size1, size2, filled);// send to shape super class

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public void createShape(){

p = new Point2D.Double(xValue, yValue);// create a new point type double

double width = sizeValue1;// set width

double height = sizeValue2;// and height values type double

cir = new Ellipse2D.Double(p.x, p.y, width, height);// create new ellipse

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public void move(int xPos, int yPos, int screenW, int screenH){

xValue = (double)xPos;// cast from int to double

yValue = (double)yPos;

sWidth = screenW;

sHeight = screenH;

if(xPos<=0 || (xPos >= sWidth-sizeValue1))// if the shape is at the left or right of the screen

{xV = -xV;}// set xV to negative (--=+)

else if(yPos<=0 || (yPos >= sHeight-sizeValue2))// if the shape is at the top or bottom of the screen

{xV=-xV;}// set yV to negative (--=+)

//cir.setPosition(xPos+xV, yPos+yV)

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public void draw(Graphics g){

Graphics2D g2D = (Graphics2D)g;//cast graphics to graphics2D

System.out.println("Colour is set to "+shapeColour);// debugger

if(shapeColour.equals("red")){// if sting equals ...

g2D.setColor(Color.red);//set to colour ...

}

else if(shapeColour.equals("blue")){

g2D.setColor(Color.BLUE);

}

else if(shapeColour.equals("green")){

g2D.setColor(Color.GREEN);

}

else if(shapeColour.equals("yellow")){

g2D.setColor(Color.YELLOW);

}

if(fillCheck == true)// check if fill is true

{System.out.println("The shape will be filled");//debugger

g2D.fill(cir);}// fill the shape

System.out.println("Drawing...");//debugger

g2D.draw(cir);// then draw the shape

System.out.println("Done");//debugger

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

}//ens of Circle class

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

import javax.swing.JPanel;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Color;

import java.awt.geom.*;

import java.awt.Point;

class View extends JPanel{

public int screenW = getSize().width;

public int screenH = getSize().height;

public boolean stop;

public static Rectangle rect;

public static Circle circ;

public static int j;

public View(Bounce theApp){

this.theApp = theApp;

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public static void createShapes()

{

for(j=0; j<5; j++)

{

if(Bounce.shape[j][0] == null){break;}

if(Bounce.shape[j][0].equals("rectangle")){

System.out.println(Bounce.shape[j][0]);

rect = new Rectangle(Bounce.shape[j][0], Bounce.shape[j][1], Bounce.position[j][0], Bounce.position[j][1], Bounce.position[j][2], Bounce.position[j][3], Bounce.colourFill[j]);

rect.createShape();

rect.draw();

}

if(Bounce.shape[j][0].equals("circle")){

System.out.println(Bounce.shape[j][0]);

circ = new Circle(Bounce.shape[j][0], Bounce.shape[j][1], Bounce.position[j][0], Bounce.position[j][1], Bounce.position[j][2], Bounce.position[j][3], Bounce.colourFill[j]);

circ.createShape();

circ.draw();

}

}

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public void bounce()

{stop = false;

while(stop == false)

{

if(Bounce.position[j][0]==0)

j=0;

rect.move(Bounce.position[j][0], Bounce.position[j][1], screenW, screenH);

circ.move(Bounce.position[j][0], Bounce.position[j][1], screenW, screenH);

repaint();

j++;

}

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

private static Bounce theApp;

}//end of View class

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

the errors,

.\View.java:31: draw(java.awt.Graphics) in Rectangle cannot be applied to ()

rect.draw();

.\View.java:38: draw(java.awt.Graphics) in Circle cannot be applied to ()

circ.draw();

2 errors

Zhela at 2007-7-16 23:13:43 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 3

> the errors,

> .\View.java:31: draw(java.awt.Graphics) in Rectangle cannot be applied to ()

rect.draw();

> .\View.java:38: draw(java.awt.Graphics) in Circle cannot be applied to ()

circ.draw();

In the View class, you have the lines:

rect.draw();

circ.draw();

But in the code for Rectangle, Circle and Shape, there is no method draw(). You only have methods for draw(Graphics g). You will have to supply a Graphics object to the existing method calls or create a new draw() method that figures out its own Graphics object.

It is a simple error to diagnose but resolving the problem may not be so simple.

atmguya at 2007-7-16 23:13:43 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 4

well thanks for that it helped I've now called my draw methods for the first time. In the View class,

import javax.swing.JPanel;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Color;

import java.awt.geom.*;

import java.awt.Point;

class View extends JPanel{

public int screenW = getSize().width;

public int screenH = getSize().height;

public boolean stop;

private static Bounce theApp;

public static Rectangle rect;

public static Circle circ;

public static int j;

public static Graphics g;

public View(Bounce theApp){

this.theApp = theApp;

g = this.getGraphics();

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public static void createShapes()

{

for(j=0; j<5; j++)

{

if(Bounce.shape[j][0] == null){break;}

if(Bounce.shape[j][0].equals("rectangle")){

System.out.println(Bounce.shape[j][0]);

rect = new Rectangle(Bounce.shape[j][0], Bounce.shape[j][1], Bounce.position[j][0], Bounce.position[j][1], Bounce.position[j][2], Bounce.position[j][3], Bounce.colourFill[j]);

rect.createShape();

rect.draw(g);

}

if(Bounce.shape[j][0].equals("circle")){

System.out.println(Bounce.shape[j][0]);

circ = new Circle(Bounce.shape[j][0], Bounce.shape[j][1], Bounce.position[j][0], Bounce.position[j][1], Bounce.position[j][2], Bounce.position[j][3], Bounce.colourFill[j]);

circ.createShape();

circ.draw(g);

}

}

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public void bounce()

{stop = false;

while(stop == false)

{

if(Bounce.position[j][0]==0)

j=0;

rect.move(Bounce.position[j][0], Bounce.position[j][1], screenW, screenH);

circ.move(Bounce.position[j][0], Bounce.position[j][1], screenW, screenH);

repaint();

j++;

}

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

}

I am calling g = this.getGraphics(); // line 21

to set g. And the compiler likes this but getGraphics returns null so when I run the app I'm getting,

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

at Circle.draw(Circle.java:43)

at View.createShapes(View.java:42)

at BounceGui.actionPerformed(BounceGui.java:149)

I was wondering to set g so its not null. thanks

Zhela at 2007-7-16 23:13:43 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 5

Yes, getGraphics will return null if the component is not currently displayable. You are trying to draw directly onto components without using the paint subsystem. You might be able to make this work by calling getGraphics after the JPanel is displayed. But the better way is to follow the guidelines here:

http://java.sun.com/products/jfc/tsc/articles/painting/index.html

Of course, this means re-writing a lot of your code.

atmguya at 2007-7-16 23:13:43 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 6

Hello again. thanks for your help much appreciated. After reading the link in your last post. I've spent the day re-witing and ironing out my code.

The problem I have is, I dont think the jpanel is being added to the app (in my Bounce class) and because of that when I try to draw my shapes they have no where to go.

heres the code.

import java.awt.Toolkit;

import java.awt.Dimension;

import java.awt.Color;

import java.awt.BorderLayout;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.awt.Graphics;

import java.io.*;

import javax.swing.SwingUtilities;

import javax.swing.JOptionPane;

public class Bounce {

public static void main(String[] args)

{

theApp = new Bounce();

SwingUtilities.invokeLater(

new Runnable(){

public void run(){

theApp.createGUI();

}

});

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public void createGUI(){

window = new BounceGui("Bouncer");

Toolkit kit = window.getToolkit();

winSize = kit.getScreenSize();

window.setBounds(0, 0, winSize.width, winSize.height-30);

aShape = new Shape(this);

aShape.setBackground(Color.BLACK);//background is not set

window.getContentPane().add(aShape, BorderLayout.CENTER);

window.setVisible(true);

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public static void loadFile(String filename)

{

// loads shape info to arrays (getLine)

String line;// calls getShapeData()

try{

RandomAccessFile r = new RandomAccessFile(filename, "r");

while((line = r.readLine()) !=null)

{

line = line.toLowerCase();

getShapeData(line);

if(shape[i][0].equals("circle"))

{

Circle circle = new Circle(shape[i][0], shape[i][1], position[i][0], position[i][1], position[i][2], position[i][3], colourFill[i]);}

if(shape[i][0].equals("rectangle"))

{

Rectangle rectangle = new Rectangle(shape[i][0], shape[i][1], position[i][0], position[i][1], position[i][2], position[i][3], colourFill[i]);}

i++;

}

}catch(IOException e){

JOptionPane.showMessageDialog(null, "Error - 101, \n"+ e,

"IO Exception", JOptionPane.ERROR_MESSAGE);}

//Shape.paint = true;

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public static void saveFile(String filename){

String s,cf;

BufferedWriter bw = null;

try

{

bw = new BufferedWriter(new FileWriter(filename, false));

for(i=0;i<5;i++)

{

if(shape[i][0]==null)

break;

if(colourFill[i]==true)

{

cf="yes";

}else

cf="no";

s="Shape:"+shape[i][0]+",Position:"+position[i][0]+","+position[i][1]+",Size:"+position[i][2]

+","+position[i][3]+",Colour:"+shape[i][1]+",Fill:"+cf;

bw.write(s);

bw.newLine();

bw.flush();

}

bw.close();

}catch(IOException e){}

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public static void getShapeData(String line)

{

int start = 0;// accepts string and finds the shape data by calling getNextIdentifier(),

int finish = 0;// getShape(), getPosition() and getColourFill()

int s=0, p=0, f=0;

start = getNextIdentifier(line, ':');//finds the end of shape:

finish = getNextIdentifier(line, ',');//finds the end of the type of shape

getShape(line, start, finish, s);//passes the info so it can work out what is to be stored

s++;

line = line.substring(finish);//cut the string at the position of last identifier

start = getNextIdentifier(line, ':');//finds the end of position:

finish = getNextIdentifier(line, ',');//finds the end of the x co-ord

getPosition(line, start, finish, p);//passes the info so it can work out what is to be stored

p++;

line = line.substring(finish);// cut the string at the position of last identifier

start = 0;//start at 0 cos string has just been cut

finish = getNextIdentifier(line, ',');// finds the end of the y co-ord

getPosition(line, start, finish, p);// finds and stores

p++;

line = line.substring(finish);//cuts the line

start = getNextIdentifier(line, ':');//finds the end of size:

finish = getNextIdentifier(line, ',');//inds the end of the first size var

getPosition(line, start, finish, p);// stores in array

p++;

line = line.substring(finish);//cuts the line again

start = 0;//line just been cut so start at 0

finish = getNextIdentifier(line, ',');//find the end of the last size var

getPosition(line, start, finish, p);//and store again

p++;

line = line.substring(finish);

start = getNextIdentifier(line, ':');//finds the end of colour:

finish = getNextIdentifier(line, ',');//finds the end of the colour var

getShape(line, start, finish, s);//and saves

line = line.substring(finish);//final cut

start = getNextIdentifier(line, ':');//finds the end of fill:

getColourFill(line, start);// saves to array

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public static String[][] getShape(String line, int start, int finish, int s)//reads a string and returns shape[0] and shape[1]

{

int S=s;

shape[i][S]=line.substring(start, finish-1);// extract the string

return shape;// return the array

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public static int[][] getPosition(String line, int start, int finish, int p)// reads a string and returns int position[][] with (x,y) and size

{

int P=p;

int value;

String temp;

temp=line.substring(start, finish-1);

position[i][P]= Integer.valueOf(temp);// extract the sting

return position;// return array

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public static boolean[] getColourFill(String line, int start)// reads a string and returns colourFill[] with boolean true/false

{

String temp;

temp=line.substring(start);// extract string

if(temp.equals("no"))

{

colourFill[i]=false;

}

else if(temp.equals("yes"))

{

colourFill[i]=true;

}

return colourFill;

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public static int getNextIdentifier(String line, char ch)

{

int index;

index=line.indexOf(ch);

index=index+1;

return index;

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public static Bounce theApp;

public static BounceGui window;

public Shape aShape;

public Dimension winSize;

public static String[][] shape = new String[5][2];

public static int[][] position = new int[5][4];

public static boolean[] colourFill = new boolean[5];

public static int i = 0;

}//end of bounce

import java.awt.Toolkit;

import java.awt.Dimension;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.util.Observable;

import java.lang.String;

import javax.swing.JComponent;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.Box;

import javax.swing.border.Border;

import javax.swing.border.TitledBorder;

import javax.swing.border.EtchedBorder;

import javax.swing.border.CompoundBorder;

import javax.swing.border.BevelBorder;

import javax.swing.JToolBar;

import javax.swing.JMenuBar;

import javax.swing.JMenu;

import javax.swing.JMenuItem;

import javax.swing.JButton;

import javax.swing.JRadioButton;

import javax.swing.JOptionPane;

import javax.swing.JCheckBox;

import javax.swing.JLabel;

import javax.swing.JComboBox;

import javax.swing.JTextField;

import javax.swing.BorderFactory;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public class BounceGui extends JFrame implements ActionListener{

public BounceGui(String title){

setTitle(title);

setJMenuBar(menuBar);

setDefaultCloseOperation(EXIT_ON_CLOSE);

fileMenu = new JMenu("File");

shapeMenu = new JMenu("Shape");

shapeMenu.setEnabled(false);

aboutMenu = new JMenu("About");

loadItem = new JMenuItem("Load");

loadItem.addActionListener(this);

saveItem = new JMenuItem("Save");

saveItem.addActionListener(this);

closeItem = new JMenuItem("Close");

closeItem.addActionListener(this);

formatItem = new JMenuItem("Format Shape");

formatItem.addActionListener(this);

runItem = new JMenuItem("Run");

runItem.addActionListener(this);

stopItem = new JMenuItem("Stop");

stopItem.addActionListener(this);

aboutItem = new JMenuItem("About Bounce V.2.34");

aboutItem.addActionListener(this);

fileMenu.add(loadItem);

fileMenu.add(saveItem);

fileMenu.addSeparator();

fileMenu.add(closeItem);

shapeMenu.add(formatItem);

shapeMenu.addSeparator();

shapeMenu.add(runItem);

shapeMenu.add(stopItem);

aboutMenu.add(aboutItem);

menuBar.add(fileMenu);

menuBar.add(shapeMenu);

menuBar.add(aboutMenu);

runButton = new JButton("Run");

runButton.addActionListener(this);

runButton.setEnabled(false);

runButton.setBorder(BorderFactory.createRaisedBevelBorder());

stopButton = new JButton("Stop");

stopButton.addActionListener(this);

stopButton.setEnabled(false);

stopButton.setBorder(BorderFactory.createRaisedBevelBorder());

toolBar.addSeparator();

toolBar.add(runButton);

toolBar.add(stopButton);

toolBar.setFloatable(false);

toolBar.setBorder(BorderFactory.createEtchedBorder());

getContentPane().add(toolBar, BorderLayout.NORTH);

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public void createFormatGUI(){

fWindow = new formatGUI("Format Shape");

Toolkit kit = fWindow.getToolkit();

Dimension winSize = kit.getScreenSize();

fWindow.setBounds(winSize.width/4, winSize.height/4, winSize.width/2, winSize.height/2);

fWindow.getContentPane();

fWindow.pack();

fWindow.setVisible(true);

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

private JMenuBar menuBar = new JMenuBar();

private JToolBar toolBar = new JToolBar();

public static JMenu fileMenu, shapeMenu, bounceMenu, aboutMenu;

public static JMenuItem loadItem, saveItem, closeItem, formatItem, runItem, stopItem, aboutItem;

public static JButton runButton, stopButton;

public static formatGUI fWindow;

public String filename = "data.txt";

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public void actionPerformed(ActionEvent e){

if(e.getSource() == loadItem){

Bounce.loadFile(filename);

shapeMenu.setEnabled(true);

runButton.setEnabled(true);

stopButton.setEnabled(true);

}

if(e.getSource() == saveItem){

Bounce.saveFile(filename);

}

if(e.getSource() == closeItem){

System.exit(0);

}

if(e.getSource() == formatItem){

createFormatGUI();

}

if(e.getSource() == aboutItem){

JOptionPane.showMessageDialog(null, "Bounce Designed by: Zhel Corp, Software Dept. 2005\nUBU: 03007270",

"About Bounce V.2.34", JOptionPane.INFORMATION_MESSAGE);

}

if(e.getSource() == runItem){

Shape.paint = true;

}

}

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

class BounceConstants{

// colour constants

// So I can use these to set/change the shapes/colours

public final static int RED = 101;

public final static int BLUE = 102;

public final static int GREEN = 103;

public final static int YELLOW = 104;

// shape constants

public final static int SQUARE = 201;

public final static int CIRCLE = 202;

public final static int TRIANGLE = 203;

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////import java.awt.Toolkit;

import java.awt.Dimension;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.lang.String;

import javax.swing.JComponent;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.Box;

import javax.swing.border.Border;

import javax.swing.border.TitledBorder;

import javax.swing.border.EtchedBorder;

import javax.swing.border.CompoundBorder;

import javax.swing.border.BevelBorder;

import javax.swing.JButton;

import javax.swing.JRadioButton;

import javax.swing.JOptionPane;

import javax.swing.JCheckBox;

import javax.swing.JLabel;

import javax.swing.JComboBox;

import javax.swing.JTextField;

import javax.swing.BorderFactory;

public class formatGUI extends JFrame implements ActionListener{

public formatGUI(String title)

{int j;

setTitle(title);

setDefaultCloseOperation(DISPOSE_ON_CLOSE);

Box top = Box.createHorizontalBox();

String[] names = new String[5];

for(j=0;j<names.length;j++)

{

if(Bounce.shape[j][0]==null)

break;

names[j]=Bounce.shape[j][1]+", "+Bounce.shape[j][0];

}

shapeList = new JComboBox(names);

shapeList.addActionListener(this);

top.add(shapeList);

top.setBorder(BorderFactory.createEtchedBorder());

red = new JRadioButton("Red");

red.addActionListener(this);

blue = new JRadioButton("Blue");

blue.addActionListener(this);

green = new JRadioButton("Green");

green.addActionListener(this);

yellow = new JRadioButton("Yellow");

yellow.addActionListener(this);

fill = new JCheckBox("Fill");

fill.addActionListener(this);

Box left = Box.createVerticalBox();

left.setBorder(new TitledBorder(new EtchedBorder(),"Color"));

left.add(Box.createVerticalStrut(25));

left.add(red);

left.add(Box.createVerticalStrut(25));

left.add(blue);

left.add(Box.createVerticalStrut(25));

left.add(green);

left.add(Box.createVerticalStrut(25));

left.add(yellow);

left.add(Box.createVerticalStrut(25));

left.add(fill);

JPanel bottom = new JPanel();

bottom.setBorder(new CompoundBorder(

BorderFactory.createLineBorder(Color.black, 1),

BorderFactory.createBevelBorder(BevelBorder.RAISED)));

fOk = new JButton("Ok");

fOk.addActionListener(this);

fCancel = new JButton("Cancel");

fCancel.addActionListener(this);

fApply = new JButton("Apply");

fApply.addActionListener(this);

bottom.add(fOk);

bottom.add(Box.createGlue());

bottom.add(fCancel);

bottom.add(Box.createGlue());

bottom.add(fApply);

Box centre = Box.createHorizontalBox();

centre.add(left);

getContentPane().add(top, BorderLayout.NORTH);

getContentPane().add(centre, BorderLayout.CENTER);

getContentPane().add(bottom, BorderLayout.SOUTH);

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//Define objects and variables

private JRadioButton red;

private JRadioButton blue;

private JRadioButton green;

private JRadioButton yellow;

private JCheckBox fill;

private JComboBox shapeList;

private JButton fOk;

private JButton fCancel;

private JButton fApply;

private int i;

private int colour;

private boolean isFilled;

private String array;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

private void setArrays(int colour, boolean isFilled){

setShapeArray(colour);

setFillArray(isFilled);

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

private String[][] setShapeArray(int colour){

//sets the colour

switch(colour){

case BounceConstants.RED:

Bounce.shape[i][1]="red";

break;

case BounceConstants.BLUE:

Bounce.shape[i][1]="blue";

break;

case BounceConstants.GREEN:

Bounce.shape[i][1]="green";

break;

case BounceConstants.YELLOW:

Bounce.shape[i][1]="yellow";

break;

}

return Bounce.shape;

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

private boolean[] setFillArray(boolean isFilled){

Bounce.colourFill[i] = isFilled;

return Bounce.colourFill;

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

private void getArrayIndex(String array)

{

int s;

String colour, shape;

s = array.indexOf(",");

colour=array.substring(0,s);

shape=array.substring(s+2);

getIndex(colour, shape);

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

private void getIndex(String colour, String shape)

{

for(i=0; i><5; i++)

{

if((Bounce.shape[i][0].equals(shape))&(Bounce.shape[i][1].equa