JPanel problem

Hi i have written this applet which has 3 JPanels.

colorPanel is at the top and has 4 coloured rectangles, red,green,black and blue representing a color pallet

shapePanel is on the left and has 2 shapes( an oval and a rect)

canvas is to hold my drawing.

The idea is to select a color and a shape and draw that on to my canvas. When a color is selected the black rectangle will change to that color. I have set up individual mouse listeners for each panel but just cant seem to get them interacting with each other.

I know there are probably simpler ways of doing this but its for my course.

Any ideas will be appreciated. Thanks

import java.awt.*;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import javax.swing.JApplet;

import java.awt.BorderLayout;

import java.awt.event.MouseMotionAdapter;

publicclass ColourChooserextends JApplet

{

int startX,startY,height,width;

boolean red, blue, green;

boolean black=true;

boolean rect=true;

boolean oval;

BorderLayout borderLayout1=new BorderLayout();

publicvoid init(){

this.setLayout(borderLayout1);

colorPanel.setBackground(Color.GRAY);

colorPanel.setPreferredSize(new Dimension(50,50));

colorPanel.addMouseListener(new ColourChooser_colorPanel_mouseAdapter(this));

shapePanel.setBackground(Color.GRAY);

shapePanel.setPreferredSize(new Dimension(50,50));

shapePanel.addMouseListener(new ColourChooser_shapePanel_mouseAdapter(this));

canvas.setBackground(Color.WHITE);

canvas.addMouseMotionListener(new

ColourChooser_canvas_mouseMotionAdapter(this));

canvas.addMouseListener(new ColourChooser_canvas_mouseAdapter(this));

this.add(colorPanel, java.awt.BorderLayout.NORTH);

this.add(shapePanel, java.awt.BorderLayout.WEST);

this.add(canvas, java.awt.BorderLayout.CENTER);

}

Panel colorPanel=new Panel(){

publicvoid paint(Graphics g)

{

g.setColor(Color.RED);

g.fillRect(160,10,20,20);

g.setColor(Color.GREEN);

g.fillRect(185,10,20,20);

g.setColor(Color.BLUE);

g.fillRect(210,10,20,20);

g.setColor(Color.BLACK);

g.fillRect(135,10,20,20);

colorChanger(g);

g.fillRect(135,10,20,20);

}

} ;

Panel shapePanel=new Panel(){

publicvoid paint(Graphics g){

g.drawRect(10,40,20,20);

g.drawOval(10,80,20,20);

}

};

Panel canvas=new Panel(){

publicvoid paint(Graphics g){

if (rect){

if (blue){

g.setColor(Color.BLUE);

g.fillRect(startX,startY,height,width);

}elseif (red){

g.setColor(Color.RED);

g.fillRect(startX,startY,height,width);

}elseif (green){

g.setColor(Color.GREEN);

g.fillRect(startX,startY,height,width);

}else{

g.setColor(Color.BLACK);

g.fillRect(startX,startY,height,width);

}

}elseif (oval)

{

if(blue){

g.setColor(Color.BLUE);

g.fillOval(startX,startY,height,width);

}elseif (red){

g.setColor(Color.RED);

g.fillOval(startX,startY,height,width);

}elseif (green){

g.setColor(Color.GREEN);

g.fillOval(startX,startY,height,width);

}else{

g.setColor(Color.BLACK);

g.fillOval(startX,startY,height,width);

}

}

}

};/// i thought i would use colorChanger to change the color on the color pallet

publicvoid colorChanger(Graphics g){

if (red){

g.setColor(Color.RED);

}elseif (green){

g.setColor(Color.GREEN);

}elseif (blue){

g.setColor(Color.BLUE);

}else{

g.setColor(Color.BLACK);

}

}

publicvoid shapePanel_mousePressed(MouseEvent e){

int x = e.getX();

int y = e.getY();

if(x<30){

if (y<60){

rect=true;

}elseif (y>80&y<100){

oval =true;

}

}

}

publicvoid colorPanel_mousePressed(MouseEvent e){

int x=getX();

int y=getY();

if (y<30){

if (x<205&x>185){

green=true;

}elseif (x<180&x>160){

red=true;

}elseif(x<230&x>210){

blue=true;

}else{

black=true;

}

}

}

publicvoid canvas_mouseDragged(MouseEvent e){

height = e.getX();

width = e.getY();

repaint();

}

publicvoid canvas_mousePressed(MouseEvent e){

startX=e.getX();

startY=e.getY();

}

}

class ColourChooser_shapePanel_mouseAdapterextends MouseAdapter{

private ColourChooser adaptee;

ColourChooser_shapePanel_mouseAdapter(ColourChooser adaptee){

this.adaptee = adaptee;

}

publicvoid mousePressed(MouseEvent e){

adaptee.shapePanel_mousePressed(e);

}

}

class ColourChooser_colorPanel_mouseAdapterextends MouseAdapter{

private ColourChooser adaptee;

ColourChooser_colorPanel_mouseAdapter(ColourChooser adaptee){

this.adaptee = adaptee;

}

publicvoid mousePressed(MouseEvent e){

adaptee.colorPanel_mousePressed(e);

}

}

class ColourChooser_canvas_mouseMotionAdapterextends MouseMotionAdapter{

private ColourChooser adaptee;

ColourChooser_canvas_mouseMotionAdapter(ColourChooser adaptee){

this.adaptee = adaptee;

}

publicvoid mouseDragged(MouseEvent e){

adaptee.canvas_mouseDragged(e);

}

}

class ColourChooser_canvas_mouseAdapterextends MouseAdapter{

private ColourChooser adaptee;

ColourChooser_canvas_mouseAdapter(ColourChooser adaptee){

this.adaptee = adaptee;

}

publicvoid mousePressed(MouseEvent e){

adaptee.canvas_mousePressed(e);

}

}

[12046 byte] By [TheRidgea] at [2007-11-26 17:33:43]
# 1
1) Use JPanel, not Panel in a Swing application.2) Override paintComponent(), not paint() for a Swing component3) Maybe this example will give you some ideas: http://forum.java.sun.com/thread.jspa?forumID=57&threadID=607073
camickra at 2007-7-9 0:01:44 > top of Java-index,Desktop,Core GUI APIs...
# 2
See also my suggestions on this thread, http://forum.java.sun.com/thread.jspa?threadID=5133382
itchyscratchya at 2007-7-9 0:01:44 > top of Java-index,Desktop,Core GUI APIs...