不使用摆动将图像添加到面板

时间:2023-02-04 15:47:28

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 个解决方案

#1


2  

Basic painting is done by a Swing components paintComponent method.

基本绘画由Swing组件paintComponent方法完成。

The best choice you have is to load the image using the ImageIO API...

您拥有的最佳选择是使用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

您可能希望阅读

#1


2  

Basic painting is done by a Swing components paintComponent method.

基本绘画由Swing组件paintComponent方法完成。

The best choice you have is to load the image using the ImageIO API...

您拥有的最佳选择是使用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

您可能希望阅读