How to remove Background of Jpanel while converting image (png). I tried following line to remove the background of the panel but The converted image background become black.
g2.clearRect(0, 0, getWidth(), getHeight());
draw a filled rectangle with the background color...
Color backGround = getBackGround();
g2.setColor(background);
g2.fillRect(0,0,getWidth(), getHeight() );
Related
How can I draw a filled oval of color red on a Graphics object created with BufferedImage which is filled with the color black?
What I have tried:
public void draw(){
BufferedImage bufferedImage = new BufferedImage(4, 5, BufferedImage.TYPE_INT_ARGB);
Graphics g = bufferedImage.createGraphics();
g.setColor(Color.BLACK);
g.fillRect(0, 0, 4, 5);
g.setColor(Color.RED);
g.fillOval(1, 1, 2, 2);
g.dispose();
}
The result is a filled red rectangle in a filled black rectangle:
But I want that filled red rectangle to be a filled red oval. How can I do that?
I want to use that image as a mouse cursor.
It looks like a red rectangle because it is only 1 pixel tall and 2 pixels wide. Since there isn't enough space to simulate a curve, you won't get one. Try a bigger oval like g.fillOval(100, 100, 200, 200);
I want to create an Image object which is absolutely transparent. The JFrame method
createImage(int x, int y);
is totally useless, because it creates a white rectangle, not a transparent one. In the past, I loaded in a PNG file that was transparent, but there must be a simpler way to do it.
Have you looked at BufferedImage? It is a base class for working with images in standard java. It supports ARGB image types that includes alpha channel (transparency) along with standard RGB.
You can try something like this to create transparent image:
BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
Graphics gr = image.getGraphics();
gr.setColor(new Color(0, 0, 0, 0)); // last component is alpha channel.
// 0 - transparent, 255 - opaque
gr.fillRect(0, 0, 100, 100):
// Now image is transparent
I didn't test it but it should work.
I have made a JLabel where I display my images like this:
BufferedImage myimage;
imageLabel.setIcon(new ImageIcon(myimage));
Is it possible to draw an image and draw upon it a smaller image (an icon) with the setIcon command? How can I do it?
For example:
BufferedImage myimage1;
BufferedImage myLittleIcon;
imageLabel.setIcon(new ImageIcon(myimage1));
imageLabel.setIcon(new ImageIcon(myLittleIcon));
The above just draws the small icon.
Calling setIcon would overwrite the icon. However, you could try something like this:
// Assumed that these are non-null
BufferedImage bigIcon, smallIcon;
// Create a new image.
BufferedImage finalIcon = new BufferedImage(
bigIcon.getWidth(), bigIcon.getHeight(),
BufferedImage.TYPE_INT_ARGB)); // start transparent
// Get the graphics object. This is like the canvas you draw on.
Graphics g = finalIcon.getGraphics();
// Now we draw the images.
g.drawImage(bigIcon, 0, 0, null); // start at (0, 0)
g.drawImage(smallIcon, 10, 10, null); // start at (10, 10)
// Once we're done drawing on the Graphics object, we should
// call dispose() on it to free up memory.
g.dispose();
// Finally, convert to ImageIcon and apply.
imageLabel.setIcon(new ImageIcon(finalIcon));
This creates a new image, paints the big icon, and then paints the small icon.
You can also paint other things, like outlining a rectangle or filling an oval.
For more advanced graphics functions, try casting to a Graphics2D object.
I am trying to create a BufferedImage instance which contains a rounded rectangle of a certain colour and is transparent everywhere else.
I am using the following code to create the image
private BufferedImage createChromImage() {
BufferedImage I = new BufferedImage(350, 20, ColorSpace.TYPE_RGB);
Graphics2D g2 = I.createGraphics();
g2.setPaint(new GradientPaint(0, 0, Color.DARK_GRAY, 100,
100, Color.BLUE, false));
g2.fillRoundRect(0, 0, 350, 20, 10, 10);
return I;
}
I end up with a rounded rectangle on a black background, is there a way in which I can get it on a transparent background. I suspect it will require a different ColorSpace setting, but I not sure which.. any help is much appreciated.
You can't have a transparent background in an image that does not support transparency. RGB is a 24-bit image with no transparency. Instead, you want to use BufferedImage.TYPE_INT_ARGB as the argument to BufferedImage's constructor: that will give you an alpha channel to play with, which will allow transparency.
Could someone provide an example of how to dynamically create an image in Java, draw lines et cetera on it, and then draw the image so that areas not painted will remain transparent in the drawing process?
One could use a BufferedImage with an image type that supports transparency such as BufferedImage.TYPE_INT_ARGB:
BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
One can draw on the BufferedImage by calling BufferedImage.createGraphics to obtain a Graphics2D object, then perform some drawing:
BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img.createGraphics();
g.drawLine(0, 0, 10, 10); // draw a line.
g.dispose();
Then, since BufferedImage is a subclass of Image that can be used to draw onto another Image using one of the Graphics.drawImage that accepts an Image.