0

I need to make a java applet that allows the user to paint with the mouse. When the applet is launched, a second window opens that allows the user to select one of 6 different colors to paint with.

First, I wrote code to construct a toolbar window which contains a getcurrentcolor method. I can't seem to link the button press with the color change.

If I launch the applet, the toolbarwindow opens successfully and I'm able to paint in black, so my only problem is selecting a color on the toolbar window and painting in that color.

toolbar code:https://gist.github.com/anonymous/3c053c69112f46d17440

painting applet code: https://gist.github.com/anonymous/aca7929dbcfc08008f30

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
user3598181
  • 43
  • 1
  • 5
  • Did you try a [JColorChooser](http://docs.oracle.com/javase/tutorial/uiswing/components/colorchooser.html)? – user1803551 May 03 '14 at 03:38
  • My professor said we couldn't use JColorChooser – user3598181 May 03 '14 at 04:50
  • Anything else you can't use? – user1803551 May 03 '14 at 05:00
  • Here is the description: "Modify the program above to incorporate colors. In a separate window, provide a “toobar” of RadioButton objects that lists the following six colors: red, black, magenta, blue, green and yellow. The toolbar should be implemented as a subclass of Frame called ToolBarWindow and should consist of six buttons, each with the appropriate color name. When a new color is selected, drawing should occur in the new color." – user3598181 May 03 '14 at 05:37
  • Why do you have the `action` method? – user1803551 May 03 '14 at 05:56
  • Whoever set this homework is a moron. 1) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. 3) An applet should not show frames, but (probably modal) dialogs. 4) There is typically no need to extend top-level containers. – Andrew Thompson May 03 '14 at 06:54

2 Answers2

0

I get the feeling your professor wants you to make your own. Here is an example I threw together in 10 minutes or so. its very rough and lacking good coding style but if you need to know what is involved, it should be useful. The example code prints out the color whenever you have selected it.

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;


public class MyColorChooser extends Component
{
    private Color[] colors;
    private Color selectedColor;
    public MyColorChooser(Color ... colors)
    {
        this.colors = colors;
        this.selectedColor = colors[0];
        this.addMouseListener
        (
            new MouseAdapter()
            {
                @Override public void mouseClicked(MouseEvent e)
                {
                    int tileWidth = MyColorChooser.this.getWidth();
                    tileWidth /=  MyColorChooser.this.colors.length;
                    int index = e.getX()/(tileWidth);
                    MyColorChooser.this.selectedColor = MyColorChooser.this.colors[index];
                }
            }
        );
    }

    @Override public void paint(Graphics renderer)
    {
        int width = this.getWidth()/this.colors.length;
        int height = this.getHeight();

        for(int i = 0; i < this.colors.length; i++)
        {
            renderer.setColor(this.colors[i]);
            renderer.fillRect(width*i,0,width,height);
        }
    }

    public Color getSelectedColor()
    {
        return this.selectedColor;
    }

    public static void main(String ... args) throws Throwable
    {
        JFrame f = new JFrame();
        f.setSize(200,100);
        f.getContentPane().setLayout(null);
        MyColorChooser chooser = new MyColorChooser
        (
            Color.RED,
            Color.GREEN,
            Color.BLUE,
            Color.YELLOW,
            Color.WHITE,
            Color.BLACK
        );
        f.getContentPane().add(chooser);
        chooser.setBounds(0,0,200,50);
        f.setVisible(true);

        Color lastColor = chooser.getSelectedColor();
        for(;;)
        {
            if(!chooser.getSelectedColor().equals(lastColor))
            {
                lastColor = chooser.getSelectedColor();
                System.out.printf("Selected Color:%s\n",lastColor.toString());
            }
            Thread.sleep(100);
        }
    }

}
0

I gave an approach here for 3 buttons, you can add the rest. The idea is that you keep a currently selected color field and update it each time a button is selected via the ActionListener. A map from the button to the color it represents is not necessary, but makes the code more manageable.

public class ToolBarWindow extends JFrame {

    private static Map<JRadioButton, Color> colors = new HashMap<>();
    private static Color currentColor = Color.BLACK;

    public static void main(String[] args) {

        ToolBarWindow frame = new ToolBarWindow();
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Colors");
        frame.setVisible(true);
    }

    public ToolBarWindow() {

        JPanel jpRadioButtons = new JPanel();
        jpRadioButtons.setLayout(new GridLayout(3, 1));

        // put the other colors
        JRadioButton red = new JRadioButton("red");
        JRadioButton black = new JRadioButton("black");
        JRadioButton magenta = new JRadioButton("magenta");

        red.addActionListener(new MyActionListener());
        black.addActionListener(new MyActionListener());
        magenta.addActionListener(new MyActionListener());

        jpRadioButtons.add(red);
        jpRadioButtons.add(black);
        jpRadioButtons.add(magenta);

        colors.put(red, Color.RED);
        colors.put(black, Color.BLACK);
        colors.put(magenta, Color.MAGENTA);

        add(jpRadioButtons, BorderLayout.WEST);

        ButtonGroup bg = new ButtonGroup();
        bg.add(red);
        bg.add(black);
        bg.add(magenta);
    }

    Color getCurrentColor() {

        return currentColor;
    }

    private class MyActionListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            currentColor = colors.get(e.getSource());
        }
    }
}
user1803551
  • 12,965
  • 5
  • 47
  • 74