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

    Default A window pops up, but no graphics

    I run this code and a window will pop up, but no graphics :(
    I tried to draw a rectangle onto the screen, but it will not show up. I was told to extend JComponent, and I did, but it still does not work.

    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;
    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){
    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) {
    
    new myGame();
    }
    }

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

    Default Re: A window pops up, but no graphics

    This question is identical to your other question in this list; it was already solved ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 9
    Last Post: 12-30-2013, 07:34 PM
  2. Centering graphics on the graphics window
    By janyc71876 in forum New To Java
    Replies: 5
    Last Post: 11-25-2013, 08:25 PM
  3. Replies: 9
    Last Post: 07-28-2011, 11:05 AM
  4. Scrollbar on graphics output window
    By ann123 in forum AWT / Swing
    Replies: 3
    Last Post: 06-27-2009, 09:47 PM
  5. Error message pops up no matter what?
    By murr0508 in forum New To Java
    Replies: 5
    Last Post: 02-26-2009, 11:10 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
  •