Is there a Java function equivalent to drawPixel?

Asked

Viewed 237 times

2

I wanted to know if there is any function in java that Plot a pixel, something like drawPixel or setPixel that I see in some other programming languages, in which you report as parameters the X and Y coordinates and the pixel color to be plotted. I don’t want drawLine style functions or something equivalent.

  • But a pixel is just a very short line, it doesn’t exist because it doesn’t need to. The people who created the library understand the subject deeply and decided it was not necessary. If you think you need to show why.

  • 1

    @bigown in computer graphics, chair I’m paying at university, pixel is not a very short line, but the smallest element in a display device, something atomic that can’t break into smaller parts. A line is a pixel concatenation. There is a whole mathematical context behind it, so the problem of using a function that is in the context of a pixel and not a straight line. Teachers don’t like code oriented "gambiarra".

  • 1

    And do they use Java? (sorry, I couldn’t resist. I’m just kidding).

  • Then you’ll have to find a solution that teachers like. By the way no modern programming language I know has graphic primitives, even because it would be a gambiarra.

  • Joking aside, a look at the drawLine source should help (or find out how it’s done, or find a reason not to).

  • @bigown Java has enough of this and I’m writing an answer now. :)

  • @Victorstafusa has enough of this what?

  • I think the problem here is that the question speaks of Java but has the swing tag. It may be that we are thinking of different things here, and so there is some controversy.

  • @bigown, C# has, wanted something like this MSDN

  • @Danielmarques But isn’t the question about Java/Swing? In . Net and not in C#, I know it exists. Languages do not have graphic primitives, libraries do.

  • @bigown, that was just an example. And yes, I want something in java/Swing, because I need to plot a pixel using graphical interface

  • @Danielmarques The simple solution used with Swing is the DrawLine(), the others are complex. But you say teachers won’t like it. So they shouldn’t ask to use Swing. I think I can not offer another solution, I am pragmatic, I solve problems, when a requirement that does not help anything is unproductive to look for good solutions.

Show 7 more comments

1 answer

3


Yes, of course. You can use the methods setRGB(x, y, cor) and getRGB(x, y) class BufferedImage.

You can get a BufferedImage when using one of its builders, or upload an external image. For example:

BufferedImage imagem = new BufferedImage(largura, altura, BufferedImage.TYPE_INT_ARGB);

This will create a blank image in memory. The third parameter is the image type, which in the case of TYPE_INT_ARGB means that each pixel is represented by a int where the 8 most significant bits are the alpha, the next 8 bits are red, plus 8 bits of green, and the least significant 8 bits are blue.

Another way to get an image is to load it from an external resource (using the class ImageIO):

private static BufferedImage carregarImagem() throws IOException {
    try {
        return ImageIO.read(new URL("http://example.com/imagem.jpg"));
    } catch (MalformedURLException e) {
        throw new AssertionError(e);
    }
}

To do other more complex operations (draw circles, polygons, lines, etc), you can get an object Graphics2D:

BufferedImage imagem = ...;
Graphics2D g2 = imagem.createGraphics();

And to draw another image inside your Graphics2D (or your super class Graphics), you can use the method drawImage:

g.drawImage(imagem, posicaoX, posicaoY, null);

If you want to draw these images on the screen (either to plot graphics, or to make games with animations), you can start by overwriting the method paintComponent(Graphics) of any JComponent. For example:

JPanel jp = new JPanel() {
    @Override
    public void paintComponent(Graphics g) {
        // Coloque aqui sua lógica de desenho.
    }
};

Note that the type of the parameter is Graphics. However AWT will always provide an instance of Graphics2D, and in this case it is always safe to cast. So, to draw a component (including to make animations and games), you can create a BufferedImage, draw on it the pixels you want using the methods getRGB(x, y), setRGB(x, y, cor) and createGraphics(), and at the end draw the resulting image into the component:

JPanel jp = new JPanel() {
    @Override
    public void paintComponent(Graphics g) {
        g.drawImage(imagem, posicaoX, posicaoY, null);
    }
};

A suggestion given by @array in a comment is to use the method fillRect(x, y, largura, altura) class Graphics (in combination with the method setColor(Color)). So if you can plot the pixel directly on Graphics if you don’t have one BufferedImage corresponding easily accessible:

Graphics g = ...;
g.setColor(...);
fillRect(posicaoX, posicaoY, 1, 1);
  • I believe something can be done with the fillRect. Example: fillRect(argX, argY, 1, 1);. If you agree, can you explain a little about it in your answer? Hugs. =]

  • @array Updated response.

  • Victor, thank you. Added positive reputation. Hugs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.