How can I fill the following rectangle using an image? Can anyone help me please?
public void paintComponent(Graphics g) {
setOpaque(false);
//Paint a filled rectangle at user's chosen point.
if (point != null) {
g.drawRect(0, 0,
rectWidth - 1, rectHeight - 1);
g.setColor(Color.yellow);
g.fillRect(1, 1,
rectWidth - 2, rectHeight - 2);
}}
I tried this code but I couldn't find a way to make it work:
File imageFile = new File("duck.jpg");
BufferedImage img;
Graphics2D graph = img.createGraphics();
graph.setColor(Color.BLACK);
graph.fill(new Rectangle(1, 2, rectWidth, rectHeight));
graph.dispose();
ImageIO.write(img, "jpg", new File("duck.jpg"));
You have to load an image into an Image object (like BufferedImage) and then call
graphics.drawImage()
on that image, giving the coordinates and other info.
Look in the tutorial for more info
Related
I have a image of dimension 215*112. I want to make it 215*142.
Src Img:
I used the following code:
BufferedImage image = ImageIO.read(new File("src.png"));
int h = 15;
BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight() + 2 * h, image.getType());
Graphics g = newImage.getGraphics();
g.setColor(Color.red);
g.fillRect(0, 0, image.getWidth(), image.getHeight() + 2 * h);
g.drawImage(image, 0, h, null);
g.dispose();
ImageIO.write(newImage, "png", new File("dest.png"));
I am getting following result:
Why the padding is also getting added to x direction?
Because the source you are giving has two transparent vertical bars on the left and right of the image
Use this image
This code below is my paintComponent for painting image objects, but while I want to be able rotate the image with out moving it about the center but also translate it according to a certain value.
trans.translate(xShift, yShift);
this is the line that I tried to use to translate my image, but it cuts off part of the image every time for some reason.
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (myImage != null) {
AffineTransform trans = new AffineTransform();
trans.translate(getWidth() / 2, getHeight() / 2);
trans.rotate(piece.getOrientation() * Math.PI / 2);
trans.translate(-myImage.getWidth() / 2, -myImage.getHeight() / 2);
trans.translate(xShift, yShift);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(myImage, trans, null);
}
}
My sense is that java has a problem with compounding transforms.
Maybe you can find another approach: e.g. transform the image itself, then apply a simple translation:
BufferedImage b22=new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2=(Graphics2D)b22.getGraphics();
AffineTransform trans = new AffineTransform();
trans.translate(getWidth() / 2, getHeight() / 2);
trans.rotate(Math.PI / 2);
trans.translate(-bim.getWidth() / 2, -bim.getHeight() / 2);
g2.drawImage(bim, trans, null);
AffineTransform trans2 = new AffineTransform();
trans2.translate(xoffset, yoffset);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(b22, trans2, null);
// or just g2d.drawImage(b22, xoffset, yoffset, null);
Of course it will cut of if you translate a certain extend.
I am trying to achieve the following
http://www.qksnap.com/i/3hunq/4ld0v/screenshot.png
I am currently able to draw rectangles successfully on a semi-transparent glasspane background using the following code:
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g.setColor(Color.black); // black background
g.fillRect(0, 0, frame.getWidth(), frame.getHeight());
g2.setColor(Color.GREEN.darker());
if (getRect() != null && isDrawing()) {
g2.draw(getRect()); // draw our rectangle (simple Rectangle class)
}
g2.dispose();
}
Which works great, however, I would love to have the area within the rectangle be completely transparent while the outside was still darken much like the screenshot above.
Any ideas?
..have the area within the rectangle be completely transparent while the outside was still darken much like the screenshot above.
Create a Rectangle (componentRect) that is the size of the component being painted.
Create an Area (componentArea) of that shape (new Area(componentRect)).
Create an Area (selectionArea) of the selectionRectangle.
Call componentArea.subtract(selectionArea) to remove the selected part.
Call Graphics.setClip(componentArea)
Paint the semi-transparent color.
(Clear the clipping area if more paint operations are required).
As Andrew has suggested (just beat me while I was finishing off my example)
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
g.setColor(Color.black); // black background
Area area = new Area();
// This is the area that will filled...
area.add(new Area(new Rectangle2D.Float(0, 0, getWidth(), getHeight())));
g2.setColor(Color.GREEN.darker());
int width = getWidth() - 1;
int height = getHeight() - 1;
int openWidth = 200;
int openHeight = 200;
int x = (width - openWidth) / 2;
int y = (height - openHeight) / 2;
// This is the area that will be uneffected
area.subtract(new Area(new Rectangle2D.Float(x, y, openWidth, openHeight)));
// Set up a AlphaComposite
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
g2.fill(area);
g2.dispose();
}
I'm trying to rotate an image, but it's getting slightly messed up when I'm rotating it, and it looks like it's not rotating it on center. So if I go around it looks like it's being truncated. Is there a better method to get the "center" of the image?
public void RotateImageLeft() {
try {
BufferedImage newImage = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(), originalImage.getType());
AffineTransform tx = new AffineTransform();
tx.rotate(Math.toRadians(-90.0), originalImage.getWidth() / 2, originalImage.getHeight() / 2);
Graphics2D g2 = newImage.createGraphics();
g2.drawImage(originalImage, tx, null);
originalImage = newImage;
this.repaint();
g2.dispose();
} catch (Exception ex) {
ex.toString();
}
//g2d.drawImage(getResImage(), rescale, x, y);
}
For full code disclosure, here's more code. Here's my painComponent overridden method:
public void paintComponent(Graphics g) {
super.paintComponent(g);
resizeImage();
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(getResImage(), rescale, x, y);
}
Here's the resizeImage() method that gets called:
public void resizeImage() {
Graphics g = getResImage().getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, getResImage().getWidth(), getResImage().getHeight());
int scaledWidth = (int) ((getOriginalImage().getWidth() * getHeight()
/ getOriginalImage().getHeight()));
if (scaledWidth < getWidth()) {
int leftOffset = getWidth() / 2 - scaledWidth / 2;
int rightOffset = getWidth() / 2 + scaledWidth / 2;
g.drawImage(getOriginalImage(),
leftOffset, 0, rightOffset, getHeight(),
0, 0, getOriginalImage().getWidth(), getOriginalImage().getHeight(),
null);
} else {
int scaledHeight = (getOriginalImage().getHeight() * getWidth())
/ getOriginalImage().getWidth();
int topOffset = getHeight() / 2 - scaledHeight / 2;
int bottomOffset = getHeight() / 2 + scaledHeight / 2;
g.drawImage(getOriginalImage(),
0, topOffset, getWidth(), bottomOffset,
0, 0, getOriginalImage().getWidth(), getOriginalImage().getHeight(),
null);
}
}
I'm using the ResizeImage method since I want any image to fit correctly on my 720/432 panel.
Here's some example pictures.
Pre-rotated
Post-rotated:
New code: (new image is the correct height/width of rotated image, still getting black bars. Screens below.
public void RotateImageLeft() {
try {
BufferedImage newImage = new BufferedImage( originalImage.getHeight(),originalImage.getWidth(), originalImage.getType());
AffineTransform tx = new AffineTransform();
tx.rotate(Math.toRadians(-90.0), newImage.getWidth() / 2, (newImage.getHeight() / 2));
Graphics2D g2 = newImage.createGraphics();
g2.drawImage(originalImage, tx, null);
originalImage = newImage;
this.repaint();
g2.dispose();
} catch (Exception ex) {
ex.toString();
}
}
Post rotate:
From my answer to another similar question
If you're rotating then this will work for 90 degrees.
move image so centered "around" the origin
plain rotate() call with no extra parameters
Move image back into the center remembering that now width = old height and height = old width.
Also remember the affine transform steps work in reverse order.
AffineTransform tx = new AffineTransform();
// last, width = height and height = width
tx.translate(originalImage.getHeight() / 2,originalImage.getWidth() / 2);
tx.rotate(Math.PI / 2);
// first - center image at the origin so rotate works OK
tx.translate(-originalImage.getWidth() / 2,-originalImage.getHeight() / 2);
If you want to rotate an image without cropping you need to add black bars around it first, since a rotated rectangle will always have a bigger bounding box than an axis-aligned one (exception: rotating a square by multiples of 90 degrees).
So what you want to do is do some trigonometric calculations beforehand to decide the maximum Width/Height of the rotated image, and combine that with the original Width/Height. Resize your image (centering it) using those dimensions, rotate it, and then crop it back to the Width/Height of the rotated image.
I wanted to add background image to my JFrame.
Background image means I can later add Components on the JFrame or JPanel
Although I coudn't find how to add background image to a JFrame,
I found out how to add background image to a JPanel from here:
How to set background image in Java?
This solved my problem, but now since my JFrame is resizable I want to keep the image in center.
The code I found uses this method
public void paintComponent(Graphics g) { //Draw the previously loaded image to Component.
g.drawImage(img, 0, 0, null); //Draw image
}
Can anyone say how to align the image to center of the JPanel.
As g.drawImage(img, 0, 0, null); provides x=0 and y=0
Also if there is a method to add background image to a JFrame then I would like to know.
Thanks.
Assuming a suitable image, you can center it like this:
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
int x = (this.getWidth() - image.getWidth(null)) / 2;
int y = (this.getHeight() - image.getHeight(null)) / 2;
g2d.drawImage(image, x, y, null);
}
If you want the other components to move with the background, you can alter the graphics context's affine transform to keep the image centered, as shown in this more complete example that includes rotation.
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.translate(this.getWidth() / 2, this.getHeight() / 2);
g2d.translate(-image.getWidth(null) / 2, -image.getHeight(null) / 2);
g2d.drawImage(image, 0, 0, null);
}