Add image to panel not using swings [closed]

余生颓废 提交于 2019-12-12 06:46:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!