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

Please read this tutorials about Icon in Swing and your Image/ImageIcon would by placed to the JLabel this way eliminated all troubles came from paint/paintComponents.

Please read this tutorials about Icon in Swing and your Image/ImageIcon would by placed to the JLabel, this way eliminated all troubles came from paint/paintComponents ...

1 for not extending any component. – Andrew Thompson Jul 23 at 3:56.

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.

3 Do not override paint(...), instead override paintComponent(...). – Moonbeam Jul 22 at 19:41 Right, my fault, thanks.. – Sorceror Jul 22 at 19:47.

To load your image into the memory, you can use ImageIO. Read(File). To edit it afterwards, obtain a Graphics2D instance from it by calling createGraphics(): BufferedImage img = ImageIO.

Read(yourFile); Graphics2D g = img.createGraphics(); // Draw here on the graphics g.dispose(); You can even turn on anti-alias by setting a RenderingsetRenderingKEY_ANTIALIASING, RenderingVALUE_ANTIALIASING_ON); Then, to add it to a JPanel, create your custom JComponent and add an instance of that component to your JPanel: public class JImageComponent extends JComponent { private BufferedImage img; public JImageComponent(BufferedImage bi) { img = bi; } @Override public void paintComponent(Graphics g) { g. DrawImg(img, 0, 0, this); } }.

1 extends JComponent – mKorbel Jul 22 at 21:34.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions