java获取每个形状的中心

时间:2017-11-07 14:02:23

标签: java swing transform java-2d

你好,我无法将箭头从圆心绘制到下一个圆心,每个圆圈都来自其他地方的xml,我们认为尺寸为14。

为什么我的代码似乎没有做到这一点?

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.AffineTransform;
import java.io.File;
import java.io.IOException;
import java.util.Vector;
import javax.swing.JFrame;

只需设置框架

public class GRAPHICS_REPRESENTATION extends JFrame {
private Vector<Point> point_tab;
public GRAPHICS_REPRESENTATION() {
    super("graphic");
    point_tab = new Vector<Point>(0);
    setSize(800, 800);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
}

画一个圆圈(从这里找到并重新加工)

// Convenience method to draw from center with radius
public void drawCircle(Graphics cg, int xCenter, int yCenter, int r) {
    Graphics2D cg2 = (Graphics2D) cg.create();
    cg2.drawOval(xCenter - r, yCenter - r, r, r);

}// end drawCircle

问题方法

private void disposeScene(Graphics g1, int x1, int y1, int r) {
    Graphics2D g = (Graphics2D) g1.create();
    AffineTransform at = g.getTransform();
    AffineTransform at2 = new AffineTransform();
    at2 = AffineTransform.getScaleInstance(0.5, 0.5); // scaling à faire en 1er
    g.transform(at2); // prise en compte de la matrice
    at2 = AffineTransform.getTranslateInstance(getWidth() / 2 + x1 + r / 2, getHeight() / 2 - y1 - r / 2); // centrage
    g.transform(at2);

    double angle = (double) Math.cos(0);
        for (int i = 0; i < 14; i++) {
        angle = Math.rint(angle / (i + 1));
        g.setColor(Color.RED);
        drawCircle(g, 0, 0, 2 * r);
        Point center = new Point();
        center.setLocation(g.getTransform().getTranslateX() / 2 + r / 2 + x1 / 2,
                g.getTransform().getTranslateY() / 2 - r / 2 - y1 / 2);
        point_tab.add(center);
        at2.concatenate(AffineTransform.getRotateInstance(angle)); // rotation autour d'un centre
        g.transform(at2);
        System.out.println(point_tab.elementAt(i).getX() + "," + point_tab.elementAt(i).getY());
        g.setColor(Color.BLUE);
        if (i + 1 < point_tab.size()) {
            drawArrow(g, point_tab.elementAt(i).getX(), point_tab.elementAt(i).getY(),
                    point_tab.elementAt(i + 1).getX(), point_tab.elementAt(i + 1).getY());
        } else {
            drawArrow(g, point_tab.elementAt(i).getX(), point_tab.elementAt(i).getY(),
                    point_tab.elementAt(0).getX(), point_tab.elementAt(0).getY());
        }
    }
    g.setTransform(at);

}

从src到dst画一个箭头(在这里找到并重新编写)

private final int ARR_SIZE = 4;

private void drawArrow(Graphics g1, double xsrc, double ysrc, double xdst, double ydst) {
    Graphics2D g = (Graphics2D) g1.create();
    double dx = xdst - xsrc, dy = ydst - ysrc; // distances 
    double angle = Math.atan2(dy, dx);
    int len = (int) Math.sqrt(dx * dx + dy * dy);
    AffineTransform at = g.getTransform();
    AffineTransform at2 = new AffineTransform();
    at2=AffineTransform.getTranslateInstance(xsrc, ysrc);
    at2.concatenate(AffineTransform.getRotateInstance(angle));
    g.transform(at2);

    // Draw horizontal arrow starting in (0, 0)
    g.drawLine(0, 0, len, 0);
    g.fillPolygon(new int[] { len, len - ARR_SIZE, len - ARR_SIZE, len }, new int[] { 0, -ARR_SIZE, ARR_SIZE, 0 },
            4);
    g.setTransform(at);
}

主要方法

public void paint(Graphics g) {
    disposeScene(g, getWidth()/4,getHeight()/4,40);
}

1 个答案:

答案 0 :(得分:2)

这是我自己找到的解决方案

/**
 * @brief draw the scene with all shapes set up
 * @param g1
 * @param x1
 * @param y1
 * @param xtg
 * @param r
 */
private void disposeScene(Graphics g1, int x1, int y1, XML_TO_GRAPH xtg, int r) {
    Graphics2D g = (Graphics2D) g1.create();
    g.translate(getWidth() / 2, getHeight() / 2);
     drawCircle(g, 0, 0, (x1+y1)/2); // cercle fictif
    for (int i = 0; i < xtg.getsceneVector().size(); i++) {
        double angle = Math.toRadians(360.0); // radian mieux 2pi

        angle = (angle*i / xtg.getsceneVector().size());
        System.out.println("angle(" + i + ")=" + angle);
        g.setColor(Color.RED);
        drawCircle(g, (int) (Math.cos(angle) * x1), (int) (Math.sin(angle) * y1), r);

        Point center_big_circle = new Point();
        center_big_circle.setLocation((0), (0)); // centre du grand cercle fictif
        Point center_little_circle = new Point();
        center_little_circle.setLocation((Math.cos(angle) * (x1)), (Math.sin(angle) * (y1))); // décalage vers les
                                                                                                // bords
        System.out.println("centre(" + i + ")=" + center_little_circle.getX() + "," + center_little_circle.getY());
        point_tab.add(center_little_circle);
        g.setColor(Color.BLACK);
        drawString(g, (int) center_little_circle.getX()-r, (int) center_little_circle.getY()-r, 16, "scène : " + i);
        System.out.println(point_tab.elementAt(i).getX() + "," + point_tab.elementAt(i).getY());
        g.setColor(Color.BLUE);
        if (i + 1 < point_tab.size()) {// si existe 
            drawArrow(g, point_tab.elementAt(i).getX(), point_tab.elementAt(i).getY(),
                    point_tab.elementAt(i + 1).getX(), point_tab.elementAt(i + 1).getY());
//              g.setColor(Color.BLACK);
//              drawString(g, (int)(( point_tab.elementAt(i+1).getX()-
point_tab.elementAt(i).getX())/2), (int) ((point_tab.elementAt(i+1).getY()-
point_tab.elementAt(i).getY())/2), 16, "X : " + 
xtg.getcount_occurence().toString());

