How to create image in Java - java

say in my program, i have this paint() method. my wish is to create an image of the rectangles that are drawn (with the for loop). I tried the method below and it did give me those rectangles (blue color), but the background is all black. When I run program without creating image, just drawing the rect on a JFrame, the background is white. How can i fix this. ?
public void paint(Graphics g) {
super.paint(g);
BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
g = Image.getGraphics(); <<<----- is this correct?
g.setColor(Color.blue);
for ( ..... ) {
g.fillRect(X , Y, width , height);
....
}
try {
ImageIO.write(image, "jpg", new File("CustomImage.jpg"));
}catch (IOException e) {
e.printStackTrace();
}
}

The background is black in your image because you are not giving any pixels a value except those in the rectangles. The BufferedImage is starting out with every pixel having RGB of (0, 0, 0), which is black. To give the entire image a white background, simply fill the entire rectangle that is the image with white.
BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
g = image.createGraphics(); // not sure on this line, but this seems more right
g.setColor(Color.white);
g.fillRect(0, 0, 100, 100); // give the whole image a white background
g.setColor(Color.blue);
for( ..... ){
g.fillRect(X , Y, width , height );
....
}
Note that my answer is about writing the image to a file with a white background, not about drawing to the JFrame with a black background. I'm not entirely sure which one you wanted.

