Java JFrame not drawing lines in window

package Main;

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

public class Main extends JFrame{

    public static void main(String[] args) {

        int width = 800;
        int height = 600;
        String title = "Test";

        JFrame display = new JFrame();
        display.setTitle(title);
        display.setSize(width, height);
        display.setVisible(true);
        display.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void paint(Graphics g) {

        g.setColor(Color.white);
        g.drawLine(0, 100, 800, 300);

        getContentPane().setBackground(Color.black);
    }

}

I’m using Java’s JFrame. So this isn’t recognising the paint method and cant figure out why. I’ve been looking on YouTube videos and having a look to see if anyone has had similar problems, however everything I’ve found doesn’t seem to help the problem.

when i set the background colour in the main part, it works, bit in paint, it doesn’t seem to do anything and leaves it blank.

Its a white line over a black background, so i should easily be able to see it.

Answer

You are creating an instance of JFrame with

JFrame display = new JFrame();

But the JFrame class has no logic to draw a white line on a black background. That logic is in your custom class Main. So instead, you should create an instance of Main:

JFrame display = new Main();

However, that change along still won’t fix the problem because you are setting the background color of the “content pane” but trying to draw directly on the JFrame itself. The preferred solution here is to extend JPanel instead of JFrame and override its paintComponent() method. Then create an instance of your new class to use as the content pain:

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

public class MainPanel extends JPanel{

    public static void main(String[] args) {

        int width = 800;
        int height = 600;
        String title = "Test";

        JFrame display = new JFrame();
        display.setTitle(title);
        display.setSize(width, height);
        display.setVisible(true);
        display.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        display.setContentPane(new MainPanel());
    }

    public MainPanel() {
        setBackground(Color.black);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.white);
        g.drawLine(0, 100, 800, 300);
    }
}

Notes:

  1. I call setBackground() in the constructor because it does not rely on the Graphics instance passed to paintComponent(). This also avoids the overhead of another function call for each render.

  2. In paintComponent(), I call super.panitComponent(). This allows JPanel to clear the area to be painted and any other necessary initialization.

Leave a Reply

Your email address will not be published. Required fields are marked *