0

When I add the function g2d.rotate(some Number) the screen shows no shape at all, and without this function everything is working.

What is the problem?

public void paintComponent(Graphics g) {
    super.paintComponent(g);
  Graphics2D g2d = (Graphics2D)g;
    g2d.setColor(color);
    Polygon polygon = new Polygon(xCordinate,yCordinate,4);

   // g2d.rotate(Math.toRadians(45));
    g2d.draw(polygon);
    g2d.fill(polygon);
}

and in the main:

public class Main extends JPanel {
    Camera c=new Camera(100, 50, (Math.PI)*2, 0, 150,200,Math.PI,Color.MAGENTA);
    //Camera c1=new Camera(100, 50, (Math.PI)*2, 0, 150,200,0,Color.black);
   public static void main(String[] a) {
      JFrame f = new JFrame();
      f.setSize(400, 400);
      f.add(new Main());
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.setVisible(true);
   }

   public void paint(Graphics g) {
      //c1.paintComponent(g);
      c.paintComponent(g);

}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

1 Answers1

1

Your posted code has several problems:

  1. Do not override paint(). You should never invoke paintComponent() directly. You just add the component to a panel and it will be painted automatically. Read the Swing tutorial on Custom Painting for more information and examples.

  2. Your Polygon doesn't have any points. You need to add 4 points to it.

  3. When you rotate a Shape you also need to translate it.

Check out Playing With Shapes and Rotated Icon. The source code shows you how to do rotation or you can just use the classes as is.

camickr
  • 321,443
  • 19
  • 166
  • 288