-1

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).

This is jPanel2 class

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);

    } 
}
Sarin Suriyakoon
  • 420
  • 1
  • 7
  • 19
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) Use a combination of `AffineTransform` instances to change the co-ordinate system. – Andrew Thompson Jan 05 '15 at 09:01
  • BTW - it looks like you've already started experimenting with `AffineTransform` instances, but the code above would have exactly the same effect as if you hadn't.. – Andrew Thompson Jan 05 '15 at 09:03
  • A side note (also @AndrewThompson ) : One should **never** call `g2d.setTransform(...)` with an affine transform that was created manually (not even with the identity transform). See the JavaDoc of this method for details: http://docs.oracle.com/javase/8/docs/api/java/awt/Graphics2D.html#setTransform-java.awt.geom.AffineTransform- – Marco13 Jan 05 '15 at 09:27
  • Thank you.So how could I use the coordinates of Jpanel2 instead of jFrame as a question – Sarin Suriyakoon Jan 05 '15 at 12:09

1 Answers1

1

Use SwingUtilities class. It has the methods

public static Point convertPoint(Component source,Point aPoint,Component destination)
public static Point convertPoint(Component source,int x, int y,Component destination)

where you can pass coordinates and convert from one component's coordinates to another componnet's coordinates.

StanislavL
  • 56,971
  • 9
  • 68
  • 98