Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: basic plotting, drawing a point/dot/pixel

I need to just have a panel inside of which i'd be able to draw. I want to be able to draw pixel by pixel.

ps: I don't need lines/circles other primitives. pps: the graphics library does not really matter, it can be awt, swing, qt.. anything. I just want to have something that is usually represented by Bufferedimage or somethign like that where you set colors of single pixels and then render it to the screen.

like image 986
Dmitry Avtonomov Avatar asked Feb 13 '13 17:02

Dmitry Avtonomov


People also ask

What is Graphics G in Java?

paintComponent(Graphics g) is a method inherited from JComponent (Note that paintComponent should have @Override anotation), it is part of the draw system of the GUI. It's invoked from Java Swing Framework to ask for a Component to draw itself on the screen.

How do you draw a circle in Java?

Solution: Write a method, drawCircle() , which has parameters for the coordinates of the center point and a radius. It then calls drawOval with transformed parameters. To make a drawing, define a new component by subclassing JPanel and overriding the paintComponent() method.

How is a pixel drawn in Java?

You can color a single pixel in a Java drawing by drawing a line with the same start point and end point. Before you can draw pixels, you must create a JFrame or other visible component and add a custom component with an overridden paint method.


2 Answers

An example of one way to do it:

// Create the new image needed
img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB );

for ( int rc = 0; rc < height; rc++ ) {
  for ( int cc = 0; cc < width; cc++ ) {
    // Set the pixel colour of the image n.b. x = cc, y = rc
    img.setRGB(cc, rc, Color.BLACK.getRGB() );
  }//for cols
}//for rows


and then from within overridden paintComponent(Graphics g)

((Graphics2D)g).drawImage(img, <args>)
like image 117
Jool Avatar answered Oct 02 '22 22:10

Jool


represented by Bufferedimage ..

I suggest a BufferedImage for that, displayed..

..or something like that where you set colors of single pixels and then render it to the screen.

..in a JLabel - as seen in this answer.

Of course, once we have an instance of BufferedImage, we can setRGB(..).

like image 39
Andrew Thompson Avatar answered Oct 03 '22 00:10

Andrew Thompson