1

I am wondering how to rotate things I have already drawn (like lines) in Java drawing Panel (not in JPanel).

I am trying to rotate a triangle i created by connecting 3 lines:

g.drawLine(size, size, 2*size, size);
g.drawLine(size, size,size+size/2, size+(int)(size/2 * Math.sqrt(3)));
g.drawLine(2*size, size,size+size/2, size+(int)(size/2 * Math.sqrt(3)));

How do I rotate that?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Louis B
  • 342
  • 1
  • 5
  • 21
  • 1) Why AWT rather than Swing? See this answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see [Mixing Heavyweight and Lightweight Components](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html). 2) Use an `AffineTransform` - though note that won't apply to things that are 'already drawn'. It will be necessary to repaint the panel. – Andrew Thompson Oct 20 '13 at 13:38
  • this is for class so my teacher requires i use awt, can you provide an example of how to rotate a single line using affineTransform – Louis B Oct 20 '13 at 14:13
  • *"..can you provide an example"* I have provided [many examples](http://stackoverflow.com/search?q=user%3A418556+affinetransform) already. If you cannot get a working solution from those examples, post an [SSCCE](http://sscce.org/) of your best attempt. – Andrew Thompson Oct 20 '13 at 14:17

2 Answers2

3

If you want to rotate a point like that, then you could:

double startX; // <------------|
double startY; // <------------|
double endX;   // <------------|
double endY;   // <-define these
double angle;  // the angle of rotation in degrees

To draw the original line

g.setColor(Color.BLACK);
g.drawLine(startX, startY, endX, endY); //this is the original line

double length = Math.pow(Math.pow(startX-endX,2)+Math.pow(startY-endY,2),0.5);
double xChange = length * cos(Math.toRadians(angle));
double yChange = length * sin(Math.toRadians(angle));

To draw the new, rotated line

g.setColor(Color.GRAY);
g.fillRect(0,0,1000,1000); //paint over it

g.setColor(Color.BLACK);
g.drawLine(startX, startY, endX + xChange, endY + yChange);
mkaminsky
  • 353
  • 2
  • 10
0

Use graphics2D and Polygons

Graphics2D g2 = (Graphics2D) g;
int x2Points[] = {0, 100, 0, 100}; //these are the X coordinates
int y2Points[] = {0, 50, 50, 0}; //these are the Y coordinates
GeneralPath polyline = 
        new GeneralPath(GeneralPath.WIND_EVEN_ODD, x2Points.length);

polyline.moveTo (x2Points[0], y2Points[0]);

for (int index = 1; index < x2Points.length; index++) {
         polyline.lineTo(x2Points[index], y2Points[index]);
};

g2.draw(polyline);

If you want to rotate it, just do it over but add, before

g2.rotate(Math.toRadians(angle), centerX, centerY);

Where "angle" is the amount you want to rotate it and (centerX, centerY) are the coordinates of the point you want to rotate it around.

I hope this helps

mkaminsky
  • 353
  • 2
  • 10
  • this is great but i literally just want to rotate a single line by a set amount of degrees while still keeping the original point the line starts at the same – Louis B Oct 20 '13 at 23:19
  • @LouisB It turns out that it's less trivial than it sounds. You either use Graphics2D.rotate() or do it yourself with some basic trig. – Radiodef Oct 21 '13 at 20:53