Results 1 to 2 of 2
  1. #1
    maskanio is offline Member
    Join Date
    Nov 2012
    Posts
    12
    Rep Power
    0

    Default Java program freezing while using graphics?

    Heres my code. Go ahead try to run it. Its supposed to just display a string in a program, but instead it freezes when you try to run.
    Java Code:
    package javagame;
    
    import javax.swing.JFrame;
    
    import java.awt.Graphics;
    
    
    public class JavaGame extends JFrame {
    
        
    public JavaGame(){
    setTitle("Java Game");
    setSize(500, 500);
    setResizable(false);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    }
     public void paint(Graphics g){
         g.drawString("Hello World!", 75, 75);
     
     }   
        public static void main(String[] args) {
       new JavaGame();
        }
    }
    Oh and this isn't homework, I'm doing this for fun. Passed Java class already :)

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: Java program freezing while using graphics?

    You are overriding paint in the JFrame so the super class implementation is essentially ignored. You should put this as the first statement in your paint method.
    Java Code:
    super.paint(g);
    Also, for future reference. Don't extend JFrame and don't draw directly in the JFrame. Check out the tutorials in my signature for tutorials on this and lots more.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Simple Graphics program not working.
    By forms in forum New To Java
    Replies: 7
    Last Post: 03-11-2012, 04:44 PM
  2. Extracting program, custom graphics goes away..
    By AndroidAppNewbie in forum New To Java
    Replies: 26
    Last Post: 03-08-2011, 08:25 PM
  3. Program Freezing While Reading Input
    By airowe in forum New To Java
    Replies: 15
    Last Post: 01-11-2011, 04:38 AM
  4. Graphics program
    By ccie007 in forum New To Java
    Replies: 6
    Last Post: 10-27-2010, 09:08 PM
  5. why does this code keep freezing?
    By berkeley in forum New To Java
    Replies: 4
    Last Post: 06-03-2010, 05:41 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
  •