Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

java - 绘制可以在JFrame上移动的线条(Painting lines that can be moved on JFrame)

Basically I'm trying to paint a bunch of lines in java that i can use code to manipulate the location later.

(基本上,我试图在Java中绘制一堆线,以便以后可以使用代码来操纵位置。)

Here is the code I used for my paint component:

(这是我用于绘画组件的代码:)

public class rectDrawer extends JComponent{
    public void drawComponent(Graphics gr) {
        Graphics2D g=(Graphics2D) gr;
        Element q= new Element();
        q=q.returnStatic();
        for(int i=1;i<5;i++) {
            g.setColor(q.getAColor(i));
            Shape s =q.getLine(i);
            AffineTransform a= AffineTransform.getScaleInstance(3,3);
            s=a.createTransformedShape(s);
            g.draw(s);
        }
    }
}

To initialize this class and add it to JFrame I used the following:

(为了初始化该类并将其添加到JFrame中,我使用了以下命令:)

public class simulator{
    public static JFrame screen = new JFrame();
    public ArrayList<Element> canvas = new ArrayList<Element>();
    public static void main(String[] args) {
        screen.setSize(900,900);
        screen.setTitle("Simulation");
        screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        screen.setVisible(true);

        Element field1=new Element(0,0,0,"field");
        field1.setStatics();
        rectDrawer d =new rectDrawer();
        screen.add(d);
    }
}

I really do not have any idea what is wrong with this, in theory it should paint a 432 by 432 square on the JFrame but all I'm getting is a blank JFrame.

(我真的不知道这有什么问题,从理论上讲,它应该在JFrame上绘制一个432 x 432的正方形,但是我得到的只是一个空白的JFrame。)

I am attaching the rest of my project (besides imports) in case there's a problem in there.

(如果有问题,我将附加我的项目的其余部分(除了进口)。)

Thanks!

(谢谢!)

