I think something is wrong with paint method, but I can't figure it out.
public void paint(Graphics g) {
screenImage = createImage(1280, 720);
screenGraphic = screenImage.getGraphics();
screenDraw(screenGraphic);
g.drawImage(screenImage, 0, 0, null);
}
public void screenDraw(Graphics g) {
g.drawImage(BG, 0, 0, null);
if(isMainScreen) {
g.drawImage(changedImageAlpha(selectedImage, 120), 130, 360, null);
}
paintComponents(g);
this.repaint();
}
public Image changedImageAlpha(Image image, int trans) {
BufferedImage img = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img.createGraphics();
Composite c = AlphaComposite.getInstance( AlphaComposite.SRC_ATOP, .5f);
g.setComposite(c);
g.drawImage(image, 0, 0, null);
g.dispose();
int colorMask = 0x00FFFFFF;
int alphaShift = 24;
for(int y=0; y<img.getHeight(); y++){
for(int x=0; x<img.getWidth(); x++) {
img.setRGB(x, y, (img.getRGB(x, y) & colorMask) | (trans << alphaShift));
}
}
return img;
}
BG is an Image object, an also is screenImage.
I expected image to be a transparent image, and yes.
I see some transparent image, but nothing in it. It's just a clear transparent image, no color, nothing.
What could be the problem?
As a comment to the answer to your previous question, I mentioned there was an easier than the bit manipulation suggested in the answer.
But I meant instead of not in addition to!
So this:
public Image changedImageAlpha(Image image, int trans) {
BufferedImage img = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img.createGraphics();
Composite c = AlphaComposite.getInstance( AlphaComposite.SRC_ATOP, .5f);
g.setComposite(c);
g.drawImage(image, 0, 0, null);
g.dispose();
int colorMask = 0x00FFFFFF;
int alphaShift = 24;
for(int y=0; y<img.getHeight(); y++){
for(int x=0; x<img.getWidth(); x++) {
img.setRGB(x, y, (img.getRGB(x, y) & colorMask) | (trans << alphaShift));
}
}
return img;
}
Should be this:
public Image changedImageAlpha(Image image, int trans) {
BufferedImage img = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img.createGraphics();
Composite c = AlphaComposite.getInstance( AlphaComposite.SRC_ATOP, .5f);
g.setComposite(c);
g.drawImage(image, 0, 0, null);
g.dispose();
return img;
}
And as a tip: Try to understand the code others are providing. 'Cut & paste' programming usually ends in failure.
Related
I draw graphics in frame with this methods.
public void paint(Graphics g) {
screenImage = createImage(1280, 720);
screenGraphic = screenImage.getGraphics();
screenDraw((Graphics2D) screenGraphic);
//g.drawImage(BG, 0, 0, null);
g.drawImage(screenImage, 0, 0, null);
}
public void screenDraw(Graphics2D g) {
g.drawImage(BG, 0, 0, null);
Graphics2D g2 = (Graphics2D)g;
if(isMainScreen) {
//g2.setComposite(alphaComposite);
g2.drawImage(selectedImage, 100, 220, null);
}
paintComponents(g);
this.repaint();
}
I want to have selectedImage to be transparency 50% or other integer.
private Image selectedImage = new ImageIcon(Main.class.getResource("../pic/something.jpg")).getImage();
this is selectedImage, and it works well.
Adapt a bit of Image -> BufferedImage code, and then you can set each pixel to be transparent
Image selectedImage = new ImageIcon([...]).getImage();
//Image -> BufferedImage code
BufferedImage img = new BufferedImage(selectedImage.getWidth(null), selectedImage.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img.createGraphics();
g.drawImage(selectedImage, 0, 0, null);
g.dispose();
//set each pixel to be transparent
int transparency = 127; //0-255, 0 is invisible, 255 is opaque
int colorMask = 0x00FFFFFF; //AARRGGBB
int alphaShift = 24;
for(int y = 0; y < img.getHeight(); y++)
for(int x = 0; x < img.getWidth(); x++)
img.setRGB(x, y, (img.getRGB(x, y) & colorMask) | (transparency << alphaShift));
//img is now transparent, can replace selectedImage if you want (optional)
selectedImage = img;
If you use ImageIO.read(File) instead of ImageIcon.getImage(), it will give you a BufferedImage directly
I need to draw a new image over old image. I first opened both images in BufferedImage and changed their white background to transparent. Then I got a Graphics2D object from the bufferedImage of old image and called drawImage method of Graphics2D class. I then saved the old image to disk. When I open the saved image I find only the old image with white background changed to transparent. Can anyone suggest me what is error with my code or how can I get to fix my error ?
BufferedImage newImage = ImageIO.read(new File("new.png"));
BufferedImage oldImage = ImageIO.read(new File("old.png"));
newImage = makeWhiteTransparent(newImage);
oldImage = makeWhiteTransparent(oldImage);
Graphics2D graphics = (Graphics2D) oldImage.getGraphics();
graphics.drawImage(newImage,null, 0,0);
File outputImage = new File("merged.png");
ImageIO.write(oldImage, "png", outputImage);
My makeWhiteTransparent method goes like this:
public static BufferedImage makeWhiteTransparent(BufferedImage img){
BufferedImage dst = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
dst.getGraphics().drawImage(img, 0, 0, null);
int markerRGB = Color.WHITE.getRGB() | 0xFF000000;
int width = dst.getWidth();
int height = dst.getHeight();
for(int x = 0; x < width; x++){
for(int y = 0; y < height; y++){
int rgb = dst.getRGB(x, y);
if ( ( rgb | 0xFF000000 ) == markerRGB ) {
int value = 0x00FFFFFF & rgb;
dst.setRGB(x, y, value);
}
}
}
return dst;
}
I tried changing graphics.drawImage(newImage, null,0,0) to graphics.drawImage(newImage, 0,0, null) and also changing TYPE_4BYTE_ABGR to TYPE_INT_ARGB as suggested but it did nothing. The error still exists.
This needs to be changed:
graphics.drawImage(newImage,null, 0,0);
to
graphics.drawImage(newImage, 0,0, null);
you are using the wrong version of drawImage - check http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html
--
Change also the type TYPE_4BYTE_ABGR to TYPE_INT_ARGB
--
Here's how it works for me:
public BufferedImage makeWhiteTransparent(BufferedImage img){
BufferedImage dst = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
dst.getGraphics().drawImage(img, 0, 0, null);
int markerRGB = 0x00ffffff; // Color.WHITE.getRGB() | 0xFF000000;
int width = dst.getWidth();
int height = dst.getHeight();
for(int x = 0; x < width; x++){
for(int y = 0; y < height; y++){
int rgb = dst.getRGB(x, y)&0x00ffffff;
if ( rgb == markerRGB ) {
int value = 0x00FFFFFF & rgb;
dst.setRGB(x, y, value);
}
}
}
return dst;
}
bim = makeWhiteTransparent(bim);
bim2 = makeWhiteTransparent(bim2);
Graphics2D graphics = (Graphics2D) bim.getGraphics();
graphics.drawImage(bim2,0,0, null);
g2.drawImage(bim, w/2-wc/2, h/2-hc/2, null);
I got the answer to my question finally. All I had to do was create a new BufferedImage and draw two images over it. Below is the code that works as expected:
BufferedImage newImage = ImageIO.read(new File("new.png"));
BufferedImage oldImage = ImageIO.read(new File("old.png"));
oldImage = makeWhiteTransparent(oldImage);
newImage = makeWhiteTransparent(newImage);
int width = Math.max(newImage.getWidth(), oldImage.getWidth());
int height = Math.max(newImage.getHeight(), oldImage.getHeight());
BufferedImage combined = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics graphics = combined.getGraphics();
graphics.drawImage(oldImage, 0, 0, null);
graphics.drawImage(newImage, 0, 0, null);
File outputImage = new File("merged.png");
ImageIO.write(combined, "PNG", outputImage);
Hello) Help solve the problem:
We need to create a green square image and display it.
I could draw a square, but I need to create it using java.
Please help me to do this)
That's what I tried to do:
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
public class Game extends Canvas {
private static final long serialVersionUID = 1L;
private static final int WIDTH = 400;
private static final int HEIGHT = 400;
#Override
public void paint(Graphics g) {
super.paint(g);
int w = 10;
int h = 10;
int type = BufferedImage.TYPE_INT_ARGB;
BufferedImage image = new BufferedImage(w, h, type);
int color = 257; // RGBA value, each component in a byte
for (int x = 1; x < w; x++) {
for (int y = 1; y < h; y++) {
image.setRGB(x, y, color);
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(WIDTH, HEIGHT);
frame.add(new Game());
frame.setVisible(true);
}
}
But nothing is displayed (
Let me remind the goal - to create a picture in the form of green squares, help to make it)
The simplest approach would be to simply use the graphics API...
#Override
public void paint(Graphics g) {
super.paint(g);
int w = 10;
int h = 10;
g.setColor(Color.GREEN);
g.fillRect(0, 0, width, height);
}
But something tells me this isn't what you want, but it does form the basics for what you need to achieve your result.
Start by making image a instance field...
private BufferedImage image;
Then you need to create the image...
int type = BufferedImage.TYPE_INT_ARGB;
image = new BufferedImage(w, h, type);
Graphics2D g2d = image.createGraphics();
g2d.setColor(Color.GREEN);
g2d.fillRect(0, 0, w, h);
g2d.dispoe();
Then in you paint method, you need to draw the image...
g.drawImage(image, x, y, this);
Take a look at the 2D Graphics trail for mor details
You must use Graphics.drawRect() and Graphics.fillRect():
http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#drawRect%28int,%20int,%20int,%20int%29
http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#fillRect%28int,%20int,%20int,%20int%29
if you want to create an Image with rectangle containing it, first create the image, draw on it using it's graphics. I am writing code snippets for you:
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics g = image.getGraphics();
g.setColor(Color.blue);
g.fillRect(0, 0, image.getWidth(), image.getHeight());
This will produce an image with Blue rectangle.
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!
I'm having a problem getting an image to flip. My program is supposed to show the default image and the flipped image. I thought that if I could replace the (0,0) pixel of the flipped picture with the (width-1,height-1) of the original picture, it would work, but instead of getting the original image, I get this.
Here is my code:
import java.awt.Color;
public class Horizontal {
public static void main(String[] args)
{
Picture source = new Picture(args[0]);//name of picture.
Picture flip = new Picture(source.width(), source.height());//sets the width and height of source
for (int i =0; i < source.width(); i++)
{
int w = 1;
int sw = source.width()-w;
for (int j = 0; j < source.width(); j++)
{
int h=1;
int sh = source.height()-h;
Color SourceColor = source.get(sw,sh);// return the the color pixel of (sw,sh)
flip.set(i, j, SourceColor);//suppose to replace the (i,j) pixel of flip with source's (sw,sh) pixel
h++;
}
w++;
}
source.show();// shows the original image
flip.show(); // shows flipped version of image
}
}
Chceck this site. It has great info about basic image algorithms in java.
You can copy code for flipping an image.
http://www.javalobby.org/articles/ultimate-image/#9
Flipping horizontally:
public static BufferedImage horizontalflip(BufferedImage img) {
int w = img.getWidth();
int h = img.getHeight();
BufferedImage dimg = new BufferedImage(w, h, img.getType());
Graphics2D g = dimg.createGraphics();
g.drawImage(img, 0, 0, w, h, w, 0, 0, h, null);
g.dispose();
return dimg;
}
Flipping vertically:
public static BufferedImage verticalflip(BufferedImage img) {
int w = img.getWidth();
int h = img.getHeight();
BufferedImage dimg = dimg = new BufferedImage(w, h, img.getColorModel().getTransparency());
Graphics2D g = dimg.createGraphics();
g.drawImage(img, 0, 0, w, h, 0, h, w, 0, null);
g.dispose();
return dimg;
}