Page 2 of 2 FirstFirst 12
Results 21 to 25 of 25
  1. #21
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,902
    Rep Power
    5

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

    Quote Originally Posted by JosAH View Post
    A JFrame doesn't extend a JComponent; don't guess but read the API documentation.

    kind regards,

    Jos
    Like this.

    Google
    type in: java VERSION JFrame
    Example: java 7 JFrame

    First hit: JFrame (Java Platform SE 7 )

    If you practice typing quickly, its a 2 second action.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  2. #22
    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

    Quote Originally Posted by JosAH View Post
    A JFrame doesn't extend a JComponent; don't guess but read the API documentation.

    kind regards,

    Jos
    So then what do I do to not have to use that line he gave me

  3. #23
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,456
    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
    So then what do I do to not have to use that line he gave me
    What about your reply #18? You had working code ...

    kind regards,

    Jos

    ps. I (we) don't want to give you 'teh codez', because we want you to understand what you're doing.
    cenosillicaphobia: the fear for an empty beer glass

  4. #24
    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

    Quote Originally Posted by JosAH View Post
    What about your reply #18? You had working code ...

    kind regards,

    Jos

    ps. I (we) don't want to give you 'teh codez', because we want you to understand what you're doing.
    Ok
    I got it working with
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Graphics;
    
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class myGame extends JComponent {
        public int x = 20;
        public int y = 20;
    
        myGame() {
            JFrame f = new JFrame("The most advanced game in the world");
            JPanel p = new JPanel();
            // New code
            p.setLayout(new BorderLayout());
            p.add(this);
            // End new code
            f.add(p);
            f.setSize(500, 500);
            f.setVisible(true);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            // Modified code
            g.drawString("Hello", 20, 20);
            g.fillRect(x, y, 50, 50);
            // End modified code
        }
    
        public static void main(String[] args) {
            new myGame();
        }
    }
    What got it working was adding the JComponent object in the p.add(this); line.

  5. #25
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,456
    Blog Entries
    7
    Rep Power
    20

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

    Now you're adding a JPanel to a JFrame and you're adding your own JComponent to that JPanel (see your own code); why not take out the middle man?

    kind regards,

    Jos

    ps. better extend your own class from a JPanel instead of a JComponent ...
    cenosillicaphobia: the fear for an empty beer glass

Page 2 of 2 FirstFirst 12

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
  •