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);}
    }
}

Recommended Answers

All 3 Replies

Ok, basically all you need to is give your ToolbarWindow a reference to the Painter applet so it has a way to call back on it. The toolbar frame has absolutely no need for a reference to the applets graphics context. Here's your code modified just a bit with a clear() method added to the applet and the toolbar now taking a reference to the applet in it's constructor. I've bolded certain changes so they stand out:

import java.applet.Applet;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Painter extends Applet {

    Color a = new Color(255, 100, 100);
    int xValue = -10, yValue = -10;
    ToolBarWindow TBW;

    public void init() {
        [B]// pass ref to ToolBarWindow
        TBW = new ToolBarWindow(this);[/B]
        TBW.setVisible(true);
    }

    public void stop() {
        TBW.setVisible(false);
    }

    public void start() {
        TBW.setVisible(true);
    }

    public void paint(Graphics 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);
    }

    [B]// added method to clear the applet
    public void clear() {
        Graphics g = getGraphics();
        g.clearRect(0, 0, getWidth(), getHeight());
        g.drawString("Drag the mouse to draw", 10, 20);
        g.dispose();
    }[/B]

    public boolean mouseDrag(Event evtObj, int x, int y) {
        xValue = x;
        yValue = y;
        repaint();
        return true;
    }
}

class ToolBarWindow extends Frame {

    CheckboxGroup cbg;
    Checkbox Red;
    Checkbox Black;
    Checkbox Green;
    Checkbox Blue;
    Checkbox Yellow;
    Checkbox Magenta;
    Button Clear;
    
    [B]Painter painter;[/B]
    
    //Set up for tool Bar checkboxes and clear button
    [B]// Note that ToolBarWindow now has a reference to call the applet
    public ToolBarWindow(Painter painter) {
        super();
        this.painter = painter;[/B]

        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 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")) {
                [B]painter.clear();[/B]
                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);
        }
    }
}

You're also using deprecated methods to handle the events and making the layout much more difficult than it needs to be, but I'm guessing you are working from an outdated book or reference.

Thanks you so much..

The only depricated code I am showing is handleEvent in my Frame. I haven't been able to find the replacement yet but I'm working on that..

Again thanks, that one had me stumped...

Check the API on handleEvent(). It specifies the replacement. It's basically been out of date since JDK 1.1.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.