Results 1 to 6 of 6
  1. #1
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default Panel not appearing in JFrame

    I made a panel consisting of a gradient colored ball:
    Java Code:
    public class _2D_Ball extends JPanel{
    	protected Color litColor = Color.WHITE;
    	protected Color shadedColor = Color.GRAY;
    	protected Color backgroundColor = Color.WHITE;
    	public void paintComponent(Graphics stylus)
    	{
    		
    		this.setBackground(backgroundColor);
    		
    		Graphics2D stylus2d = (Graphics2D) stylus;
    		super.paintComponents(stylus2d);
    		GradientPaint gradientColor = new GradientPaint(70,70,litColor,150,150,shadedColor);
    		stylus2d.setPaint(gradientColor);
    		stylus2d.fillOval(70, 70, 100, 100);
    	}
    
    }
    the panel class was used in this JFrame class
    Java Code:
    public class GradientBallPanel extends JFrame {
    	_2D_Ball gradientBallPanel;
    	JButton litColorChooser;
    	JButton shadedColorChooser;
    	public GradientBallPanel()
    	{
    		super("Ball");
    		setLayout(new FlowLayout());
    		
    		gradientBallPanel = new _2D_Ball();
    		gradientBallPanel.setSize(400, 400);
    		add(gradientBallPanel);
    
    //rest of the code is irrelevant
    this is the main function:
    Java Code:
    public class GradientBallPanel_MainClass extends JFrame {
    	public static void main(String args[])
    	{
    		GradientBallPanel GradientBallPanelApplication = new GradientBallPanel();
    		GradientBallPanelApplication.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		GradientBallPanelApplication.setVisible(true);
    		GradientBallPanelApplication.setSize(500,500);
    	}
    }
    The problem is that when I run the application, the _2D_Ball panel doesn't appear and i don't quite understand why? The debugger doesn't find anything wrong.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You should set the preferred size of the JPanel and call pack() on the JFrame after adding all components to it but before calling setVisible(true).

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,247
    Rep Power
    19

    Default

    Also, construction of a GradientPaint is computationally intensive. Your GradientPaint doesn't change in the course of painting, so it is better to declare it as a field and initialize it on declaration or in a constructor of the class. If and when it changes, you can reassign the field as soon as the changes are made -- not every time the panel is repainted.

    db

  4. #4
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    @Fubarable: Thanks I'll try that
    @Derryl : I'm not quite clear on what you said. Could you please illustrate with an example. Thanks.

  5. #5
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    @Furable: I tried but the panel still isn't visible.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,247
    Rep Power
    19

    Default

    You're constructing a new GradientPaint every time the panel is repainted. The only variables in its constructor are the colors. Declare and initialize the GradientPaint as an instance field and reassign it any time either of the colors change, in the ActionListeners of litColorChooser and shadedColorChooser.

    db

Similar Threads

  1. display media control panel in jframe.
    By Saran185 in forum AWT / Swing
    Replies: 0
    Last Post: 03-12-2011, 08:56 AM
  2. How to add panel to JFrame?
    By geekchick in forum New To Java
    Replies: 17
    Last Post: 08-01-2010, 05:23 PM
  3. JDialog appearing in the toolbar
    By riddick in forum New To Java
    Replies: 6
    Last Post: 11-06-2009, 02:19 PM
  4. jfilechooser not appearing
    By letter5 in forum New To Java
    Replies: 5
    Last Post: 02-15-2009, 04:45 PM
  5. Log messages are not appearing in my log file
    By indu_raj in forum Java Servlet
    Replies: 1
    Last Post: 08-08-2007, 11:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •