绘画应用程序与颜色选择器

时间:2014-05-16 20:26:56

标签: java swing applet paint

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

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

http://i.imgur.com/awvLiMB.png

COLOR CHOOSER

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

1 个答案:

答案 0 :(得分:1)

getCurrentColor1()在您的colorchoose课程中定义。目前,您在没有引用colorchoose对象的情况下调用它。你可能意味着这样做:

    g.setColor(tbw.getCurrentColor1());

P.S。类命名的Java约定是“驼峰式”。 colorchoose课程的更好名称是ColorChooseColorChooser