This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Getting the TwoButtons to add the gradient circle to the panel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Getting the TwoButtons to add the gradient circle to the panel" Watch "Getting the TwoButtons to add the gradient circle to the panel" New topic
Author

Getting the TwoButtons to add the gradient circle to the panel

Mark Owen Kelly
Greenhorn

Joined: Apr 06, 2012
Posts: 3

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


Java Newbie
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
MyCircleG is a JPanel with no components,
therefore its preferred size is 0,0

include a constructor in the class and add this line
setPreferredSize(new Dimension(400,300));

where 400,300 is anything you want
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38025
    
  22
Welcome to the Ranch

I shall move you to the GUIs forum, where we usually discuss such questions.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Getting the TwoButtons to add the gradient circle to the panel
 
Similar Threads
Simple Gui Question (Head First Java)
HFJ TwoButtons problems
Beginners question (Head First - TwoButtons)
Head First Java Question - GUI program TwoButtons
TwoButtons App From "HeadFirst Java" Book