954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

can anyone tell me how to rotate the inner yellow star within the blue star

can anyone tell me how to rotate the inner yellow star within the blue star

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Path2D;

public class app extends Applet
{
   final static int OFFSET = 30;
   final static int OFFSETS = 30;

   public void paint(Graphics g)
   {
      Graphics2D g2 = (Graphics2D) g;
      int width = getWidth()-2*OFFSET;
      int height = getHeight()-2*OFFSET;
      g2.translate(width/2-height/2+OFFSETS, OFFSETS);
      width = height;
      g2.setColor(Color.white);
      setBackground(Color.white);
      Path2D star = new Path2D.Float();
      star.moveTo(width/5F, height-1);
      star.lineTo(width/2F, 0);
      star.lineTo(4*width/5F, height-1);
      star.lineTo(0, 2*height/5F);
      star.lineTo(width-1, 2*height/5F);
      star.closePath();
      g2.draw(star);
      g2.setColor(Color.blue);
      g2.fill(star);
      
   ////////////////////////// inner star//////////////////////////////////  


      int widths = getWidth()-3*90;
      int heights = getHeight()-3*37;
      g2.translate(widths/2-heights/2+OFFSETS, OFFSETS);
      widths = heights;      
      g2.setColor(Color.white);
      setBackground(Color.white);
      Path2D star1 = new Path2D.Float();
      star1.moveTo(widths/5F, heights-1);
      star1.lineTo(widths/2F, 0);
      star1.lineTo(4*widths/5F, heights-1);
      star1.lineTo(0, 2*heights/5F);
      star1.lineTo(widths-1, 2*heights/5F);
      star1.closePath();
      g2.draw(star1);
      g2.setColor(Color.yellow);
      g2.fill(star1);
   }
}
Jessurider
Posting Whiz in Training
207 posts since Feb 2011
Reputation Points: 9
Solved Threads: 0
 

You could use the transform(AffineTransform at) method on your Path2D star1, with an AffineTransform obtained from AffineTransform.getRotateInstance(double theta, double anchorx, double anchory) to rotate it by any desired angle

AffineTransformations look very scary, and often they are, but in essence they take a shape and change it by stretching, skewing, sheering, rotating etc. All you need is a simple rotation, which the method I suggested will give you.

API documentation is in the usual place.

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

You can also rotate the current graphics context with the .rotate() method of the Graphics2D class. This part of the Java tutorial discusses these transformations.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You