Thank you, that did the trick...
Java Greenhorn needs help,
I created this based on some other code I found when I searched for TwoButtons in some of the forums.
It compiles OK and does not throw any exceptions a run time, but does not draw a gradient circle either.
Line 21 & 24 is not working, I'm obviously missing something. Can some one please give me some guidence, please?
other than my issue I like this code better than the Head First Java book example with inner classes...
------ TwoButtons.java -------
1 import java.awt.*;
2 import java.awt.event.*;
3 import javax.swing.*;
4 public class TwoButtons extends JFrame implements ActionListener
5 {
6 JButton colorButton ;
7 JButton labelButton ;
8 JLabel label;
9
String x = "flip";
10
11 // constructor for TwoButtons
12 public TwoButtons()
13 {
14 colorButton = new JButton("Change Circle Color");
15 labelButton = new JButton("Change Label Text");
16
17 colorButton.setActionCommand( "circle" ); // set the command
18 labelButton.setActionCommand( "label" ); // set the command
19 label = new JLabel("I'm a Label");
20 label.setPreferredSize(new Dimension(100, 70));
21 MyCircleG drawcircle = new MyCircleG();
22
23 getContentPane().setLayout( new FlowLayout() );
24 getContentPane().add( drawcircle );
25 getContentPane().add( labelButton );
26 getContentPane().add( label );
27 getContentPane().add( colorButton );
28 // register the buttonDemo frame
29 // as the listener for both Buttons.
30 colorButton.addActionListener( this );
31 labelButton.addActionListener( this );
32 //getContentPane().setBackground( Color.yellow );
33 }
34 public void actionPerformed( ActionEvent evt)
35 {
36 // check which command has been sent
37 if ( evt.getActionCommand().equals( "circle" ) ) {
38 // getContentPane().setBackground( Color.red );
39 repaint(); }
40 if ( evt.getActionCommand().equals( "label" ) ) {
41 if (x == "flip") {
42 label.setText("Label Changed!");
43 x = "flop";
44 }else {
45 x = "flip";
46 label.setText("I'm a Label");
47 } }
48 }
49 public static void main ( String[] args )
50 {
51 TwoButtons demo = new TwoButtons() ;
52 WindowQuitter wquit = new WindowQuitter();
53 demo.addWindowListener( wquit );
54 demo.setSize( 500, 500 );
55 demo.setVisible( true );
56 }
57 }
58 class WindowQuitter extends WindowAdapter
59 {
60 public void windowClosing( WindowEvent e )
61 {
62 System.exit( 0 );
63 }
64 }
-------- MyCircleG.java ------
1 import java.awt.*;
2 import javax.swing.*;
3
4 class MyCircleG extends JPanel {
5
6 public void paintComponent(Graphics g) {
7
8 Graphics2D g2d = (Graphics2D) g;
9
10 int red = (int) (Math.random() * 256);
11 int green = (int) (Math.random() * 256);
12 int blue = (int) (Math.random() * 256);
13 Color startColor = new Color(red, green, blue);
14
15 red = (int) (Math.random() * 256);
16 green = (int) (Math.random() * 256);
17 blue = (int) (Math.random() * 256);
18 Color endColor = new Color(red, green, blue);
19
20 GradientPaint gradient= new GradientPaint(70,70,startColor, 150, 150, endColor);
21 g2d.setPaint(gradient);
22 g2d.fillOval(70,70,100,100);
23
24 } // End method
25
26 } // End class