alibabaei12
alibabaei12

Reputation: 187

Why JFrame is empty after drawing my graphics?

Why JFrame is empty after drawing my graphics?

package javaGame;

import java.awt.Graphics;

import javax.swing.JFrame;


public class javaGame extends JFrame {

   public javaGame (){
       setVisible(true);
       setSize(250,250);
       setResizable(false);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setTitle("Java Game");
   }

   public void paint (Graphics g){
      g.drawString("Hello world!", 75, 75);
   }

   public static void main (String[] args){
       new javaGame ();
   }
}

Upvotes: 3

Views: 255

Answers (3)

nachokk
nachokk

Reputation: 14413

1) Follow java code conventions javaGame should be JavaGame

2) Swing programs should override paintComponent() instead of overriding paint().

3) You should make custom painting in a JComponent like JPanel.

Change your code to for example something like this.

public class JavaGame {

    private JFrame frame; //use composition instead of concrete inheritance

    public JavaGame () {
        jframe.setSize(250,250);
        jframe.setResizable(false);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.setTitle("Java Game");
        jframe.add(new JPanel() {
            @Override
            public void paintComponent (Graphics g) {
                super.paintComponent(g);
                g.drawString("Hello world!", 75, 75);
            }
        });
        jframe.pack(); //sizes the frame
        jframe.setVisible(true);
    }

    public static void main (String[] args) {
        SwingUTilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new javaGame();
            }
        });
    }
}

Upvotes: 1

Roman C
Roman C

Reputation: 1

When you opening a new frame you should make it visible after validate or pack is applied to it, also call setVisible(true) at the end of the frame creation. The code for modification:

public void paint (Graphics g){
   super.paint(g);
   g.drawString("Hello world!", 75, 75);
}

Upvotes: 1

tbodt
tbodt

Reputation: 16987

You are used to applets, where overriding paint will actually work. In a JFrame, the recommended technique is a little more complicated. Add this code to your constructor:

getContentPane().add(new JPanel() {
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawString("Hello world!", 75, 75);
    }
});

This creates an anonymous subclass of JPanel which overrides paintComponent to do some painting, then adds the JPanel to the frame. The call to super.paintComponent is important.

There are several other things that you should know are different from applets:

  • You need to call setVisible(true) to make the frame appear. This is mandatory for your program to work.
  • You should add setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) if you want the program to be closed if the window is closed. Highly recommended.

Upvotes: 3

Related Questions