1

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();
    }    
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Amit
  • 157
  • 8
  • What do you mean with "How would I change this into two classes?" – Jens Jun 27 '14 at 08:18
  • 4
    never to call repaint(); inside paint()/paintComponent(), because can creating endless loop – mKorbel Jun 27 '14 at 08:18
  • 2
    Try to extends your panel and override paintComponent() method as the [post](http://stackoverflow.com/questions/15103553/difference-between-paint-and-paintcomponent) say. – Tony Jun 27 '14 at 08:27
  • 1
    *"frame.setSize(5000,2000);"* You really have a 10 mega pixel display? What is it, the Jumbotron? – Andrew Thompson Jun 27 '14 at 08:54
  • 1
    "***runpaintgui***"; It's a good habit to use some name rules. You can refer to http://java.about.com/od/javasyntax/a/nameconventions.htm – Eugene Jun 27 '14 at 09:59

2 Answers2

5

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Martin Dinov
  • 8,757
  • 3
  • 29
  • 41
5

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.

David Yee
  • 3,515
  • 25
  • 45
Jens
  • 67,715
  • 15
  • 98
  • 113