Painting an Image onto a JPanel
Author |
Message |
deltamanx
|
Posted: Mon Apr 25, 2011 9:49 pm Post subject: Painting an Image onto a JPanel |
|
|
How to: Paint an Image onto a JPanel
This tutorial will cover:
- Painting an Image onto a JPanel
- Buffered Image
- ImageIO
Before we begin, let's import all the necessary libraries.
code: | import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
|
First off we will need to create a subclass of JPanel in order to override the paintComponent(Graphics g) method.
So let's create a basic Class.
code: | public class PaintImage extends JPanel
{
public PaintImage()
{
super();
}
}
|
We now have the bones for our program.
In order to paint an Image onto a component, your going to need an image. But an image lying around on your desktop
won't do. You're going to have to bring it into the memory by reading in the Image. This will be stored as a BufferedImage;
So let's declare a BufferedImage:
code: | public static BufferedImage image; |
Now let's get the image in question.
We can use the static method read(File imageFile) in the ImageIO class, it should look something like this.
code: | image = ImageIO.read(new File("Path to image")); |
However, since we're doing I/O, we're going to have to place this in a try-catch block in order to catch a possible IOException.
Now we have the code to read in our image from a file, and store it in the memory.
Now we're going to have to paint the image onto the component. To do this, we're going to need to override the paintComponent(Graphics g)
method that we inherited from our super Class.
code: | public void paintComponent (Graphics g)
{
}
|
And finally, we can now place the code to draw our image onto the JPanel.
We'll be using the drawImage(BufferedImage i, x, y, ImageObserver observer);
For the purposes of this tutorial, we won't be using an ImageObserver, so we'll leave the field as [b]null[/null].
So what we end up with is:
code: | g.drawImage(image, 0, 0, null);
repaint(); //Just to make sure that the JPanel updates it's Graphics
|
And let's put it all together and we get:
code: | import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
public class PaintImage extends JPanel
{
public static BufferedImage image;
public PaintImage ()
{
super();
try
{
image = ImageIO.read(new File("Image.jpg"));
}
catch (IOException e)
{
//Not handled.
}
}
public void paintComponent(Graphics g)
{
g.drawImage(image, 0, 0, null);
repaint();
}
public static void main(String [] args)
{
JFrame f = new JFrame("Window");
f.add(new PaintImage());
f.setSize(image.getWidth(), image.getHeight() + 30);
f.setVisible(true);
}
} |
I even added a main method so you could test it more easily!
Enjoy, and I hope you learned something!
I'll attach a demo. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
rafael
|
Posted: Tue Sep 27, 2016 8:29 am Post subject: RE:Painting an Image onto a JPanel |
|
|
hi can you teach me how can I display or save image in svg file |
|
|
|
|
|
|
|