基本上,我正在尝试在Java中绘制一堆线,以便以后可以使用代码来操纵位置。
这是我用于绘画组件的代码:
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);
}
}
}
要初始化此类并将其添加到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,"field");
field1.setStatics();
rectDrawer d =new rectDrawer();
screen.add(d);
}
}
我真的不知道这有什么问题,从理论上讲,它应该在JFrame上绘制一个432 x 432的正方形,但是我得到的只是一个空白的JFrame。如果有问题,我将附加我的项目的其余部分(除了进口)。谢谢!
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,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,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,86)),capstone(8,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,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;
}
}