Rotate rectangle on an Image Swing - java

I have an Image on a JPanel. I'm then drawing a rectangle on top of the image like this:
Graphics2D image = (Graphics2D) g;
image.drawRect(......);
//create image code here.
image.rotate(1.5);
image.drawImage(....);
Problem is that when I rotate the image image.rotate(1.5), the rectangles stay in the same place.
I've tried creating the rectangle before rotating the image and after rotating the image, but both times it fails.
Is there an easy way to make the rectangles also rotate along with the image?
Thanks.

One approach is to rotate the graphics context's affine transform, as shown in this example. In that way, all drawing will be rotated by the same amount.

You might want to try using Rectangle, an implementation of the Rectangle2D class, then use g2d.draw(rectangle). This might be able to get the rotation state from the Graphics2D object better.

Related

Rotation and collision is possible with fillrect() created rectangle?-j2me

I am working on a J2ME landscape game.I have created a rectangle using fillRect()
like this.
gr.fillRect(52,180,5,10);
Now I want to do rotation,and check collision with this rectangle.Is this possible?Is there any way to convert this fillrect() image to a sprite?
How can I manage it?
I have done it with immutable image.I used same coordinates as fillRect()
Image source; // the image to be copied
source = Image.createImage(...);
Image copy = Image
.createImage(source.getWidth(), source.getHeight());
Graphics g = copy.getGraphics();
g.drawImage(source, 0, 0, TOP|LEFT);
But it is impossible to rotate.

Drawing images to 4 different points

I have a quadrilateral drawn in Path2D, and I would like for there to be an image on it. More specifically, I am trying to draw an image of my choice to 4 different points on a quadrilateral. In my case, it is a parallelogram. I do not want the image to go over the paralellogram. A better way to see what I am trying to say is to see the screenshot below.
I would like the image to be transformed to fit the green area. Not clipped.
I want the image to be pinned over the green paralellogram. However. I do not want the image to go over into the blue paralellogram, or the white space foe that matter.
So far I have tried
Researching for a way to place images directly onto Path2D.Double() objects. No answer
Rotating the image to fit the paralellogram. Didnt work.
Using AffineTransform in java. Dont get it ;-;
Thanks. I am new to java so do try to be lenient?
One way is to:
create a separate BufferedImage.
Apply a transform to the new image.
Draw your image to that new image.
Use the Shape object for the green area as a clip on the main drawing area
Draw the transformed image onto the main drawing area.
It's been a while since I have done transformations. You may have to set the transformation first and then draw the image after. Transformation has to come first.
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
g2.transform(AffineTransform.getShearInstance(1.0, 0));
g2.drawImage(image, 0, 0, this);
}
Here is a simple example of how transforms work. You will have to spend some time on figuring out what values you need to make it work or if you might need to manually create a transformation matrix yourself.

Apply AffineTransform to TexturePaint

I would like to know how can I rotate a texture paint with image in java.
Normally it is easy to rotate the Graphics2D g2 object, but I don't want to do that. I also don't want to rotate my shape and do some crazy rotation scales, etc. It will be easy to normally do the drawing and fills with paint while paint itself is scaled, rotated, etc.
I also don't want to create a rotated image and then texturepaint it.
There is a createContext inside the texturepaint class but I don't know how can I use it to rotate the original image.
I hope you understand what I mean by rotating the texture itself nwo :) ?
Thank you.
Edit:
I was playing with Java custom Paint implementation performance issue but there is an issue I cannot handle. When there is an offset to where tiling will be started, it does not start correctly.
Sorry for mixing up the problem.
I decided to create a second shape before transform the canvas, and then rotate it back to match the inverse of the trnsform.
This solved my problem.
try {
this.shape = new Path2D.Double(bx.createInverse().createTransformedShape(shape));
} catch (NoninvertibleTransformException ex) {
}

How to create a Buffered Image of the content drawn on a Canvas?

I have a canvas on which I am adding primitive shapes like square, circles etc. Is it possible to get the content drawn on the canvas in form of a Buffered Image.
I actually aim to access single pixels from the canvas, and couldn't find a better way of doing this?
Try this approach:
1.) Create a bufferedimage the width and height of the canvas
2.) Create a graphics2D object from the new bufferedimage
3.) Use the paint(g2d) or paintall(g2d) method of your canvas object
So you have something like:
BufferedImage myBI = new BufferedImage(myCanvas.getWidth(), myCanvas.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = myBI.createGraphics();
myCanvas.paint(g);
Your canvas should now be painted into the bufferedimage.
The question of whether to draw from a canvas to a bufferedimage or to draw from a bufferedimage to a canvas has to do with speed and with image quality. Drawing to buffered image is faster than drawing to canvas, but that might not matter if you are just doing this for static images. But you may also notice a difference in image quality. I have printed jpanels to buffered image and then to jpg files, and have noticed reduced image quality.

can we zoom in zoom out the drawing we draw on the Canvas?

We have an old (more than 10yrs old) Java Swing applicatin which draws lots of circles and connections (lines) between those circles on a JCanvas (a subclass of JComponent) based on lab data.
Because the data becames bigger and bigger, we cannot display the entire drawing now. We have put the JCavans into a JScrollPane but it is not convenience to scroll the drawing.
Can we add zoom in zoom out for it? if yes, how? I know we can zoom image but the drawing on Canvas is an image?
thanks,
EDIT:
we draw those circles and line with Graphics within paintComponent(Graphics g) method.
You could apply a scaling Transform to the Graphics2D object passed to the paintComponent method. You can learn how to use it in the Java 2D programming trail.
Without knowing anything about your application it's hard to provide useful advice (adding a code snippet or better yet a cutdown example app would be helpful to show how things are being drawn), but I'll give it a shot:
Why don't you multiply the x,y and width,height values by a scaling factor before you draw each circle/line? I assume that somewhere your canvas is using a Graphics object to draw each shape?

Categories