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 5 of 5

Thread: I get no error, my window opens, but the graphics don't show

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I get no error, my window opens, but the graphics don't show

    I wrote a program to display a rectangle within a window, but it won't show. Is it because I didn't call the paint() method?
    Sorry for indentation, I am using a text editor
    import javax.swing.*;
    import java.awt.Graphics; 
     
    public class myGame {
    public int x = 20;
    public int y = 20;
    myGame(){
    JFrame f = new JFrame("The most advanced game in the world");
    f.setSize(500,500);
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void paintComponent (Graphics g){
    g.drawString("Hello",20,50);
    g.drawRect(50,50,x,y);
    g.fillRect(50,50,x,y);
    }
    public static void main(String[] args) {
     
    new myGame();
    }
    }

    Thanks for any and all help!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,124
    My Mood
    Mellow
    Thanks
    186
    Thanked 658 Times in 645 Posts

    Default Re: I get no error, my window opens, but the graphics don't show

    The beauty and "automatic" function of the paintComponent() method is when it overrides the parent's paintComponent() method, and usually the parent is a JComponent or a child of a JComponent (hence the name, paintComponent()). You have not yet learned the finer points of painting or drawing in/on Swing components, so I recommend you read the Custom Painting Tutorial. It's quite good and should open your eyes to new things.

  3. #3
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: I get no error, my window opens, but the graphics don't show

    Quote Originally Posted by MW130 View Post
    I wrote a program to display a rectangle within a window, but it won't show. Is it because I didn't call the paint() method?
    No, paint/paintComponent is not generally invoked by the programmer .... but by the framework!
    And there is another important question. Your myGame class is not a "component" (doesn't extend for example JComponent or JPanel) and thus a paintComponent method is totally pointless because the GUI framework cannot handle it.

    As GregBannon has well understood, you have to better understand the painting concepts in AWT/Swing. Take also a look at Trail: 2D Graphics (The Java™ Tutorials)
    Andrea, www.andbin.net — SCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginners – My new project Java Examples on Google Code

  4. #4
    Junior Member
    Join Date
    Jan 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I get no error, my window opens, but the graphics don't show

    So, I have to extend JComponent?

  5. #5
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: I get no error, my window opens, but the graphics don't show

    Quote Originally Posted by MW130 View Post
    So, I have to extend JComponent?
    Or typically JPanel. There are both conceptual and technical differences between JComponent and JPanel. The latter is a "generic" container that is typically used for layout of N components but can also be used for custom painting.
    Andrea, www.andbin.net — SCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginners – My new project Java Examples on Google Code

Similar Threads

  1. defaultCloseOperation doesn't work plus JButttons don't show up
    By Gugubo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 27th, 2013, 05:42 AM
  2. JInternalFrame Only Show one Window
    By sadaharu in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 1st, 2013, 11:18 PM
  3. Replies: 2
    Last Post: August 27th, 2012, 09:19 PM
  4. Why Graphics objects don't overlap.
    By SyntheticD in forum Java Theory & Questions
    Replies: 0
    Last Post: March 27th, 2011, 09:40 PM
  5. How do i show all the values in one window(JOptionPane)??
    By Antonioj1015 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 25th, 2009, 08:24 PM