Results 1 to 5 of 5
  1. #1
    Sandeep Bob is offline Member
    Join Date
    Jun 2014
    Posts
    1
    Rep Power
    0

    Default Running this code only makes the JFrame, but doesn't draw anything.

    Java Code:
    import java.awt.*;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class openGraphics extends JPanel {
    	public static void main(String[] args) {
    		new openGraphics();
    	}
    
    	public openGraphics() {
    		JPanel p = new JPanel();
    		JFrame q = new JFrame();
    		q.add(p);
    		q.setVisible(true);
    	}
    	@Override
    	public void paintComponent(Graphics g) {
    		g.setColor(Color.blue);
    		g.fillRect(0, 0, 100, 100);
    	}
    }
    Last edited by Sandeep Bob; 06-30-2014 at 04:32 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Running this code only makes the JFrame, but doesn't draw anything.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    Where is an instance of the openGraphics class added to the JFrame so its paintComponent() method will be called?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    805
    Rep Power
    6

    Default Re: Running this code only makes the JFrame, but doesn't draw anything.

    go thru the[U] modified code.please go thru sun tutorial ..
    ****CODE removed
    }
    Last edited by Norm; 06-30-2014 at 02:02 PM. Reason: Please don't do OPs work for him
    Ramya:cool:

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    929
    Rep Power
    2

    Default Re: Running this code only makes the JFrame, but doesn't draw anything.

    Quote Originally Posted by RamyaSivakanth View Post
    go thru the[U] modified code.please go thru sun tutorial ..
    When are you going to stop using the [quote][/quote] and instead use [code][/code] tags?

    OT: either set a size, or preferredSize and pack() of the JFrame.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: Running this code only makes the JFrame, but doesn't draw anything.

    And a minor observation. Class names by convention start with an upper case letter. For more on Java Naming Conventions, check out the
    link in my signature.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Draw in Jframe without extends
    By tcms in forum AWT / Swing
    Replies: 11
    Last Post: 11-24-2013, 06:33 PM
  2. Replies: 7
    Last Post: 05-30-2013, 06:17 PM
  3. Compiling this code makes Level 1 not work
    By thelordposeidon in forum New To Java
    Replies: 1
    Last Post: 05-20-2013, 11:44 AM
  4. Draw outside JFrame
    By DFTBA in forum AWT / Swing
    Replies: 2
    Last Post: 06-13-2012, 08:18 AM
  5. Why doesn't the image draw?
    By MrFish in forum Java 2D
    Replies: 20
    Last Post: 11-16-2010, 07:32 PM

Tags for this Thread

Posting Permissions

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