Amit
Amit

Reputation: 177

Paint in Swing not working

Why does this not work? It shows me the GUI but not the paint. How would I change this into two classes?

import java.awt.Graphics;
import javax.swing.JFrame;

public class runpaintgui extends JFrame{    

    public static void main(String[] args){
        JFrame frame = new JFrame();
        frame.setSize(5000,2000);
        frame.setResizable(false);
        frame.setTitle("game");
        frame.setVisible(true);    
    }

    public void paint(Graphics g){
        super.paint(g);
        g.drawString("adsf",40,45);
        g.draw3DRect(50, 30, 600, 700, true);   

        repaint();
    }    
}

Upvotes: 1

Views: 104

Answers (2)

Martin Dinov
Martin Dinov

Reputation: 8825

You are creating a generic JFrame in this line:

JFrame frame = new JFrame();

What you want to probably do is:

JFrame frame = new runpaintgui();

Then your paint() method will be called.

Upvotes: 5

Jens
Jens

Reputation: 69494

You have to instantiate your class and not the JFrame class.

change:

JFrame frame = new JFrame();

to

runpaintgui frame = new runpaintgui();

Then your paint() method will be called.

And do not call repaint() in paint. Because repaint() calls paint.

Upvotes: 5

Related Questions