I use netbeans IDE to create Gui then I customize the Jpanel2 and draw polygon on it this is code snippet
int[] polygonXs = { 151, 153, 158, 159, 154};
int[] polygonYs = { 6, 1, 3, 8, 10};
Shape shape = new Polygon(polygonXs, polygonYs, polygonXs.length);
Then the problem is the point(151,6) (153,1) (158,3) (159,8) (154,10) is in JFrame coordinates.
In additional now (0,0) is at the top left of the JFrame but I want (0,0) be at the top left of Jpanel2(black background). So how to fix that (code of Jpanel2 below the image).
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import javax.swing.JPanel;
public class J2d1 extends JPanel{
public static final String TITLE = "Affine Transform Demo";
int[] polygonXs = { 151, 153, 158, 159, 154};
int[] polygonYs = { 6, 1, 3, 8, 10};
Shape shape = new Polygon(polygonXs, polygonYs, polygonXs.length);
double x = 50.0, y = 50.0;
public void paintComponent(Graphics g){
super.paintComponent(g);
setBackground(Color.BLACK);
Graphics2D g2d = (Graphics2D)g;
AffineTransform saveTransform = g2d.getTransform();
AffineTransform identity = new AffineTransform();
g2d.setTransform(identity);
g2d.setColor(Color.green);
g2d.fill(shape);
g2d.translate(x, y);
g2d.scale(2.2, 2.2);
g2d.fill(shape);
for(int i = 0; i < 5; ++i){
g2d.translate(50.0, 5.0);
g2d.setColor(Color.blue);
g2d.fill(shape);
g2d.rotate(Math.toRadians(15.0));
g2d.setColor(Color.RED);
g2d.fill(shape);
}
g2d.setTransform(saveTransform);
}
}