I need to make a JButton with an image icon and regular text. This question is not duplicate of How to make JButton with transparent background and regular text?, as I need to upload an image as icon and make it transparent as well. I tried to use overriden paintComponent() method
#Override
public void paintComponent(java.awt.Graphics g) {
java.awt.Graphics2D g2 = (java.awt.Graphics2D) g;
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
super.paintComponent(g2);
}
But all it does is paints icon and text both transparent, also button does not refresh properly.
Are there any possible workarounds?
UPDATE
The way I am setting the button is the following (item.getImage() returns array of bytes):
setFocusable(true);
setFocusPainted(true);
setVerticalTextPosition(SwingConstants.CENTER);
setHorizontalTextPosition(SwingConstants.CENTER);
if(item.getImage() != null) {
int w = BUTTON_SIZE - 10;
int h = BUTTON_SIZE - 10;
if(menuItem.isShowImageOnly()) {
setIcon(menuItem.getScaledImage(w, h));
}
else {
w = 80;
h = 40;
setIcon(menuItem.getScaledImage(w, h));
}
Just draw the transparency on the Image first
Image im = ...;
java.awt.Graphics2D g2 = (java.awt.Graphics2D) im.getGraphics();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
g2.drawImage(0,0, im, null);
g2.dispose();
ImageIcon icon = ...
Related
I have a function that draws a rotated picture on a g2 component but for some reason I can't give it any colored background...
I was trying to use methods .setColor() and .setBackground() but with no use.
I have seen a similiar case here Graphics2D: Drawing black on white?
but it didn't really help. Here is my function:
public void rotateImage1(double degrees, ImageObserver o){
double sin = Math.abs(Math.sin(Math.toRadians(degrees)));
double cos = Math.abs(Math.cos(Math.toRadians(degrees)));
ImageIcon icon = new ImageIcon(this.spiral);
int w = icon.getIconWidth();
int h = icon.getIconHeight();
int neww = (int)Math.floor(w*cos+h*sin);
int newh = (int)Math.floor(h*cos+w*sin);
BufferedImage blankCanvas = new BufferedImage(neww, newh, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = (Graphics2D)blankCanvas.getGraphics();
if(PhotoEdit.black)
g2.setColor(Color.BLACK);
else
g2.setColor(Color.WHITE);
g2.translate((neww-w)/2, (newh-h)/2);
g2.rotate(Math.toRadians(degrees), icon.getIconWidth()/2, icon.getIconHeight()/2);
g2.drawImage(this.spiral, 0, 0, o);
this.spiral = blankCanvas;
}
PhotoEdit.black is a boolean variable that is true if the user selected the checkbox with black background option.
You are setting the color in graphics, but you aren't drawing it to the panel.
You can use g2.setColor(...) and g2.fillRect(...) and specify coordinates that cover the whole panel, and then draw your image on top.
Docs for fillRect: http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#fillRect%28int,%20int,%20int,%20int%29
Is there anyway to find the current position of a buffered image on jpanel?
I draw an image on buffer, named it currentImage. Now I want to move it around a Panel by using affine transform.
To do translation by using mouse, I have to know the current position, new position and update the new position.
But I have trouble in getting the current position of the BufferedImage. There is no method in the IDE's suggestion working.
Can anyone give me an idea how to do this ?? Thanks!
EDIT
this is my draw and paintComponent method:
private void draw(){
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC);
int w = this.getWidth(); int h = this.getHeight();
if (currentImage == null || width != w || height != h){
width = w;
height = h;
currentImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
graphics = currentImage.createGraphics();
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
drawClearAndGradedArea(graphics);
drawActualRunway(graphics, width, height, width, height);
drawLeftToRight(graphics);
drawRightToLeft(graphics);
drawCentralLine(graphics);
graphics.setComposite(ac);
}
}
paintComponent()
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.clearRect(0,0, this.getWidth(), this.getHeight());
RenderingHints rh = new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHints(rh);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
/*
* main components on panel
*/
this.draw();
this.animation();
g2d.drawImage(currentImage, transform, this);
drawNote(g2d);
g2d.dispose();
}
In a project, I want to simultaneously resize and change the opacity of an image. So far I think I've got the resizing down. I use a method defined like so to accomplish the resizing:
public BufferedImage resizeImage(BufferedImage originalImage, int type){
initialWidth += 10;
initialHeight += 10;
BufferedImage resizedImage = new BufferedImage(initialWidth, initialHeight, type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, initialWidth, initialHeight, null);
g.dispose();
return resizedImage;
}
I got this code from here. What I can't find a solution to is changing the opacity. That's what I'm wondering how to do (if it's possible at all). Thanks in advance.
UPDATE:
I tried this code to display a picture of a circle with transparent insides and outsides (see below image) growing and becoming less and less opaque, but it didn't work. I'm not sure what's wrong. All the code is in a class called Animation
public Animation() throws IOException{
image = ImageIO.read(new File("circleAnimation.png"));
initialWidth = 50;
initialHeight = 50;
opacity = 1;
}
public BufferedImage animateCircle(BufferedImage originalImage, int type){
//The opacity exponentially decreases
opacity *= 0.8;
initialWidth += 10;
initialHeight += 10;
BufferedImage resizedImage = new BufferedImage(initialWidth, initialHeight, type);
Graphics2D g = resizedImage.createGraphics();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity));
g.drawImage(originalImage, 0, 0, initialWidth, initialHeight, null);
g.dispose();
return resizedImage;
}
I call it like this:
Animation animate = new Animation();
int type = animate.image.getType() == 0? BufferedImage.TYPE_INT_ARGB : animate.image.getType();
BufferedImage newImage;
while(animate.opacity > 0){
newImage = animate.animateCircle(animate.image, type);
g.drawImage(newImage, 400, 350, this);
}
first make sure the type you're passing into to method contains an alpha channel, like
BufferedImage.TYPE_INT_ARGB
and then just before you paint the new image, call the Graphics2D method setComposite like so:
float opacity = 0.5f;
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity));
that will set the drawing opacity to 50%.
I'm working with an application in which I add a heavyweight (Canvas) to a JFrame. The Canvas is a 3rd party component, so I am required to keep it heavyweight. I'd like to add capabilities for the user to draw on the canvas and paint a selection rectangle.
I don't think I can do this with the glass pane since the heavyweight canvas will be displayed over the glass pane. I've tried adding a mouse listener to the canvas and drawing directly on its graphics, but that seems to give the "flicker" effect since it's not a lightweight double-buffered component.
Is there a way to achieve this smooth drawing on heavyweight components?
This is my current attempt in the paint method of the heavyweight component, but there is still the flashing.
#Override
public void paint(Graphics g)
{
super.paint(g);
if (showUserSelection)
{
Point startDrawPoint = new Point(Math.min(startSelectPoint.x,
endSelectPoint.x), Math.min(startSelectPoint.y,
endSelectPoint.y));
Point endDrawPoint = new Point(Math.max(startSelectPoint.x,
endSelectPoint.x), Math.max(startSelectPoint.y,
endSelectPoint.y));
int w = endDrawPoint.x - startDrawPoint.x;
int h = endDrawPoint.y - startDrawPoint.y;
if (w > 0 && h > 0)
{
BufferedImage img = new BufferedImage(w, h,
BufferedImage.TYPE_INT_ARGB);
Graphics2D imgGraphics = img.createGraphics();
imgGraphics.fillRect(0, 0, w, h);
g.drawImage(img, startDrawPoint.x, startDrawPoint.y, w, h,
null);
}
}
}
I'm not aware of a drop in "Host AWT/SWT in Swing" component, but you should be able to build a work around yourself.
Have you considered implementing double buffering yourself?
You're 90% of the way there with your code, just at a glance.
#Override
public void paint(Graphics g)
{
BufferedImage buffer = new BufferedImage(COMPONENT_WIDTH, COMPONENT_HEIGHT, BufferedImage.TYPE_INT_ARGB);
Graphics bufferG = buffer.getGraphics();
super.paint(bufferG);
if (showUserSelection)
{
Point startDrawPoint = new Point(Math.min(startSelectPoint.x,
endSelectPoint.x), Math.min(startSelectPoint.y,
endSelectPoint.y));
Point endDrawPoint = new Point(Math.max(startSelectPoint.x,
endSelectPoint.x), Math.max(startSelectPoint.y,
endSelectPoint.y));
int w = endDrawPoint.x - startDrawPoint.x;
int h = endDrawPoint.y - startDrawPoint.y;
if (w > 0 && h > 0)
{
bufferG.fillRect(startDrawPoint.x, startDrawPoint.y, w, h);
}
}
g.drawImage(buffer, 0, 0, null);
}
Is there a way to convert a JPanel (that has not yet been displayed) to a BufferedImage?
thanks,
Jeff
From the BufferedImage you can create a graphics object, which you can use to call paint on the JPanel, something like:
public BufferedImage createImage(JPanel panel) {
int w = panel.getWidth();
int h = panel.getHeight();
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
panel.paint(g);
g.dispose();
return bi;
}
You may need to make sure you set the size of the panel first.
Basically I'm building a component
that needs to get written to an image
but not displayed
ScreenImage explains how to do what you want.
Relevant section of ScreenImage.java (slightly edited). layoutComponent forces all buttons appear in the image.
/**
* #return Renders argument onto a new BufferedImage
*/
public BufferedImage createImage(JPanel panel, int width, int height) {
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();
panel.setSize(width, height); // or panel.getPreferedSize()
layoutComponent(panel);
panel.print(g);
g.dispose();
return bi;
}
private void layoutComponent(Component component) {
synchronized (component.getTreeLock()) {
component.doLayout();
if (component instanceof Container) {
for (Component child : ((Container) component).getComponents()) {
layoutComponent(child);
}
}
}
}
The answer from Tom is basically correct, but invoke paint() directly is not recommended, as it is a synchronous call and can interrupt with other operation on the swing thread. Instead of using paint(), we should use print() instead
public BufferedImage createImage(JPanel panel) {
int w = panel.getWidth();
int h = panel.getHeight();
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
panel.print(g);
g.dispose();
return bi;
}
Take a look at BasicTableUI. The cell renderer is drawn on image without showing and then drawn on visible table component.