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 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.

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

Stretch a JLabel text

In the approach shown below, the desired text is imaged using TextLayout using a suitably large Font size and scaled to fill the component. There’s a related example here. import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.RenderingHints; import java.awt.font.FontRenderContext; import java.awt.font.TextLayout; import java.awt.image.BufferedImage; import javax.swing.JFrame; import javax.swing.JLabel; /** @see … Read more

Java: Rotating Images

This is how you can do it. This code assumes the existance of a buffered image called ‘image’ (like your comment says) // The required drawing location int drawLocationX = 300; int drawLocationY = 300; // Rotation information double rotationRequired = Math.toRadians (45); double locationX = image.getWidth() / 2; double locationY = image.getHeight() / 2; … Read more

rotating coordinate plane for data and text in Java

One approach is shown in SineTest. In outline, Save the graphics context’s transform. Graphics2D g2d = (Graphics2D) g; AffineTransform at = g2d.getTransform(); Translate the origin to the center. g2d.translate(w / 2, h / 2); Invert the y-axis. g2d.scale(1, -1); Render using cartesian coordinates. Restore the transform for conventional rendering. g2d.setTransform(at);

Swing HTML drawString

If a fan of Java2D; but to get the most leverage from HTML in Swing components and layouts, I’d encourage you to use the component approach suggested by @camickr. If necessary, you can use the flyweight renderer approach seen in JTable, et al, in which a single component is used repeatedly for drawing. The example … Read more