How can I edit a jpg image through Java? - java

I have loaded a jpg image in which I want to draw letters and circles, given a x,y coordinate.
I have been trying to figure out the paintIcon of the ImageIcon class
public void paintIcon(Component c,
Graphics g,
int x,
int y)
Does this method allow me to edit jpg images the way I want to? What are supposd to be the Component c and Graphics g paramethers? What would I add to its body to paint circles or letters?
I'm working on Netbeans 6.5, do I have anything builtin for this task (instead of ImageIcon)?

The pure-Java way is to use ImageIO to load the image as a BufferedImage. Then you can call createGraphics() to get a Graphics2D object; you can then draw whatever you want onto the image.
You can use an ImageIcon embedded in a JLabel to do the displaying, and you can add a MouseListener and/or a MouseMotionListener to the JLabel if you're trying to allow the user to edit the image.

Manipulating images in Java can be achieved by using the Graphics or Graphics2D contexts.
Loading images such as JPEG and PNG can be performed by using the ImageIO class. The ImageIO.read method takes in a File to read in and returns a BufferedImage, which can be used to manipulate the image via its Graphics2D (or the Graphics, its superclass) context.
The Graphics2D context can be used to perform many image drawing and manipulation tasks. For information and examples, the Trail: 2D Graphics of The Java Tutorials would be a very good start.
Following is a simplified example (untested) which will open a JPEG file, and draw some circles and lines (exceptions are ignored):
// Open a JPEG file, load into a BufferedImage.
BufferedImage img = ImageIO.read(new File("image.jpg"));
// Obtain the Graphics2D context associated with the BufferedImage.
Graphics2D g = img.createGraphics();
// Draw on the BufferedImage via the graphics context.
int x = 10;
int y = 10;
int width = 10;
int height = 10;
g.drawOval(x, y, width, height);
g.drawLine(0, 0, 50, 50);
// Clean up -- dispose the graphics context that was created.
g.dispose();
The above code will open an JPEG image, and draw an oval and a line. Once these operations are performed to manipulate the image, the BufferedImage can be handled like any other Image, as it is a subclass of Image.
For example, by creating an ImageIcon using the BufferedImage, one can embed the image into a JButton or JLabel:
JLabel l = new JLabel("Label with image", new ImageIcon(img));
JButton b = new JButton("Button with image", new ImageIcon(img));
The JLabel and JButton both have constructors which take in an ImageIcon, so that can be an easy way to add an image to a Swing component.

Use a library to do just that. One that you might try is JMagick.

I have used Java Advanced Imaging library (http://java.sun.com/products/java-media/jai/forDevelopers/jaifaq.html), but you can also look at ImageJ (http://rsbweb.nih.gov/ij/index.html)

I imagen you could use this method to overlay the elements you need every time the image is drawn in the UI (this would happen numerous times as you are not drawing ON the image data its self) but may be suitable to your purposes (and advantageous if the overlay changes over time).
Something like:
new ImageIcon("someUrl.png"){
public void paintIcon(Component c, Graphics g, int x, int y) {
super(c, g, x, y);
g.translate(x, y);
g.drawOval(0, 0, 10, 10);
...
g.translate(-x, -y);
}
};
Having said that, mmyers' answer is much better if you want to modify the image data.

Related

How to render java component to high resolution image?

I'm trying to extract an images of the application window's components in java.
I've created a class that overrides java.awt.GraphicsConfiguration and looks very similar to sun.awt.Win32GraphicsConfig. The problem is that when I call the createAcceleratedImage function with component (for example application window) and 2x larger dimensions than the real one, the java renders it small in the image on the top left corner and the rest of the image is empty space.
My createAcceleratedImage function is same as in sun.awt.Win32GraphicsConfig:
public Image createAcceleratedImage(Component target, int w, int h) {
ColorModel localColorModel = getColorModel(Transparency.OPAQUE);
WritableRaster localWritableRaster = localColorModel.createCompatibleWritableRaster(w, h);
return new OffScreenImage(target, localColorModel, localWritableRaster, localColorModel.isAlphaPremultiplied());
}
I apologize if I forgot to mention more information, If you need more information please ask in comments.
Thank you for help.
I used VolatileImage instead of OffScreenImage inside my CustomComponentPeer.
public VolatileImage createVolatileImage(int width, int height) {
GraphicsConfiguration graphicsConfig = getGraphicsConfiguration();
return graphicsConfig.createCompatibleVolatileImage(width, height);
}
I found that drawImage function in Graphics2D support high resolution rendering only for MultiResolutionImage and VolatileImage. Rendering in high DPI using OffScreenImage is not supported.

Why drawing pixels directly is slower than drawing data in a BufferedImage?

Currently I'm trying to create something to display a "2D view" for a Grid which can be used for various proporses, such as a simple 2D game.
After doing a wrap work with a simple Core class (this holds all information to my visual stuff, including pixels values for sprites) I started the visual work. As my aim is to do this with plain swing/awt in Java I just created a custom JComponent to this.
In my JComponent I tried some approaches and decided to test draw my pixels info in a BufferedImage and at other hand draw the pixels directly in Graphics object.
Looking through internet I found some ways to draw a pixel/dot on screen, like this. In this case, I wrapped this in a method:
private void drawSpritePixels(ArrayList<Spr.Pixel> pixels, int startX, int startY, Graphics2D g) {
for (Spr.Pixel p : pixels){
g.setColor(p.color);
g.fillRect(p.x + startX, p.y + startY, 1, 1);
}
}
For drawing my data onto a BufferedImage, I'm using this:
public BufferedImage spriteImage(int spriteAddress) {
BufferedImage img = GraphicsEnvironment
.getLocalGraphicsEnvironment()
.getDefaultScreenDevice()
.getDefaultConfiguration()
.createCompatibleImage(32, 32, Transparency.TRANSLUCENT);
for (Pixel p : getSpriteInfo(spriteAddresses.get(spriteAddress))) {
img.setRGB(p.x, p.y, p.color.getRGB());
}
return img;
}
As title says, the performance was better using BufferedImage approach. Im my tests BufferedImage takes 40ms to finish while Pixels takes 200ms+ to the same amount of "work".
Then, why this is so different? Should do I avoid any iterations in pixels approach?

Save a drawn picture on a JPanel in a file [java]

I've made a code to draw on a Jpanel
frame1.add( new JPanel() {
public void paintComponent( Graphics g ) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.white);
g2.fillRect(0, 0, width, height);
//Drawnig Part
});
Every thing is OK
Now My question is how to save what I've drawn on my JPanel in a file a.PNG or any other type
I have spent a lot of time to write the Drawing Part, So it will be helpful if you can suggest a solution by changing the required parts of my code, instead of rewriting the whole code.
I suggest that you buffer your drawing operations with a BufferedImage, like so:
// This should not be done in the draw method, but rather when
// the frame is created. You should also make a new buffer image when
// the frame is resized. `frameWidth` and `frameHeight` are the
// frame's dimensions.
BufferedImage bufferImage = new BufferedImage(frameWidth, frameHeight,
BufferedImage.TYPE_INT_ARGB);
Graphics2D bufferGraphics = bufferImage.createGraphics();
// In your draw method, do the following steps:
// 1. Clear the buffer:
bufferGraphics.clearRect(0, 0, width, height);
// 2. Draw things to bufferGraphics...
// 3. Copy the buffer:
g2.drawImage(bufferImage, null, 0, 0);
// When you want to save your image presented in the frame, call the following:
ImageIO.write(bufferImage, "png", new File("frameImage.png"));
The Java Tutorial on Creating and Drawing to an Image, along with the ImageIO API reference might be helpful should you require more information.
Have a look at Writing/Saving an Image for more details, but essentially you can do something like...
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
// Draw what ever you want to to the graphics context
g2d.dispose();
ImageIO.write(img, "png", new File("My Awesome Drawing.png"));
If you can't separate the drawing logic from your panel, you could simply use the Graphics context from the BufferedImage to paint the the component with.
Have a look at Exporting a JPanel to an image and Print the whole program layout for examples

