java - 带有颜色选择器的绘画应用程序

标签 java swing applet paint

我必须制作一个油漆小程序,在第二帧中启动颜色选择器。因此,当单击红色按钮时,用户将绘制红色。

在实际的绘画应用程序中,我似乎无法从 colorchoose 调用 getCurrentColor 方法。

/image/zLj1G.png

颜色选择器

public class colorchoose extends JFrame {

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

    public static void main(String [] args) {
        colorchoose frame = new colorchoose();
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Colors");
        frame.setVisible(true);
    }

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

        JRadioButton red = new JRadioButton("red");
        JRadioButton black = new JRadioButton("black");
        JRadioButton magenta = new JRadioButton("magenta");
        JRadioButton blue = new JRadioButton("blue");
        JRadioButton green = new JRadioButton("green");
        JRadioButton yellow = new JRadioButton("yellow");

        red.addActionListener(new MyActionListener());
        black.addActionListener(new MyActionListener());
        magenta.addActionListener(new MyActionListener());
        blue.addActionListener(new MyActionListener());
        green.addActionListener(new MyActionListener());
        yellow.addActionListener(new MyActionListener());

        jpRadioButtons.add(red);
        jpRadioButtons.add(black);
        jpRadioButtons.add(magenta);
        jpRadioButtons.add(blue);
        jpRadioButtons.add(green);
        jpRadioButtons.add(yellow);

        colors.put(red, Color.RED);
        colors.put(black, Color.BLACK);
        colors.put(magenta, Color.MAGENTA);
        colors.put(yellow, Color.YELLOW);
        colors.put(green, Color.GREEN);
        colors.put(blue, Color.BLUE);

        add(jpRadioButtons, BorderLayout.WEST);

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

    Color getCurrentColor1() {
        return currentColor;
    }

    private class MyActionListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            currentColor = colors.get(e.getSource());
        }
    }

    public boolean action(Event evtObj, Object arg) {
        if (evtObj.target instanceof Checkbox) {
            repaint();
            return true;
        }
        return false;
    }
}

绘画应用程序

public class Frame extends Applet {

    private int xValue = -10, yValue = -10;
    colorchoose tbw = new colorchoose();

    public void init() {
        tbw.setVisible(true);
    }

    public void paint(Graphics g) {
        g.setColor(getCurrentColor1());
        g.drawString("Drag the mouse to draw", 10, 20);
        g.fillOval(xValue, yValue, 4, 4);
    }

    public void update(Graphics g) {
        paint(g);
    }

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

最佳答案

getCurrentColor1() 在您的 colorchoose 类中定义。目前,您在调用它时没有引用 colorchoose 对象。您可能想这样做:

    g.setColor(tbw.getCurrentColor1());

附注Java 类命名约定是“驼峰命名法”。 colorchoose 类的更好名称是 ColorChooseColorChooser

关于java - 带有颜色选择器的绘画应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23704024/

相关文章:

java - 使用 gradle 运行时 Jackson 子类型反序列化失败

c# - 从 C# 转换后 Java 代码中的错误

java - 如何使用 Java 显示我的数据库?

java - 如何获取从 JComboBox 中选择的项目的索引?

java - TableModel,导致 Java JFrame 中出现重复的数据库信息

Java Applet 获取 JAR 之外的文件

OS X Mountain Lion 上带有自签名证书的 Java 小程序

java - 如何以随机方式用较小的数组填充数组?

java - 如何在机器人框架中使用 "Choose File"关键字上传文件

java - 小程序EDT中运行I/O的问题