Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Graphics Will Not Display

Here is my code:

package survival;
import javax.swing.*;
import java.awt.*;

public class Survival extends JFrame { 
    private static int applicationWidth = 1400;
    private static int applicationHeight = 900;  

    public Survival() {
        setTitle("Survival");
        setResizable(false);
        setSize(applicationWidth, applicationHeight);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void paint(Graphics g) {
        g.drawString("Test", 0, 0);
    }

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

Why isn't "Test" showing up?

like image 494
Conner Ruhl Avatar asked Jun 08 '26 05:06

Conner Ruhl


1 Answers

Do not override paint. Whenever you customize a component, override paintComponent.

Example -

@Override
protected final void paintComponent(final Graphics g){
    super.paintComponent(g);
    final Graphics gCopy = g.create(); // Prevents clobbering
    gCopy.drawString("Test", 0, 0);
    gCopy.dispose();
}
like image 165
mre Avatar answered Jun 10 '26 18:06

mre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!