I spent 5 hours looking for solution to my question, but I really cannot. I have only learnt java swing for 2 days to do this assignment. ( my professor didn't teach what is needed to finish assignment and only talked about swing for less then 30 mins) I also tried to read Swing documentation, but I have no clue how to read documentation. Because getting into the read question I want to ask 2 irrelevant question.
- How to search for method() like repaint() method which I cannot find in the documentation, but I found it on other website.
- When learning new language, How would I start learning it from reading documentation? Do I read the tutorial or there are better way?
Questions above are raised because, in this situation, I found myself in loops of confusion, and I just do not know where to start/learn and how.
Anyway, Lets get to the point.
What make this question so confusing to me are all the conditions and requirements that need to be met.
Goal: to have 3 buttons with color's name on it that when is clicked on the color of circle icon changed.
Requirements(confusing part to me)
button label must be color's name in this case red/green/blue
displays a simple icon with a circle initially colored solid red
the circle color must change to that indicated by the button's label.
when a button is clicked you need to change the icon's color, then call the label's
repaint()
method. This, in turn will call the icon'spaintIcon()
[Jlabel
calls paintIcon for us]the buttons' action listener objects must be of anonymous classes.
button must create in
main
() and set up in loop.for (i=0; i<3; i++) { btn[i] = createButton(i, ......); }
The
createButton(
) function you will write gets an index (0 to 2) and creates the green, blue, and red buttons, depending on the valus of i.JButton
objects are returned with the proper listener attached.
Hint: Use a Color array that can be indexed by i:
Color[] colors = new String[]{"RED", "GREEN", "BLUE"};
Then, create the color with the required color:
Below are my current code:
The problems I have are:
how to label create color object that contain string. ( required by professor, but his code does not even work)
How do I use repaint() in ActionPerform to send "signal" to paintIcon, so that when I click a button. Circle shape changes color.
public class Button_hw implements Icon { private int size; private Color color; public Button_hw(int aSize) { size = aSize; } // @Override public void paintIcon(Component c, Graphics g, int x, int y) { Graphics2D g2 = (Graphics2D) g; Ellipse2D.Double planet = new Ellipse2D.Double(x,y,size,size); g2.setColor(color); g2.fill(planet); } @Override public int getIconWidth() { return size; } @Override public int getIconHeight() { return size; } public static void main(String[] args) { JFrame frame = new JFrame(); final Color[] colors; colors = new Color[3]; colors[0]= Color.GREEN; colors[1]= Color.RED; colors[2]= Color.BLUE; Button_hw Circle = new Button_hw(50); final JLabel label = new JLabel(Circle); final int FIELD_WIDTH = 20; JButton btn[]; btn = new JButton[3]; for (int i=0; i<3; i++) { btn[i] = new JButton(String.valueOf(colors[i])); } JTextField textField = new JTextField(FIELD_WIDTH); textField.setText("Click a button!"); btn[0].addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event) { textField.setText("green"); label.repaint(); } }); btn[1].addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event) { textField.setText("red"); label.setColor(Color.RED); label.repaint(); } }); btn[2].addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event) { textField.setText("blue"); label.repaint(); } }); frame.setLayout(new FlowLayout()); frame.add(label); frame.add(btn[0]); frame.add(btn[1]); frame.add(btn[2]); frame.add(textField); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } }