简体   繁体   中英

Using super.paint() won't show anything

I know this is probably simple, but it is causing me trouble. When I use paint(), it shows nothing, and if I use paintComponent(), it shows an error (cannot find symbol). What am I doing wrong?

This is an example of using paint():

import javax.swing.*; 
import java.awt.Graphics; 
import java.awt.*; 

public class Test extends JFrame {

    public Test() {
        this.setPreferredSize(new Dimension(400, 400));
        this.pack();
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);

        // define the position
        int locX = 200;
        int locY = 200;

        // draw a line (there is now drawPoint..)
        g.drawLine(locX, locY, locX, locY); 
    }

    public static void main(String[] args) {
        Test test = new Test(); 
    }
}

Comments say this is a nice and simple code but I can't see anything because it shows nothing.

[SOLVED] To all who answered, thanks. LOL at me bros, I really didn't notice that there was a tiny dot. Awesome dude, thanks.

You code is not the recommended way of doing things, but that aside, it works.

You do not set a color to paint with, and you draw a single dot. You probably just didnt see it (I had to look twice). It draw a single black pixel at 200, 200.

I would bet the problem is that you're only drawing a single point, so it's hard to see. Your code works fine for me.

However, you should be extending JPanel, not JFrame. Recommended reading: http://docs.oracle.com/javase/tutorial/uiswing/painting/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM