Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Drawing in a JPanel

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Drawing in a JPanel

    I want to make a drawing (A rectangle which is colored according RGB values that are scanned real-time), and add this drawing to a JPanel. This JPanel has to be added to the layout of the main frame. If I make a class which extends JPanel, and draw the rectangle in the paintComponent(Graphics g) method, the drawing isn't displayed.


  2. #2
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    47
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: Drawing in a JPanel

    With no code I don't really know where to advise you, either post progress or take a look at this.

    THIS

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Drawing in a JPanel

    Here is my code:

    import java.awt.Container;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.GridLayout;
    import java.awt.Insets;
     
    import javax.swing.*;
     
    public class Frame {
     
    	public static void main(String[] args) 
    	{
    		JFrame frame = new JFrame("gui");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(500, 500);
     
    		Container pane = frame.getContentPane();
    		ColorValuesPanel values = new ColorValuesPanel();
     
    		GridBagLayout gridBagLayout = new GridBagLayout();
    		pane.setLayout(gridBagLayout);
    		GridBagConstraints c = new GridBagConstraints();
     
    		c.gridx = 0;
    		c.gridy = 0;
    		c.insets = new Insets(0, 0, 0, 0);
    		c.anchor = GridBagConstraints.WEST;
    		pane.add(values, c);
     
    		frame.setVisible(true);
    	}
    }

    import java.awt.Color;
    import java.awt.Graphics;
     
    import javax.swing.JPanel;
     
    public class ColorValuesPanel extends JPanel
    {
    	Color c;
    	int red = 50, green = 50, blue = 50;
     
    	public ColorValuesPanel()
    	{
    		super.setSize(500, 500);
    	}
     
    	protected void paintComponent(Graphics g)
    	{	
    		c = new Color(red, green, blue);
    		g.setColor(c);
    		g.fillRect(350, 50, 50, 400);
    	}
    }

    My problem is that the drawing isn't displayed. Without the gridbagLayout it works. But I need the gridbagLayout because this is part of a bigger GUI. It's like the size of the JPanel is set to (0, 0) even if I set the size of the ColorValuesPanel to (500, 500).

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Drawing in a JPanel

    Please use code tags when posting code, assistance can be found on the Announcements page.
    "Frame" is an existing class in Java, so using the name is discouraged. Perhaps use something like MyFrame or ProjectNameFrame, or what ever, but something not already being used in Java
    The accepted answer here will explain why setSize is not working as expected.
    Calling pack on the jframe will cause the layout manager(s) to adjust things according to the respective min max and preferred sizes, if they have been set...
    This page, as part of a larger tutorial series, explains more details and includes sample code using GridBagLayout

  5. The Following User Says Thank You to jps For This Useful Post:

    Stone (August 7th, 2013)

  6. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Drawing in a JPanel

    Please wrap your code in the code tags to preserve formatting.

    A method to debug UI issues such as this is to add a border around the component, for instance in the constructor of the ColorValuesPanel
    setBorder(BorderFactory.createLineBorder(Color.RED));
    What this will do is add red border - this will at least let you see the size and placement of the component. From there you can alter the layouts, preferred size (hint, hint), call frame.pack() (hint, hint), etc... to see how they affect the size and placement of the Component.

  7. The Following User Says Thank You to copeg For This Useful Post:

    Stone (August 7th, 2013)

  8. #6
    Junior Member
    Join Date
    Aug 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Drawing in a JPanel

    Thank you, the setPreferredSize does work! I was using the setSize method at the JPanel but it didn't had any effect

Similar Threads

  1. Drawing in JPanel not working as expected
    By brocode in forum AWT / Java Swing
    Replies: 12
    Last Post: May 23rd, 2013, 12:44 PM
  2. JFrame, JPanel and Drawing Rectangles Help
    By TheSheepey9 in forum Java Theory & Questions
    Replies: 4
    Last Post: April 23rd, 2013, 08:53 PM
  3. JPanel drawing question
    By peter9207 in forum AWT / Java Swing
    Replies: 3
    Last Post: August 26th, 2011, 01:35 PM
  4. drawing a BufferedImage onto a JPanel
    By nemo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 13th, 2010, 07:48 PM
  5. Drawing image on JPanel from another frame
    By jeryslo in forum AWT / Java Swing
    Replies: 3
    Last Post: December 8th, 2009, 04:01 PM

Tags for this Thread