I use an AffineTransform class to transform my image and I need to have a position of image after translation and rotation. How to achieve that?
public void rotateImage(ImageObserver o, Graphics g, int x, int y) {
AffineTransform at = new AffineTransform();
at.translate(x, y);
at.rotate(Math.toRadians(deegres),ramie1.getWidth()/2,0);
Graphics2D g2 =(Graphics2D) g;
g2.drawImage(image, at, null);
}
Related
I'm trying to rotate an object in java but i noticed that something is wrong.
When i rotate it of 180 degrees,i get a value of the angle of '90°',so in order to get an angle of 360 degrees i have to rotate it twice.
What's wrong?
0°,90°,180°
The code:
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
if (rotate == true) {
ship.increaseDegress();
}
ship.draw(g);
}
Ship.java
public void increaseDegress() {
rotationAngle += 10;
if(rotationAngle>360) {
rotationAngle = 0;
}
}
public void draw(Graphics g) {
this.g = g;
Graphics2D g2 = (Graphics2D) g;
AffineTransform at = new AffineTransform();
Rectangle rect = this.getBounds();
at.rotate(Math.toRadians(rotationAngle), rect.getX() + rect.getWidth() / 2, rect.getY() + rect.getHeight() / 2);
g2.setColor(Color.BLUE);
g2.setTransform(at);
g2.draw(at.createTransformedShape(this));
}
So, let's take a quick look at your code...
public void draw(Graphics g) {
this.g = g;
Graphics2D g2 = (Graphics2D) g;
AffineTransform at = new AffineTransform();
Rectangle rect = this.getBounds();
at.rotate(Math.toRadians(rotationAngle), rect.getX() + rect.getWidth() / 2, rect.getY() + rect.getHeight() / 2);
g2.setColor(Color.BLUE);
g2.setTransform(at);
g2.draw(at.createTransformedShape(this));
}
So,
First you create AffineTransform, nice
You rotate the transformer, also nice
You apply the transformer to the Graphics context ... okay, there's a problem here, but let's move on
You apply the transformer to the shape!
So, on a single draw pass, you will rotate the shape by rotationAngle * 2! So, when the angle is 10°, the shape will be rendered at 20°, when it's 20°, it will rendered at 40°!
Okay, but there's another problem. Transformations applied to a Graphics context are compounding, this means, based on the available code, each time you call draw, the Graphics context is been rotated by rotationAngle. So if rotationAngle is 10°
On pass #1, the shape will be rotated to 20°
On pass #2, the shape will be rotated to 30°
On pass #3, the shape will be rotated to 40°
... so on and so forth ...
So, what's the answer?
When ever I pass a Graphics context off to some other method, I first create a copy it, because I don't trust anybody!
Graphics gCopy = g.create();
shape.draw(gCopy);
gCopy.dispose();
This ensures that the state of the Graphics context is returned to the state it was before I called draw, that will get rid of the compounding transformation.
The other solution is, don't transform the Graphics context. If you're transforming the shape, what's the point?
public void draw(Graphics g) {
this.g = g;
Graphics2D g2 = (Graphics2D) g;
AffineTransform at = new AffineTransform();
Rectangle rect = this.getBounds();
at.rotate(Math.toRadians(rotationAngle), rect.getX() + rect.getWidth() / 2, rect.getY() + rect.getHeight() / 2);
g2.setColor(Color.BLUE);
g2.draw(at.createTransformedShape(this));
}
how to draw this in java swing TitledBorder or any other panel ?
i tried the following code and didn't work, kept drawing half ellipse and the other half was cut.
#Override
public void paintBorder(Component c, Graphics g, int x, int y, int width,
int height) {
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.BLUE);
FontMetrics m = c.getFontMetrics(getTitleFont());
Ellipse2D shape = new java.awt.geom.Ellipse2D.Float(2, -20,m.stringWidth(title)+10, m.getHeight()+40);
g2.fill(shape);
g2.draw(shape);
super.paintBorder(c, g, x, y, width, height);
}
I want to rotate and set a BufferedImage.Rotate operation is working with my code. However how can i set left margin and top margin
Graphics g = combined.getGraphics();
Graphics2D g2d = (Graphics2D) g;
AffineTransform at = new AffineTransform();
at.translate(overlay.getWidth() / 2, overlay.getHeight() / 2);
double d = Math.PI/2;
at.rotate(d);
at.translate(-overlay.getWidth()/2, -overlay.getHeight()/2);
g2d.drawImage(image, 0, 0, null);
g2d.drawImage(overlay, at, null);
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();
}
I'm drawing a transparent rendered image on top of a background image which is then displayed on a JPanel.
I need to get the bounds of the rendered image after it has been scaled and rotated.
So that the next time I call repaint it can be with
the clip of the rendered image.
Any ideas? thanks for your help.
#Override public void paintComponent(java.awt.Graphics g)
{
if(g != null)
{
g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), null);
Graphics2D g2 = (Graphics2D)g.create();
double cx = sprite.getWidth() / 2.0;
double cy = sprite.getHeight() / 2.0;
AffineTransform at = AffineTransform.getTranslateInstance(xPos, yPos);
at.concatenate(AffineTransform.getScaleInstance(scale, scale));
at.rotate(theta, cx, cy);
g2.drawRenderedImage(sprite, at);
//get width and height of rendered image here?
g2.dispose();
}
Something like
Rectangle resizedBounds = at.createTransformedShape(sprite.getBounds()).getBounds();