adding rectangles in images
hey guys, I'm new to Java, and I need to highlight a part of an image with a rectangle, but I don't know if it is possible to modify a jpg picture this way. I found out on the internet that I could add a rectangle to an image and store it something called TexturePaint. I tried to put it on my JPanel, but it didn't compile. Anyone knows a simple way to display a TexturePaint or even better, how to add a simple rectangle in an image??
Rectangle Rect = new Rectangle(60, 70, image.getWidth(), image.getHeight());
TexturePaint TextureAux = new TexturePaint(image, Rect);
JPanel panel = new JPanel ();
panel.add(new JLabel(new ImageIcon(image)));
panel.add(new JLabel(new TexturePaintIcon(TextureAux))); //---> THIS DOESN'T COMPILE!
JFrame frame = new JFrame ("Images");
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
Re: adding rectangles in images
If you want help, you'll have to provide the actual compiler error, as well as an SSCCE that demonstrates the problem. Don't forget the highlight tags.
Also, this thread is in the wrong section. I'll move it for you.
Re: adding rectangles in images
Quote:
how to add a simple rectangle in an image
If you mean to display the image with a rectangle on it,
In a paintComponent method, draw the image first and then draw the rectangle
Re: adding rectangles in images
Don't you know where I can find a simple tutorial for paintComponent?
I ended up modifying the pixels of my images by using 2 fors. Well, I know it's not the best solution, but it worked as well.
// x and y are the top left corners of the rectangle
// Xsize and Ysize are respectively, the width and the height of the rectangle
// ImageDest is the image where I'm adding the blue rectangles
for (int xi=x; xi<(x+Xsize); xi++) {
ImageDest.setRGB(xi, y, 255);
ImageDest.setRGB(xi, y+Ysize, 255);
}
for (int yi=y; yi<(y+Ysize); yi++) {
ImageDest.setRGB(x, yi, 255);
ImageDest.setRGB(x+Xsize, yi, 255);
}
Re: adding rectangles in images
There are a lot of code samples on the forum that use the paintComponent method. Try doing a Search here.