Java: Load image from file, edit and add to JPanel

前端 未结 3 1581
陌清茗
陌清茗 2021-01-28 05:40

I want to load an image from my computer into 2D Graphics so that I can edit it afterwards and then I want to add it to JPanel. If you need to see my project I can

3条回答
  •  旧巷少年郎
    2021-01-28 05:59

    For image loading you should use ImageIO object with method read(File file) see docs. Then you will get BufferedImage instance of which you can make your changes through Graphics2D instance which you'll obtain by calling createGraphics() on the image instance see docs. Last thing, override method paintComponent() from JPanel or better JComponent see docs and there you can draw your image on Graphics instance which you'll get as parameter in paintComponent(Graphics g) method by calling drawImage(Image img, int x, int y, ImageObserver observer) see docs where ImageObserver set to null.

提交回复
热议问题