Page 1 of 2 12 LastLast
Results 1 to 20 of 25
  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default My window is popping up, but the rectangle is not appearing

    here is the code: the window opens, no errors, but the rectangle does not appear
    Java Code:
    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();
    }
    }
    sorry for not indenting, I am using a text editor

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: My window is popping up, but the rectangle is not appearing

    It doesn't look like your myGame class extends any type of JComponent(i.e. a JPanel).

  3. #3
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Re: My window is popping up, but the rectangle is not appearing

    MyGame should extend JPanel or Canvas paint on. The paintComponent() method paints onto the classes JComponent. You should add the JPanel to the JFrame. Also, you have your x and y in the width and height for the g.fillRect(x, y, width, height).
    You do not need to drawRect() and then fillRect(). The fillRect() should suffice for drawing a rectangle.

  4. #4
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: My window is popping up, but the rectangle is not appearing

    Ok, so I just need to put extends JPanel? What would be the correct code?

  5. #5
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: My window is popping up, but the rectangle is not appearing

    I'm not really clear on what I'm doing wrong.... will just putting "extends JPanel" fix it? Why do I need to do that when I don't even use a JPanel? Is there any way to do it without doing extends JPanel?

  6. #6
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: My window is popping up, but the rectangle is not appearing

    Think about it like this.
    Your myGame class, as it stands now, is a standard Java class.
    Therefore, there is no 'visible' aspect to it, if you will.
    So, when would the paintComponent() method ever be called on your myGame object that you create?
    What is there to 'paint'?

  7. #7
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: My window is popping up, but the rectangle is not appearing

    oh, so what do I do to tell it to paint it?

    How would I tell it to paint?
    Last edited by MW130; 01-07-2014 at 05:23 AM.

  8. #8
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: My window is popping up, but the rectangle is not appearing

    By having your class extend a JPanel, you are essentially saying that myGame IS A JPanel.
    This means that anything a JPanel can do, a myGame object can do.
    What you want to do is (as you have partially done) override the paintComponent method.
    That is your way of saying "I know that the paintComponent method is defined and implemented by a class that I (myGame) inherit from, but I want to do something different".
    Keep in mind that at some point you will need to add the JPanel to the JFrame that you have created.

  9. #9
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: My window is popping up, but the rectangle is not appearing

    Ok, Sehudson, so would the code work if I created a JPanel and added it to the JFrame? After that, do I need to add anything to the JPanel?
    Aka, what exactly do I need to do so that my code displays graphics?

  10. #10
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: My window is popping up, but the rectangle is not appearing

    I tried extending JPanel and adding it to JFrame; nothing changed.

  11. #11
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: My window is popping up, but the rectangle is not appearing

    -Update your myGame class so that it extends JPanel
    -Update your myGame constructor so that you actually add the instance of myGame to the JFrame

    Java Code:
    f.setContentPane(this);
    This will get you up and running, but it's not the best way to do it, because you will end up using the 'this' keyword inside of your constructor, which is something you want to try to avoid. The reason is because the constructor is used to create an instance of a class. Therefore, when you use the 'this' keyword and pass the object to a method, you could end up in a situation where you pass an 'incomplete' or not fully constructed object which can have consequences. In your case, the constructor is just used for setting up the JFrame, so you can get away with it, but just keep in mind that you want to avoid that. I would refactor the code, maybe move the contents of your constructor to a separate method, but that's up to you.

  12. #12
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: My window is popping up, but the rectangle is not appearing

    So should I just write a method for setting up the window?

    How would you go about adding the instance of myGame to the JFrame?
    Last edited by MW130; 01-07-2014 at 06:07 AM.

  13. #13
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: My window is popping up, but the rectangle is not appearing

    So, I changed it to this, but it is still just a gray box. No graphics.

    Java Code:
    import javax.swing.*;
    import java.awt.Graphics; 
    import javax.swing.SwingUtilities;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.BorderFactory;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    
    public class myGame extends JComponent {
    public int x = 20;
    public int y = 20;
    public void setUp(){
        
    JFrame f = new JFrame("The most advanced game in the world");
        JPanel p = new JPanel();
        f.add(p);
        f.setSize(500,500);
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.drawString("Hello",20,50);
    g.drawRect(x,y,50,50);
    g.fillRect(x,y,50,50);
    }
    
    public static void main(String[] args) {
    myGame g = new myGame();
    g.setUp();
    }
    }
    Can you edit the code on there and show me the new code so I can understand the difference?

    Thank you

  14. #14
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: My window is popping up, but the rectangle is not appearing

    You are just missing the adding of the myGame object to the JFrame in your setUp() method:

    Java Code:
    f.setContentPane(this);

  15. #15
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: My window is popping up, but the rectangle is not appearing

    How do I add the myGame object to the JFrame in the setup method?

  16. #16
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: My window is popping up, but the rectangle is not appearing

    I posted the code in my last comment.

  17. #17
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: My window is popping up, but the rectangle is not appearing

    I don't really understand that code. Is there a simpler way to do it? Or can you explain that code?

  18. #18
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: My window is popping up, but the rectangle is not appearing

    Your code made it work! :) :) but I need to resize it a lot for me to see the square. At first it is blank.

  19. #19
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: My window is popping up, but the rectangle is not appearing

    I tried extending JFrame, so that I am extending a JComponent. Why am I getting the same error; the square not showing up?

    Java Code:
    import javax.swing.*;
    import java.awt.Graphics; 
    import javax.swing.SwingUtilities;
    import javax.swing.JFrame;
        import javax.swing.JPanel;
        import javax.swing.BorderFactory;
        import java.awt.Color;
        import java.awt.Dimension;
        import java.awt.Graphics;
        
    public class myGame extends JFrame {
    public int x = 20;
    public int y = 20;
    public void setUp(){
    setSize(500,500);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void paintComponent(Graphics g){
    g.drawString("Hello",20,50);
    g.drawRect(x,y,150,150);
    g.fillRect(x,y,150,150);
    }
    
    public static void main(String[] args) {
    myGame g = new myGame();
    g.setUp();
    }
        }

  20. #20
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: My window is popping up, but the rectangle is not appearing

    Quote Originally Posted by MW130 View Post
    I tried extending JFrame, so that I am extending a JComponent. Why am I getting the same error; the square not showing up?
    A JFrame doesn't extend a JComponent; don't guess but read the API documentation.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Page 1 of 2 12 LastLast

Similar Threads

  1. popping the last element in a linked list
    By fultonwilcox in forum New To Java
    Replies: 2
    Last Post: 09-14-2012, 04:25 AM
  2. Trouble popping a stack
    By Savantx in forum New To Java
    Replies: 4
    Last Post: 03-16-2011, 12:26 AM
  3. Replies: 22
    Last Post: 05-19-2010, 02:00 PM
  4. XTerm window appearing in full screen swing app
    By clarose in forum AWT / Swing
    Replies: 1
    Last Post: 11-17-2008, 10:56 PM
  5. Stack not popping
    By bugger in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 04: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
  •