I'm setting up a simple painter applett that uses a seperate frame class to control color and clear the screen. I've got all but the clear button working fine. After attempting several different methods I've settled on passing the address of the applett graphics object to the frame class and calling super.update when the clear button is pushed. Unfortunatley it does not seem to have any effect. I was hoping someone might take a look at my code and tell me where I went wrong.
Thanks in advance
Allan
package myframe;
/**
* Title: MyFrame </p>
*
*/
import java.applet.Applet;
import java.awt.*;
public class Painter extends Applet
{ Color a = new Color(255,100,100);
int xValue = -10, yValue = -10;
ToolBarWindow TBW;
public void init()
{
TBW=new ToolBarWindow();
TBW.setVisible(true);
}
public void stop()
{
TBW.setVisible(false);
}
public void start()
{
TBW.setVisible(true);
}
public void paint(Graphics g)
{
TBW.GetGraph(g);
g.drawString("Drag the mouse to draw", 10, 20);
g.setColor(TBW.GetCurrentColor());
g.fillOval(xValue, yValue, 4, 4);
}
//Override Component class update method to allow all ovals
// to remain on the screen by not clearing the background
public void update(Graphics g)
{
paint(g);
}
public boolean mouseDrag(Event evtObj, int x, int y)
{
xValue = x;
yValue = y;
repaint();
return true;
}
}
package myframe;
/**
* Title: MyFrame </p>
*
* version: 1.0
*/
import java.awt.*;
import java.awt.event.*;
public class ToolBarWindow extends Frame
{
CheckboxGroup cbg;
Checkbox Red;
Checkbox Black;
Checkbox Green;
Checkbox Blue;
Checkbox Yellow;
Checkbox Magenta;
Button Clear;
Graphics Pgraphic;
//Set up for tool Bar checkboxes and clear button
public ToolBarWindow()
{
ToolBarWindowLayout customLayout = new ToolBarWindowLayout();
setFont(new Font("Helvetica", Font.PLAIN, 12));
setLayout(customLayout);
setBackground (Color.LIGHT_GRAY);
cbg = new CheckboxGroup();
Red = new Checkbox("Red", cbg, false);
add(Red);
Black = new Checkbox("Black", cbg, true);
add(Black);
Green = new Checkbox("Green", cbg, false);
add(Green);
Blue = new Checkbox("Blue", cbg, false);
add(Blue);
Yellow = new Checkbox("Yellow", cbg, false);
add(Yellow);
Magenta = new Checkbox("Magenta", cbg, false);
add(Magenta);
Clear = new Button("Clear");
add(Clear);
setSize(getPreferredSize());
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String args[])
{
ToolBarWindow window = new ToolBarWindow();
window.setTitle("ToolBarWindow");
window.pack();
window.setVisible(true);
window.setBackground(Color.LIGHT_GRAY);
}
// Gets address for Graphics object in Painter applett
public void GetGraph(Graphics a)
{
Pgraphic = a;
}
public boolean handleEvent(Event evtObj)
{
if(evtObj.id==Event.WINDOW_DESTROY)
{
setVisible(false);
return true;
}
return super.handleEvent(evtObj);
}
//Handles clear by doing super.update on Graphics object passed
//from Painter applett
public boolean action(Event evtObj, Object arg) {
if (evtObj.target instanceof Button) {
if (arg.equals("Clear"))
{
super.update(Pgraphic);
return true;
}
}return false;
}
//determines color for applett
public Color GetCurrentColor()
{
Color c = new Color(255,100,100);
if (cbg.getSelectedCheckbox()==Red)
c=Color.red;
else
if (cbg.getSelectedCheckbox()==Black)
c=Color.black;
else
if (cbg.getSelectedCheckbox()==Magenta)
c=Color.magenta;
else
if (cbg.getSelectedCheckbox()==Blue)
c=Color.blue;
else
if (cbg.getSelectedCheckbox()==Green)
c=Color.green;
else
if (cbg.getSelectedCheckbox()==Yellow)
c=Color.yellow;
return (c);
}
}
class ToolBarWindowLayout implements LayoutManager {
public ToolBarWindowLayout() {
}
public void addLayoutComponent(String name, Component comp) {
}
public void removeLayoutComponent(Component comp) {
}
public Dimension preferredLayoutSize(Container parent) {
Dimension dim = new Dimension(0, 0);
Insets insets = parent.getInsets();
dim.width = 730 + insets.left + insets.right;
dim.height = 137 + insets.top + insets.bottom;
return dim;
}
public Dimension minimumLayoutSize(Container parent) {
Dimension dim = new Dimension(0, 0);
return dim;
}
public void layoutContainer(Container parent) {
Insets insets = parent.getInsets();
Component c;
c = parent.getComponent(0);
if (c.isVisible()) {c.setBounds(insets.left+24,insets.top+40,72,24);}
c = parent.getComponent(1);
if (c.isVisible()) {c.setBounds(insets.left+120,insets.top+40,72,24);}
c = parent.getComponent(2);
if (c.isVisible()) {c.setBounds(insets.left+408,insets.top+40,72,24);}
c = parent.getComponent(3);
if (c.isVisible()) {c.setBounds(insets.left+312,insets.top+40,72,24);}
c = parent.getComponent(4);
if (c.isVisible()) {c.setBounds(insets.left+504,insets.top+40,72,24);}
c = parent.getComponent(5);
if (c.isVisible()) {c.setBounds(insets.left+216,insets.top+40,72,24);}
c = parent.getComponent(6);
if (c.isVisible()) {c.setBounds(insets.left+616,insets.top+40,72,24);}
}
}