How to move everything in Graphics2D by x,y coordinates.

I need to move already made BufferedImage by x,y coordinates and then draw another things on it with Graphics2D object. I tried to use this code to do that :
Graphics2D g = img.createGraphics();
g.translate(x, y);
but it doesn't work. Is there any way to move everything in Graphics2D object and then draw on it or I have to use this code to do that:
BufferedImage temp = new BufferedImage(img.getWidth(),img.getHeight(),BufferedImage.TYPE_INT_ARGB);
Graphics2D g = temp.createGraphics();
g.drawImage(img,x, y,null);
Using this code and then drawing only few elements rather than making whole image from scratch isn't big leap in performance so I think making new BufferedImage then drawing image on it isn't best way. I would rather just create Graphics2D object from already made image and then just move it by a few pixels diagonally, but I couldn't find the way to do that.
From the Graphics2d docs when you use translate:
All coordinates used in subsequent rendering operations on this graphics context are relative to this new origin.
You are defining a transformation that affects future operations. After calling translate if you were to call a method on graphics like draw3DRect(0, 0, ... snipped ... ) the starting coordinates 0,0 would be translated by x,y.
I think your best bet might be to use the methods of BufferedImage to move all the pixels before you get the graphics object. You have getRgb and setRgb
A naive example of moving the pixels:
BufferedImage buffImg = ImageIO.read(img);
int width = buffImg.getWidth();
int horizontalOffset = 10;
int verticalOffset = 10;
int widthToMove = width - horizontalOffset;
int heightToMove = buffImg.getHeight() - verticalOffset;
int[] rgb = buffImg.getRGB(0, 0, widthToMove, heightToMove, null, 0, widthToMove);
buffImg.setRGB(horizontalOffset,verticalOffset,widthToMove, heightToMove,rgb, 0, widthToMove);
This still leaves you with some work to do because there is a strip at the top and to the left that you need to fill with background colour.
If it's going to be used on big images you might want to use a buffer int[] and pass it to getRGB in a loop, getting and setting in chunks.

Component painted to image renders with ugly fonts

When I'm rendering a Swing component to an image the fonts are different then when the component is rendered on screen. Here is an image showing the difference:
And this is the code:
public static BufferedImage renderComponent(Component component) {
int width = component.getWidth();
int height = component.getHeight();
BufferedImage buffImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D) buffImage.getGraphics();
g.setFont(component.getFont());
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
component.paint(g);
g.dispose();
return buffImage;
}
What should I change to make a perfect screen shot of a Swing component? (The app is running on Windows 7)
What should I change to make a perfect screen shot of a Swing component?
I would guess you should not be providing rendering hints.
I use the Screen Image class for this (it is basically your code with a few other features built in).
After comparing the ScreenImage class (suggested by camickr) I found the cause of the problem. If I create a BufferedImage with the type set to TYPE_INT_RGB instead of TYPE_INT_ARGB the fonts are fine.
Class Robot
public synchronized BufferedImage createScreenCapture(Rectangle screenRect)
?

Categories