Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
    }
}
like image 344
Dallox Avatar asked Dec 01 '22 05:12

Dallox


1 Answers

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.

like image 126
MadProgrammer Avatar answered Dec 05 '22 10:12

MadProgrammer