添加图片到面板不使用波动 [英] Add image to panel not using swings

查看:515
本文介绍了添加图片到面板不使用波动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何我们面板中添加位图图像,然后让该图像是使用图形,告诉面板使用图像里面相同的图形绘制一条线。

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.

推荐答案

基本的绘画是由Swing组件做的paintComponent 方法。

Basic painting is done by a Swing components paintComponent method.

您的最佳选择是使用加载图像的ImageIO API ...

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());
}

您可能想有一个读

  • Performing Custom Painting
  • Working with Images

这篇关于添加图片到面板不使用波动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