BufferedImage bufferedImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bufferedImage.createGraphics();
Font font = new Font("Georgia", Font.BOLD, 18);
g2d.setFont(font);
RenderingHints rh = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
rh.put(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHints(rh);
GradientPaint gp = new GradientPaint(0, 0,
Color.red, 0, height/2, Color.black, true);
g2d.setPaint(gp);
g2d.fillRect(0, 0, width, height);
g2d.setColor(new Color(255, 153, 0));

Try this
public void paint(Graphics g) {
super.paint(g);
BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
g = Image.createGraphics(); // it should be createGraphics
g.setBackground(Color.black);
g.setColor(Color.blue);
for( ..... ){
g.fillRect(X , Y, width , height );
....
}
try {
ImageIO.write(image, "jpg", new File("CustomImage.jpg"));
}catch (IOException e) {
e.printStackTrace();
}
}

It should be createGraphics.
http://docs.oracle.com/javase/tutorial/2d/images/drawonimage.html
..

Related

cropping in image into an cricle

I want to crop an image which is rectangular , into a circle of specific diameter. I am able to do it through graphics2D, and I get the image saved, but, when I read it through ImagIO, i get the full image again inspite to it being cropped to a circle. the image is a masked circle, and evrything outside is discarded like a mask. I am attaching the image here. inspite of it being clipped, i get the full image rendered, when i read it through imageIO. here is the code.
int w = bufferedImage.getWidth();
int h = bufferedImage.getHeight();
BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = output.createGraphics();
Area areaOval = new Area(new Arc2D.Double(0, 0, w, w, 0, 360,
Arc2D.PIE));
Shape shapeClipSave = g2.getClip();
g2.setClip(areaOval);
g2.drawImage(bufferedImage, 0, 0, null);
g2.setClip(shapeClipSave);
bufferedImage=output;
try {
ImageIO.write(bufferedImage,"png", new File("D:/new.png"));
bufferedImage= ImageIO.read(new File("D:/new.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
g2.dispose();
Here's my take. I rewrote some parts for performance and better fidelity (I couldn't get the edges of the circular area antialiased using clip). Although your code should also work, in general.
public static void main(String[] args) throws IOException {
BufferedImage image = ImageIO.read(new File(args[0]));
// Remove odd borders (imgur issue?)... Remove this if your input doesn't have borders
image = image.getSubimage(10, 0, image.getWidth() - 20, image.getHeight() - 10);
int w = image.getWidth();
int h = image.getHeight();
image = createCircular(image, Math.min(w, h));
if (!ImageIO.write(image, "png", new File("new.png"))) {
System.err.println("Could not write PNG format");
System.exit(1);
}
image = ImageIO.read(new File("new.png"));
showItAll(image);
}
private static BufferedImage createCircular(BufferedImage image, int size) {
BufferedImage output = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = output.createGraphics();
try {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.fillOval(0, 0, size, size);
g2.setComposite(AlphaComposite.SrcIn);
g2.drawImage(image, 0, 0, null);
}
finally {
g2.dispose();
}
return output;
}
private static void showItAll(BufferedImage image) {
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setContentPane(new JPanel() {
{
setBackground(Color.ORANGE);
}
});
frame.getContentPane().add(new JLabel(new ImageIcon(image)));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
Using your giraffe as input, I got the following output, the orange background is just to make the transparent parts clearly visible:
Alternatively, if you use the TwelveMonkeys library and the Adobe Path Support module, you can replace:
image = createCircular(image, Math.min(w, h));
with the following:
int size = Math.min(w, h);
image = Paths.applyClippingPath(new Ellipse2D.Float(0, 0, 1, 1),
image.getSubimage(0, 0, size, size));
Just be aware that the shape coordinates are relative to the size of the image, not in pixels.

Lines are angular

I want to paint on an image what the user is painting on the screen. For that I created an image array where the images are saved. That all works how it should. But if I draw a circle or some other round shapes it is shown very angular and not nearly round. My code:
private void onPress(java.awt.event.MouseEvent evt) {
images[actualLevel] = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g2d = (Graphics2D) images[actualLevel].getGraphics();
g2d.fillOval(evt.getX() - 200, evt.getY() - 200, 400, 400);
lastX = evt.getX();
lastY = evt.getY();
g2d = (Graphics2D) display.getGraphics();
g2d.drawImage(images[actualLevel], 0, 0, null);
g2d = (Graphics2D) paPaintingArea.getGraphics();
g2d.drawImage(display, 0, 0, paPaintingArea);
System.out.println(actualLevel);
}
private void onDrag(java.awt.event.MouseEvent evt) {
Graphics2D g2d = (Graphics2D) images[actualLevel].getGraphics();
g2d.setStroke(new BasicStroke(20));
g2d.drawLine(lastX, lastY, evt.getX(), evt.getY());
lastX = evt.getX();
lastY = evt.getY();
g2d = (Graphics2D) display.getGraphics();
g2d.drawImage(images[actualLevel], 0, 0, null);
g2d=(Graphics2D)paPaintingArea.getGraphics();
g2d.drawImage(display, 0, 0,paPaintingArea);
}
display is the image that is painted on the panel after saving the image in the array.
Why are my lines so angular and how can I fix it?

how to rescale binary character image?

http://cafefiles.naver.net/20130606_179/rise1925_1370463189365uKV60_PNG/%C1%A6%B8%F1_%BE%F8%C0%BD.png
I tried to resize character images by bicubic algorithm in Java.
but as you can see it linked , connectivity of image has broken...
I just made code..
public BufferedImage scaleImage(BufferedImage img, int width, int height,
Color background) {
BufferedImage newImage = new BufferedImage(width, height,
BufferedImage.TYPE_BYTE_BINARY);
Graphics2D g = newImage.createGraphics();
try {
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
//g.setBackground(background);
//g.setColor(Color.BLACK);
g.clearRect(0, 0, width, height);
g.drawImage(img, 0, 0, width, height, this);
} finally {
g.dispose();
}
return newImage;
}
what am i doing wrong?
Originaly I was just going to say use .getScaledInstance(), but I read an ardicle on good practices with the java Image and Buffered Image api.
https://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html
And this is the sorce code that I think you need
public static BufferedImage scaleImage(BufferedImage img, int width, int height) {
BufferedImage newImage = new BufferedImage(width, height,img.getType());
Graphics2D g = newImage.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g.clearRect(0, 0, width, height);
g.drawImage(img, 0, 0, width, height, null);
g.dispose();
return newImage;
}
You were setting the new re-sized image to be a binary (read as black and white ie 1 and 0) image. It is a good practice to always set a re-scaled image to the same type as the image the re-scaling is based off and to change its type with a conversion method.

BufferedImage from JPanel with transparent background

i'm trying to display an image in my onPaint method, but keep getting a black background. The image i display is the result of a JPanel.print(Graphics) or JPanel.paint(Graphics). So i convert this result to a png file, which is stored in a database. Later this image is loaded and transformed by the user. This process gives me a black background.
Here's the code:
First the image creation:
public static BufferedImage createBufferedImageFromView(JPanel panel){
int w = panel.getWidth();
int h = panel.getHeight();
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();
g.setBackground(new Color(0, 0, 0, 0));
g.clearRect(0, 0, bi.getWidth(), bi.getHeight());
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN, 0.0f));
panel.setOpaque(false);
panel.repaint();
panel.print(g);
return bi;
}
then i paint it in the onPaint() method:
public void paint(Graphics g) {
super.paint(g);
ParameterBlock params = new ParameterBlock();
params.addSource(previewImage); //source is the input image
int w = previewImage.getWidth(); //Set to the original width of the image
int h = previewImage.getHeight(); //Set to the original height of image
Point tl = new Point((int)cornersTrans[0].getX(), (int)cornersTrans[0].getY()); //The new top left corner
Point tr = new Point((int)cornersTrans[1].getX(), (int)cornersTrans[1].getY()); //The new top right corner
Point bl = new Point((int)cornersTrans[2].getX(), (int)cornersTrans[2].getY()); //The new bottom left corner
Point br = new Point((int)cornersTrans[3].getX(), (int)cornersTrans[3].getY()); //The new bottom right corner
try {
params.add(new WarpPerspective(PerspectiveTransform.getQuadToQuad(0,0, 0, h, w, h, w, 0, tl.x, tl.y, bl.x, bl.y, br.x, br.y, tr.x, tr.y).createInverse()));
} catch (NoninvertibleTransformException e) {
e.printStackTrace();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
if(!isInMovement)
params.add(Interpolation.getInstance(Interpolation.INTERP_BICUBIC));
PlanarImage image = JAI.create("warp", params);
int y0 = (int) (cornersTrans[0].getY() < cornersTrans[1].getY() ? cornersTrans[0].getY() : cornersTrans[1].getY());
int x0 = (int) (cornersTrans[0].getX() < cornersTrans[2].getX() ? cornersTrans[0].getX() : cornersTrans[2].getX());
int x1 = (int) (cornersTrans[3].getX() > cornersTrans[1].getX() ? cornersTrans[3].getX() : cornersTrans[1].getX());
int y1 = (int) (cornersTrans[3].getY() > cornersTrans[2].getY() ? cornersTrans[3].getY() : cornersTrans[2].getY());
Graphics2D g2d = (Graphics2D) g;
// g2d.setBackground(new Color(0, 255, 0, 0) );
// g2d.clearRect(0, 0, 9999, 9999);
g2d.drawImage((Image) image.getAsBufferedImage(), x0,y0,x1, y1, 0, 0, image.getWidth(), image.getHeight(), null);
Any idea of how to fix the black background?
Thanks!

java get color of pixel for transparent, gradient overlay

In my application I have an image (world map) as background picture. Over this background picture, there is a polygon with a color gradient and transparent filling effect.
Here you find a code snippet of the overlay:
public void paint(Graphics g) {
//draw a polygon with a gradient filling effect
Graphics2D g2 = (Graphics2D)g;
GradientPaint gp = new GradientPaint(x1, y1, color1, x2, y2, color2, false);
g2.setPaint(gp);
g2.fill(polygon);
}
Does somebody know a method to get the color of one pixel of the overlay? I don't need the color, which can be seen on the screen including the background picture - just the color of the overlay.
Best regards,
Michael
This is somewhat ugly, but works:
GradientPaint gp = new GradientPaint(0, 0, new Color(255, 0, 0, 50),
10, 10, new Color(128, 255, 0, 150));
ColorModel cm = ColorModel.getRGBdefault();
Rectangle r = new Rectangle(0, 0, 10, 10);
Raster raster = gp.createContext(cm, r, r, new AffineTransform(), null)
.getRaster(0, 0, 10, 10);
int[] rgba = raster.getPixel(5, 5, (int[])null);
Alternately, you could just paint the overlay into a BufferedImage (which you had first cleared to transparent).

Categories