painting rectangle at an angle?
Hi,
There are 2 points, (x1,y1) and (x2,y2), a line is drawn before then using the drawline method, what I want to do it have a rectangle which will go from point (x1,y1) to (x2,y2).
However, fillrect do not support any angle parameter and I can't find a method to the rotation.
See the orange rectangle in the image:
http://img207.imageshack.us/img207/7584/javamh1.jpg
Any help will be very much appreciated.
# 1
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class InLineRectangle extends JPanel {
Point p1 = new Point(100,300);
Point p2 = new Point(300,100);
Rectangle rect = new Rectangle(75,25);
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.blue);
g2.draw(new Line2D.Double(p1, p2));
g2.setPaint(Color.red);
mark(p1, g2);
mark(p2, g2);
// Locate rect at 1/4 the length of the line from p1 to p2.
double theta = Math.atan2(p2.y-p1.y, p2.x-p1.x);
double length = p1.distance(p2);
double x = p1.x + (length/4.0)*Math.cos(theta);
double y = p1.y + (length/4.0)*Math.sin(theta);
// Locate rect on the line.
AffineTransform at = AffineTransform.getTranslateInstance(x, y-rect.height/2);
// Rotate rect about x, y on the line.
at.rotate(theta, 0, rect.height/2);
g2.setPaint(Color.orange);
g2.fill(at.createTransformedShape(rect));
// Show x, y for confirmation.
g2.setPaint(Color.green.darker());
mark(new Point2D.Double(x, y), g2);
}
private void mark(Point2D p, Graphics2D g2) {
g2.fill(new Ellipse2D.Double(p.getX()-2, p.getY()-2, 4, 4));
}
public Dimension getPreferredSize() {
return new Dimension(400,400);
}
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, new InLineRectangle(), "", -1);
}
}
# 3
Hi, the draw square part integrate into the application very well, thanks!
However, I have used drawString to draw some text on top of the square box,
g2.drawString("XX", x, y);
And this command can not be used to drawString
g2.fill(at.createTransformedShape(rect));
Any idea what should I do?
# 4
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*;
public class InLineRectangle extends JPanel {
Point p1 = new Point(100,300);
Point p2 = new Point(300,100);
Rectangle rect = new Rectangle(75,25);
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.blue);
g2.draw(new Line2D.Double(p1, p2));
g2.setPaint(Color.red);
mark(p1, g2);
mark(p2, g2);
// Locate rect at 1/4 the length of the line from p1 to p2.
double theta = Math.atan2(p2.y-p1.y, p2.x-p1.x);
double length = p1.distance(p2);
double x = p1.x + (length/4.0)*Math.cos(theta);
double y = p1.y + (length/4.0)*Math.sin(theta);
// Locate rect on the line.
AffineTransform at = AffineTransform.getTranslateInstance(x, y-rect.height/2);
// Rotate rect about x, y on the line.
at.rotate(theta, 0, rect.height/2);
g2.setPaint(Color.orange);
g2.fill(at.createTransformedShape(rect));
// Show x, y for confirmation.
g2.setPaint(Color.green.darker());
mark(new Point2D.Double(x, y), g2);
// Draw text.
String s = "aligned";
Font font = g2.getFont().deriveFont(16f);
g2.setFont(font);
FontRenderContext frc = g2.getFontRenderContext();
float sw = (float)font.getStringBounds(s, frc).getWidth();
LineMetrics lm = font.getLineMetrics(s, frc);
float sh = lm.getAscent() + lm.getDescent();
x = (rect.width - sw)/2;
y = (rect.height + sh)/2 - lm.getDescent();
// Position the text relative to rect.
AffineTransform xf = AffineTransform.getTranslateInstance(x, y);
xf.preConcatenate(at);
g2.setFont(font.deriveFont(xf));
g2.setPaint(Color.blue);
g2.drawString(s, 0, 0);
}
private void mark(Point2D p, Graphics2D g2) {
g2.fill(new Ellipse2D.Double(p.getX()-2, p.getY()-2, 4, 4));
}
public Dimension getPreferredSize() {
return new Dimension(400,400);
}
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, new InLineRectangle(), "", -1);
}
}