Using a matrix to rotate rectangles individually

I would use a function similar to this: public void RotateRectangle(Graphics g, Rectangle r, float angle) { using (Matrix m = new Matrix()) { m.RotateAt(angle, new PointF(r.Left + (r.Width / 2), r.Top + (r.Height / 2))); g.Transform = m; g.DrawRectangle(Pens.Black, r); g.ResetTransform(); } } It uses a matrix to perform the rotation at a certain … Read more

Rotate a Java Graphics2D Rectangle?

For images you have to use drawImage method of Graphics2D with the relative AffineTransform. For shape you can rotate Graphics2D itself: public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D)g; g2d.setColor(Color.WHITE); Rectangle rect2 = new Rectangle(100, 100, 20, 20); g2d.rotate(Math.toRadians(45)); g2d.draw(rect2); g2d.fill(rect2); } And btw, you should override paintComponent method instead of paint. Citing … Read more

Rotate a buffered image in Java

As always, the Internet to the rescue. So, this is some code which I hobbled together from other resources/post/blogs which will return a new image which is sized so it will contain the rotated image public BufferedImage rotateImageByDegrees(BufferedImage img, double angle) { double rads = Math.toRadians(angle); double sin = Math.abs(Math.sin(rads)), cos = Math.abs(Math.cos(rads)); int w … Read more

Fit/Scale JComponent to page being printed

The basic concept is to use an AffineTransformation to provide scaling to the resulting output. In my tests, I was able to take an image of 7680×4800 and get printed on a page of 595×842 (scaled down by something like 93%) import java.awt.BorderLayout; import java.awt.Component; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.ActionEvent; … Read more

How to make line animation smoother?

I put together this little test and got no significnt issues, I was basically capable of maintaining 50fps even with 1000 rectangles all moving at random speeds in random directions. public class SimpleAnimationEngine { public static void main(String[] args) { new SimpleAnimationEngine(); } public SimpleAnimationEngine() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try … Read more

Rotating Image with AffineTransform

The major problem (that I can see) is the translation of the Graphics context which is offset the position that the rotation will take place. I “think” rotation by default occurs at the top/left corner of the Graphics context (where it’s 0x0 position is, which you’ve translated to something else), this could be causing the … Read more

Drawing a simple line graph in Java

Problems with your code and suggestions: Again you need to change the preferredSize of the component (here the Graph JPanel), not the size Don’t set the JFrame’s bounds. Call pack() on your JFrame after adding components to it and before calling setVisible(true) Your foreach loop won’t work since the size of your ArrayList is 0 … Read more

Drawing an object using getGraphics() without extending JFrame

If you want to change the way your component is being drawn (you are adding rectangles), you need to redefine paintComponent() in that component. In your code, you are using getGraphics(). You shouldn’t call getGraphics() on a component. Any painting you do (to the Graphics returned) will be temporary and will be lost the next … Read more

Problems with newline in Graphics2D.drawString

The drawString method does not handle new-lines. You’ll have to split the string on new-line characters yourself and draw the lines one by one with a proper vertical offset: void drawString(Graphics g, String text, int x, int y) { for (String line : text.split(“\n”)) g.drawString(line, x, y += g.getFontMetrics().getHeight()); } Here is a complete example … Read more

Rotating BufferedImage instances

Maybe you should try using AffineTransform like this: AffineTransform transform = new AffineTransform(); transform.rotate(radians, bufferedImage.getWidth() / 2, bufferedImage.getHeight() / 2); AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR); bufferedImage = op.filter(bufferedImage, null); Hope this helps.