Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java making a 'dot/pixel' In swing/awt

Tags:

java

I want to know how to make a dot/pixel at a certain x,y co-ordinate on my JFrame.

Anyone know some simple code for this?

like image 664
James Andrew Avatar asked Nov 18 '10 15:11

James Andrew


1 Answers

I have created a small example program:

public class Test extends JFrame {

    public Test() {
        this.setPreferredSize(new Dimension(400, 400));
        this.pack();
        this.setVisible(true);
        this.setDefaultCloseOperation(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 no drawPoint..)
        g.drawLine(locX, locY, locX, locY); 
    }

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

You could also use the update or paintComponents method which would be much nicer. But then you have to make sure, that it gets called. If you have problems and it does not get called you could use the following solution: Why is paint()/paintComponent() never called?

like image 147
Prine Avatar answered Oct 22 '22 06:10

Prine