简体   繁体   English

Java Paint Applet-需要帮助在单选按钮上添加操作

[英]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. 我需要制作一个Java小程序,允许用户用鼠标绘画。 When the applet is launched, a second window opens that allows the user to select one of 6 different colors to paint with. 启动小程序时,将打开第二个窗口,允许用户选择6种不同的颜色之一进行绘制。

First, I wrote code to construct a toolbar window which contains a getcurrentcolor method. 首先,我编写了代码来构建一个包含getcurrentcolor方法的工具栏窗口。 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. 如果我启动的应用程序内, toolbarwindow成功地打开,我能够在黑色的油漆,所以我唯一的问题是选择在颜色栏窗口和画上的颜色。

toolbar code: https://gist.github.com/anonymous/3c053c69112f46d17440 工具栏代码: https//gist.github.com/anonymous/3c053c69112f46d17440

painting applet code: https://gist.github.com/anonymous/aca7929dbcfc08008f30 绘画小程序代码: 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. 我在这里给出了3个按钮的方法,您可以添加其余的按钮。 The idea is that you keep a currently selected color field and update it each time a button is selected via the ActionListener . 这样的想法是,您保留当前选定的颜色字段,并在每次通过ActionListener选择按钮时对其进行更新。 A map from the button to the color it represents is not necessary, but makes the code more manageable. 从按钮到它代表的颜色的map不是必需的,但是可以使代码更易于管理。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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