public class Element {
    private double c1x, c1y, c2x, c2y, c3y, c3x, c4x, c4y, dSS, rSS, l, w; 
    private int canvasLocation;
    //c1 to c2 is width (short side) c2 to c4 is short 
    private Color color, frontColor;
    private String type;
    public static Element thisEl;
    private Elem t;
    public Element() {}
    public Element(double c1X, double c1Y, double degrees, String t){
        Elem el=Elem.getElem(t);
        setElem(el);
        c1x=c1X;
        c1y=c1Y;
        type=el.name();
        dSS=degrees;
        rSS=Math.PI*degrees/180;
        color=el.getEC();
        frontColor=el.getEFC();
        l=el.getEL();
        w=el.getEW();
        c2x=Math.cos(rSS)*w+c1x;
        c3x=Math.cos(rSS)*l+c2x;
        c2y=Math.sin(rSS)*w+c1x;
        c3y=Math.sin(rSS)*l+c2x;
        c4x=c1x+c3x-c2x;
        c4y=c1y+c3y-c2y;
    }
    public Element(double c1X, double c1Y, double degrees, Elem ty){
        setElem(ty);

        c1x=c1X;
        c1y=c1Y;
        type=t.name();
        dSS=degrees;
        rSS=Math.PI*degrees/180;
        color=t.getEC();
        frontColor=t.getEFC();
        l=t.getEL();
        w=t.getEW();
        c2x=Math.cos(rSS)*w+c1x;
        c3x=Math.cos(rSS)*l+c2x;
        c2y=Math.sin(rSS)*w+c1x;
        c3y=Math.sin(rSS)*l+c2x;
        c4x=c1x+c3x-c2x;
        c4y=c1y+c3y-c2y;
    }
    private void setElem(Elem ty) {
        t=ty;
    }
    public void setPosMove(double x, double y, double deg){
        c1x+=x;
        c1y+=y;
        rSS+=Math.PI*deg/180;
        dSS+=deg;
        c2x=Math.cos(rSS)*w+c1x;
        c3x=Math.cos(rSS)*l+c2x;
        c2y=Math.sin(rSS)*w+c1x;
        c3y=Math.sin(rSS)*l+c2x;
        c4x=c1x+c3x-c2x;
        c4y=c1y+c3y-c2y;
    }
    public double getdSS(){
        return dSS;
    }
    public double getrSS(){
        return rSS;
    }
    public double getc1x(){
        return c1x;
    }
    public double getc1y(){
        return c1y;
    }
    public double getc2x(){
        return c2x;
    }
    public double getc2y(){
        return c2y;
    }
    public double getc3x(){
        return c3x;
    }
    public double getc3y(){
        return c3y;
    }
    public double getc4x(){
        return c4x;
    }
    public double getc4y(){
        return c4y;
    }
    public int getCanvasLocation() {
        return canvasLocation;
    }
    @SuppressWarnings("null")
    public Point2D.Double getc1(){
        Point2D.Double c = null;
        c.setLocation(c1x, c1y);
        return c;
    }
    @SuppressWarnings("null")
    public Point2D.Double getc2(){
        Point2D.Double c = null;
        c.setLocation(c2x, c2y);
        return c;
    }
    @SuppressWarnings("null")
    public Point2D.Double getc3(){
        Point2D.Double c = null;
        c.setLocation(c3x, c3y);
        return c;
    }
    @SuppressWarnings("null")
    public Point2D.Double getc4(){
        Point2D.Double c = null;
        c.setLocation(c4x, c4y);
        return c;
    }
    public Line2D.Double getFront(){
        return new Line2D.Double(getc1(), getc4());
    }
    public Line2D.Double getBack(){
        return new Line2D.Double(getc2(), getc3());
    }
    public Line2D.Double getRight(){
        return new Line2D.Double(getc3(), getc4());
    }
    public Line2D.Double getLeft(){
        return new Line2D.Double(getc1(), getc2());
    }
    public Element returnStatic() {
        return thisEl;
    }
    public double getCorner(int cNum, boolean yCoord){
        if(yCoord) {
            cNum+=4;
        }
        switch(cNum){
            case 1: return c1x;
            case 2: return c2x;
            case 3: return c3x;
            case 4: return c4x;
            case 5: return c1y;
            case 6: return c2y;
            case 7: return c3y;
            default: return c4y;
        }
    }
    public Line2D.Double getLine(int cNum){
        switch(cNum){
            case 1: return getFront();
            case 2: return getRight();
            case 3: return getBack();
            default: return getLeft();
        }
    }
    public Point2D.Double getCenter(){
        double x=0,y=0;
        for(int i=1;i<5;i++) {
            x+=getCorner(i,false);
            y+=getCorner(i,true);
        }
        return new Point2D.Double(x/4.0,y/4.0);
    }
    public double getLength() {
        return l;
    }
    public double getWidth(){
        return w;
    }
    public String getType(){
        return type;
    }
    public Color getColor() {
        return color;
    }
    public Color getFrontColor() {
        return frontColor;
    }
    public void setStatics() {
        thisEl=new Element(c1x, c1y, dSS, type);
    }
    public Color getAColor(int i) {
        if(i==1) {
            return frontColor;
        }
        return color;
    }
    public ArrayList<Line2D.Double> getLines(){
        ArrayList<Line2D.Double> lines=new ArrayList<Line2D.Double>();
        for (int i=1;i<5;i++) {
            lines.add(getLine(i));
        }
        return lines;
    }
    public Color[] getColors() {
        Color[] c= {getFrontColor(), getColor()};
        return c;
    }
}
public enum Elem {
    skystone(8,4,new Color(255,234,86),new Color(135, 135, 15)),
    stone(8,4,new Color(255,234,86)),
    capstone(8,4,new Color(255,234,86)),
    foundation(32,16, new Color(40,40,40)),
    robot(18,18,new Color(125,125,125)),
    post(2,2,new Color(175,175,175)),
    field(144,144,new Color(128,128,0));

    public int length, width; 
    public Color frontColor, color;
    Elem(int l, int w, Color c, Color fC){
        length=l;
        width=w;
        color=c;
        frontColor=fC;
    }
    Elem(int l, int w, Color c){
        length=l;
        width=w;
        color=c;
        frontColor=c;
    }
    int getEL() {
        return length;
    }
    int getEW() {
        return width;
    }
    Color getEFC() {
        return frontColor;
    }
    Color getEC() {
        return color;
    }
    public static Elem getElem(String t) {
        for(Elem e: Elem.values()){
            if(e.name().equalsIgnoreCase(t)) {
                return e;
            }
        }
        return field;
    }
}
  ask by Ankit Verghese translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...