简体   繁体   中英

java paint applet - need help adding action on radio buttons

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

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

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM