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

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

http://i.imgur.com/awvLiMB.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;
    }
}


绘画APP

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

09-11 20:57