            //idem
            drawArrow(g, point_tab.elementAt(i+1).getX(), point_tab.elementAt(i+1).getY(),
                    point_tab.elementAt(i).getX(), point_tab.elementAt(i).getY());
//              drawString(g, (int)(( point_tab.elementAt(i).getX()-
point_tab.elementAt(i+1).getX())/2), (int) ((point_tab.elementAt(i).getY()-
point_tab.elementAt(i+1).getY())/2), 16, "X : " + 
xtg.getcount_occurence().toString());

        }
    }

}

// Convenience method to draw from center with radius
/**
 * 
 * @param cg
 * @param xCenter
 * @param yCenter
 * @param r
 */
public void drawCircle(Graphics cg, int xCenter, int yCenter, int r) {
    Graphics2D cg2 = (Graphics2D) cg.create();
    System.out.println("Center at: " + (xCenter - r) + "," + (yCenter - r));
    cg2.drawOval(xCenter - r, yCenter - r, 2 * r, 2 * r);

}// end drawCircle

方便的drawString方法:

/**
 * 
 * @param g1
 * @param x
 * @param y
 * @param size
 * @param str
 */
private void drawString(Graphics g1, int x, int y, int size, String str) {
    Graphics2D g = (Graphics2D) g1.create();
    g.setFont(new Font("Times New Roman", Font.PLAIN, size));
    g.drawString(str, x, y);

}

并画画:

public void paint(Graphics g) {

    disposeScene(g, (int) (getWidth() / 3), (int) (getHeight() / 3), 20);

}