Dallox
Dallox

Reputation: 487

Paint in JFrame not working (Java)

So in class we are making a Java program. We are trying to use the paint(Graphics g) function in the JFrame. We have tried it in the past (weeks ago) and it used to work. But now it doesnt (or we made a mistake somewhere). We have also tried to use paintComponent(Graphics g) but nothing seems to work. Here is our code:

public class MainAc {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Class Paint");       
        JButton button = new JButton("Click for more");             
        frame.setSize(800, 600);    
        frame.add(button);
        frame.setLayout(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        button.setLayout(null);
        button.setLocation(100,100);
        button.setSize(200,100);
        frame.setVisible(true);     
    }
    public void paint(Graphics g){
        g.drawString("Hello", 200, 50);
    }
}

Upvotes: 4

Views: 29514

Answers (4)

Richard Taylor
Richard Taylor

Reputation: 2742

So what you're doing, is implementing a paint method inside your own MainAc class, not the JFrame.

Your MainAc class itself, should derive from the JFrame.

The code below should work. If you don't understand inheritance, you should look over your class notes, it'll be in there.

public class MainAc extends JFrame {

    public static void main(String[] args) {
        new MainAc();
    }
    
    public MainAc() {
        super("Class Paint");       
        JButton button = new JButton("Click for more");             
        setSize(800, 600);    
        add(button);
        setLayout(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        button.setLayout(null);
        button.setLocation(100,100);
        button.setSize(200,100);
        setVisible(true);
    }
    
    public void paint(Graphics g) {
        super.paint(g);
        g.drawString("Hello", 200, 50);
    }
    
}

Upvotes: 5

R. Gabriel
R. Gabriel

Reputation: 73

Here is a simplified version of Sticky's code that seems to work. I changed it to an extend JApplet and use the init() method instead of the main().

public class JavaApplication51 extends JApplet {
    public  void something() {
       JButton button = new JButton("Click for more");             
        add(button);
        setLayout(null);
        button.setLayout(null);
        button.setLocation(100,100);
        button.setSize(101,20);
        setVisible(true); 
    }

    public void paint(Graphics g) {
        super.paint(g);
        g.drawString("Hello", 200, 50);
    }

    public void init() {
        something();
    }
}

Upvotes: -1

MadProgrammer
MadProgrammer

Reputation: 347204

Your class doesn't extend from any Component capable of painting

Have a read through Performing Custom Painting for more ideas

You could do something like:

enter image description here

public class SimplePaint {

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

    public SimplePaint() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new PaintPane());
                frame.setSize(200, 200);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    protected class PaintPane extends JPanel {

        @Override
        protected void paintComponent(Graphics g) {

            super.paintComponent(g);

            Graphics2D g2d = (Graphics2D) g.create();
            String text = "Look ma, no hands";
            FontMetrics fm = g2d.getFontMetrics();
            int x = (getWidth() - fm.stringWidth(text)) / 2;
            int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent();
            g2d.drawString(text, x, y);
            g2d.dispose();


        }

    }

}

Where possible, you should avoid overriding top level container's paint methods, if for no other reason, they're not double buffered.

You should also try and extend from Swing based components for the same reason, as mixing heavy and lightweight components can cause issues with painting.

Upvotes: 12

Dan D.
Dan D.

Reputation: 32391

As the code is, you are just defining a paint method in a class. The class extends Object, so you are not overridding paint on a JComponent.

Your class must extend any subclass of Component or Component itself in order to actually override paint or paintComponent. In your case, MainAc would extend JFrame.

Upvotes: 4

Related Questions