问题
how to we add bitmap image in panel and then get the graphics that the image is using and tell the panel to draw a line using the same graphics inside the image.
回答1:
Basic painting is done by a Swing components paintComponent
method.
The best choice you have is to load the image using the ImageIO
API...
BufferedImage image;
public void loadImage() throws IOException {
image = ImageIO.read(...);
// ImageIO can read a image from a file or a URL or a ImageInputStream
}
Then simply paint the image...
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
// Now you can continue drawing ontop of it...
g.setColor(Color.RED);
g.drawLine(0, 0, image.getWidth(), image.getHeight());
}
You might like to have a read of
- Performing Custom Painting
- Working with Images
来源:https://stackoverflow.com/questions/13774913/add-image-to-panel-not-using-swings