【问题标题】:java paint applet - need help adding action on radio buttonsjava paint applet - 需要帮助在单选按钮上添加操作
【发布时间】:2014-05-03 14:17:20
【问题描述】:

我需要制作一个允许用户用鼠标绘画的 Java 小程序。启动小程序时,会打开第二个窗口,允许用户从 6 种不同颜色中选择一种进行绘画。

首先,我编写了代码来构建一个包含getcurrentcolor 方法的工具栏窗口。我似乎无法将按钮按下与颜色变化联系起来。

如果我启动小程序,toolbarwindow 会成功打开并且我可以绘制黑色,所以我唯一的问题是在工具栏窗口上选择一种颜色并使用该颜色进行绘制。

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

绘画小程序代码:https://gist.github.com/anonymous/aca7929dbcfc08008f30

【问题讨论】:

  • 你试过JColorChooser吗?
  • 我的教授说我们不能使用 JColorChooser
  • 还有什么不能用的吗?
  • 这里是描述:“修改上面的程序以合并颜色。在一个单独的窗口中,提供一个 RadioButton 对象的“工具栏”,其中列出了以下六种颜色:红色、黑色、洋红色、蓝色、绿色和黄色。工具栏应实现为名为 ToolBarWindow 的 Frame 的子类,并应由六个按钮组成,每个按钮都有适当的颜色名称。选择新颜色时,应以新颜色进行绘制。"
  • 你为什么有action方法?

标签: java applet radio-button


【解决方案1】:

我在这里为 3 个按钮提供了一种方法,您可以添加其余的。这个想法是您保留当前选择的颜色字段,并在每次通过ActionListener 选择按钮时更新它。从按钮到它所代表的颜色之间的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());
        }
    }
}

【讨论】:

    【解决方案2】:

    我感觉你的教授希望你自己做。这是我在 10 分钟左右拼凑起来的一个例子。它非常粗糙且缺乏良好的编码风格,但如果您需要了解其中涉及的内容,它应该很有用。示例代码会在您选择颜色时打印出颜色。

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

    【讨论】:

      猜你喜欢
      • 2013-02-24
      • 2011-09-02
      • 1970-01-01
      • 2010-10-23
      • 2010-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多