Reputation: 43
I have to make an paint applet that launches a color chooser in a second frame. So when the red button is clicked, the user paints in the color red.
In the actual paint app, I can't seem to call the getCurrentColor method from colorchoose.
https://i.sstatic.net/zLj1G.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;
}
}
PAINTING 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;
}
}
Upvotes: 0
Views: 209
Reputation: 12332
getCurrentColor1()
is defined in your colorchoose
class. Currently, you are calling it without a reference to your colorchoose
object. You probably mean to do this:
g.setColor(tbw.getCurrentColor1());
P.S. The Java convention for class naming is "camel case". A better name for your colorchoose
class would be ColorChoose
or ColorChooser
.
Upvotes: 1