A rotated square panel in Java GUI
The critical thing seems to be painting the components after rotating the graphics context. Here's an example:
Addendum 1:As @Atreys comments, the rotated components are drawn, but interact poorly. If the components must remain usable, event coordinates should also be transformed. Compare this (considerably) more complex example that mirrors components.
Addendum 2: If you also need to transform the mouse coordinates, this example may be helpful.
Addendum 3: Alternatively, consider the drawString()
examples examined here.
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/** @see https://stackoverflow.com/questions/6333464 */
public class RotatePanel extends JPanel {
public RotatePanel() {
this.setPreferredSize(new Dimension(320, 240));
this.add(new JLabel("Hello World!", JLabel.CENTER));
}
@Override
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
int w2 = getWidth() / 2;
int h2 = getHeight() / 2;
g2d.rotate(-Math.PI / 2, w2, h2);
super.paintComponent(g);
}
private void display() {
JFrame f = new JFrame("RotatePanel");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new RotatePanel().display();
}
});
}
}
How to make a rotated panel in java
Yes, rotate the image around its center, as shown in this example.
Rotate what is painted in a JPanel keeping the centroid's location
Found. This example helped me. I updated the paintComponent(Graphics gr)
method like this:
@Override
public void paintComponent(Graphics gr) {
super.paintComponent(gr);
Graphics2D g2 = (Graphics2D) gr;
if (angle != 0) {
// Moving to centroid
g2.translate(getWidth()/2, getHeight()/2);
g2.rotate(angle);
// Moving back but using the string's dimensions
// instead of the bounds (which have been changed)
g2.translate(-stringWidth/2, -stringHeight/2);
}
g2.drawString("Hello World", 2, 12);
if (angle != 0) {
// Revert the transformation matrix back to its initial state
g2.translate(stringWidth/2, stringHeight/2);
g2.rotate(-angle);
g2.translate(-getWidth()/2, -getHeight()/2);
}
}
The key is that the Graphics2D#rotate()
executes the following calls:
translate(x, y);
rotate(theta);
translate(-x, -y);
But in my case I didn't want to translate back exactly the same amount as the original translation. I had to use the string's dimensions.
How to draw and rotate square inside center of other square?
You could first draw your figure around the origin (that's easy) and then translate:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(400, 400);
frame.setVisible(true);
frame.add(new JPanel() {
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
double alpha = Math.toRadians(5);
double factor = 1 / (Math.sin(alpha) + Math.cos(alpha));
double size = 200;
g2d.translate(size, size);
for (int i = 0; i < 20; i++) {
g2d.setColor(i % 2 == 0 ? Color.black : Color.green);
int intSize = (int) Math.round(size);
g2d.fillRect(-intSize / 2, -intSize / 2, intSize, intSize);
size = size * factor;
g2d.rotate(alpha);
}
}
});
}
}
Rotate BufferedImage Inside JPanel
I solved my own issue. The problem lay in the code:
myPicture.getType()
Since there is a lot of variability in the types of images you could put in to the program, the results are going to be unpredictable when you start drawing into the new BufferedImage. I solved the problem by setting the type explicitly, which in my case required
BufferedImage.TYPE_INT_ARGB
so the full statement read:
BufferedImage newImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Rotating a JTextField vertically
You can't usefully rotate interactive components without also transforming the mouse coordinates, but you can rotate the graphics context to render non-interactive components like JLabel
, as shown here.
In your example, 1/2 * Math.PI != Math.PI / 2
.
Related Topics
Check If a Value Exists in Arraylist
Getting Value of Public Static Final Field/Property of a Class in Java via Reflection
Java Se 6 VS. Jre 1.6 VS. Jdk 1.6 - What Do These Mean
Java Hashmap Performance Optimization/Alternative
Converting to Upper and Lower Case in Java
How to Send List of Objects to View and Back to Post Method in Controller
Java 8 Streams - Collect VS Reduce
How to Upload and Store an Image with Google App Engine (Java)
Java 8 Default Methods as Traits:Safe
Time Complexity for Java Arraylist
How to Deal with Maven-3 Timestamped Snapshots Efficiently
Intellij - Convert a Java Project/Module into a Maven Project/Module
JSON - Iterate Through JSONarray