我正在尝试制作一个游戏,其中您必须使火箭飞过一个山洞,如果撞到墙壁,火箭就会崩溃。为了检测碰撞,我决定将CaveWall位置和火箭位置存储在对象中。当我尝试根据这些对象的位置绘制线条时,什么也没有发生。代码如下。
public class Rocket{//this class is the rocket object
private double x,y;
public Rocket(double x, double y) {
this.x=x;
this.y=y;
}
public class CaveWalls {//This class is the cavewalls object
private int startx;
private int starty;
private int endx;
private int endy;
public CaveWalls(int startx, int starty, int endx, int endy) {
this.startx=startx;
this.starty=starty;
this.endx=endx;
this.endy=endy;
}
public class ConfigureWalls extends JComponent{//This class sets the list of cavewalls
private List <CaveWalls> wall=new ArrayList<CaveWalls>();
public void one() {
CaveWalls wall1=new CaveWalls(0, getHeight()-(getHeight()/6), getWidth()/6, getHeight()-(getHeight()/6));
CaveWalls wall2=new CaveWalls(getWidth()/6, getHeight()-(getHeight()/6), getWidth()/6, getHeight()-getHeight()/4);
CaveWalls wall3=new CaveWalls(getWidth()/6, getHeight()-getHeight()/4, getWidth()/6+getWidth()/10, getHeight()-getHeight()/4);
CaveWalls wall4=new CaveWalls(getWidth()/6+getWidth()/10, getHeight()-getHeight()/4, getWidth()/6+getWidth()/10, getHeight()-getHeight()/12);
CaveWalls wall5=new CaveWalls(getWidth()/6+getWidth()/10, getHeight()-getHeight()/12, getWidth()/2, getHeight()-getHeight()/12);
CaveWalls wall6=new CaveWalls(getWidth()/2, getHeight()-getHeight()/12, getWidth()/2, getHeight()-getHeight()/4);
CaveWalls wall7=new CaveWalls(getWidth()/2, getHeight()-getHeight()/4, getWidth()/3+getWidth()/15, getHeight()-getHeight()/4);
CaveWalls wall8=new CaveWalls(getWidth()/3+getWidth()/15, getHeight()-getHeight()/4, getWidth()/3+getWidth()/15, getHeight()-getHeight()/3);
CaveWalls wall9=new CaveWalls(getWidth()/3+getWidth()/15, getHeight()-getHeight()/3, getWidth()/2+getWidth()/7, getHeight()-getHeight()/3);
CaveWalls wall10=new CaveWalls(getWidth()/2+getWidth()/7, getHeight()-getHeight()/3, getWidth()/2+getWidth()/7, getHeight()-getHeight()/6);
CaveWalls wall11=new CaveWalls(getWidth()/2+getWidth()/7, getHeight()-getHeight()/6, getWidth()-getWidth()/4 , getHeight()-getHeight()/6);
CaveWalls wall12=new CaveWalls(getWidth()-getWidth()/4, getHeight()-getHeight()/6, getWidth()-getWidth()/4, getHeight()-getHeight()/4);
CaveWalls wall13=new CaveWalls(getWidth()-getWidth()/4, getHeight()-getHeight()/4, getWidth()-getWidth()/7, getHeight()-getHeight()/4);
CaveWalls wall14=new CaveWalls(getWidth()-getWidth()/7, getHeight()-getHeight()/4, getWidth()-getWidth()/7, getHeight()-getHeight()/8);
CaveWalls wall15=new CaveWalls(getWidth()-getWidth()/7, getHeight()-getHeight()/8, getWidth(), getHeight()-getHeight()/8);
wall.add(wall1);
wall.add(wall2);
wall.add(wall3);
wall.add(wall4);
wall.add(wall5);
wall.add(wall6);
wall.add(wall7);
wall.add(wall8);
wall.add(wall9);
wall.add(wall10);
wall.add(wall11);
wall.add(wall12);
wall.add(wall13);
wall.add(wall14);
wall.add(wall15);
}
这是我的绘画组件方法的1级课程
public class Level1 extends JComponent implements KeyListener,ActionListener {
Rocket rocket=new Rocket(getWidth()/12-10, getHeight()-(getHeight()/4)-20);
ConfigureWalls config=new ConfigureWalls();
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2=(Graphics2D) g;
config.one();
for(int x=0; x<config.getWall().size();x++) {
g2.drawLine(config.getWall().get(x).getStartx(), config.getWall().get(x).getStarty(), config.getWall().get(x).getEndx(), config.getWall().get(x).getEndy());
}
g.fillOval((int)rocket.getX(), (int)rocket.getY(), 10,10);
}
这是我的主要内容,但JFrame上没有任何内容
public class Test {
public static void main(String[] args) {
JFrame frame=new JFrame();
frame.setSize(1080,720);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Level1 one=new Level1();
frame.add(one);
frame.setVisible(true);
}
}