我是java新手。我正在尝试制作小行星克隆游戏,但遇到了麻烦。
我不确定如何将 graphics2d 对象添加回 jframe。任何帮助,将不胜感激。另外,我计划在此基础上添加更多内容,如果看起来有点傻,请见谅。
目前 jframe 绘制但我没有在里面得到任何对象。
主班
import java.applet.*;
import javax.swing.*;
public class Program extends JApplet
{
public void init()
{
String[] s = new String[0];
main(s);
};
public static void main(String[] args)
{
System.out.println("Launching Window");
Window w = new Window();
w.setSize(700, 600);
w.setLocation(50,50);
w.setVisible(true);
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
System.out.println("Launching Game");
Game g = new Game();
while(g.play)
{
System.out.println("Looping");
g.tick();
w.repaint();
};
}
};
Gui Stuff(不确定我是否需要这个或与game.java合并)
import javax.swing.*;
public class Window extends JFrame
{
private JPanel panel;
public Window()
{
panel = new JPanel();
add(panel);
};
};
Game.java,程序的主要逻辑,还不完整
import java.applet.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Game implements KeyListener
{
public static int xmax = 800;
public static int ymax = 600;
private LinkedList<Rock> rocks = new LinkedList();
private LinkedList<Laser> lasers = new LinkedList();
private Ship ship;
private int score = 0;
public boolean play = true;
public Game()
{
ship = new Ship(xmax/2, ymax/2); //Game objects
rocks.add(new Rock(50, 50, 50));
};
public void reinit()
{
ship = null;
rocks.clear();
lasers.clear();
score = 0;
};
public void tick()
{
for(int i = 0; i < rocks.size(); i++) //Check to see if bullets hit unit, remove that unit
{
for(int j = 0; j < lasers.size(); j++)
{
if(rocks.get(i).hit(rocks.get(j)))
{
if(rocks.get(i).size > 20) //if rocks bigger than 20 spawn some more rocks
{
rocks.add(new Rock(rocks.get(j).x, rocks.get(j).y, rocks.get(j).size/3)); //add three rocks on hit
rocks.add(new Rock(rocks.get(j).x, rocks.get(j).y, rocks.get(j).size/3));
rocks.add(new Rock(rocks.get(j).x, rocks.get(j).y, rocks.get(j).size/3));
};
rocks.remove(j); //remove hit rock
lasers.remove(i); //remove laser
};
};
};
for(int i = 0; i < rocks.size(); i++) //move units
{
rocks.get(i).tick();
};
for(int i = 0; i < lasers.size(); i++) //move bullets
{
lasers.get(i).tick();
};
ship.tick();
};
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_SPACE)
lasers.add(new Laser(ship.x, ship.y, ship.angle));
if (e.getKeyCode()==KeyEvent.VK_UP)
ship.thrust();
if (e.getKeyCode()==KeyEvent.VK_LEFT)
ship.left();
if (e.getKeyCode()==KeyEvent.VK_RIGHT)
ship.right();
if (e.getKeyCode()==KeyEvent.VK_Q)
play = false;
};
public void keyReleased(KeyEvent e)
{
};
public void keyTyped(KeyEvent e)
{
};
};
保存对象的通用类
import java.lang.Math;
public class Coords
{
public static double RAD = 0.0174532925;
public static int xmax = 800;
public static int ymax = 600;
public double x = 0.0;
public double y = 0.0;
public double dx = 0.0;
public double dy = 0.0;
public double vel = 0.0;
public double size = 0.0;
public double angle= 0.0;
public double da = 0.0;
public Coords()
{
};
public void reinit()
{
x = 0.0;
y = 0.0;
dx = 0.0;
dy = 0.0;
vel = 0.0;
size = 0.0;
angle = 0.0;
da = 0.0;
};
public void rot()
{
};
public boolean oob(int xmax, int ymax)
{
if((x < xmax) && (x > 0) && (y < ymax) && (y > 0))
return true;
return false;
};
public boolean hit(Coords in)
{
if((in.x + in.size > x) && (in.x < x + size) && (in.y + in.size > y) && (in.y < y + size))
return true;
return false;
};
public void tick()
{
x = (dx + x) % xmax;
y = (dy + y) % ymax;
angle = (angle + da) % 360;
};
};
宇宙飞船级
import java.applet.*;
import java.lang.Math;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
public class Ship extends Coords
{
private ImageIcon imageIcon;
private Image image;
public Ship(double x_in, double y_in)
{
imageIcon = new ImageIcon(getClass().getResource("images/ship.png"));
image = imageIcon.getImage();
x = x_in;
y = y_in;
};
public void thrust()
{
dx = dx + (vel * Math.sin(angle * RAD)); //Move points based on speed
dy = dy + (vel * Math.cos(angle * RAD));
};
public void left()
{
da--;
};
public void right()
{
da++;
};
public void draw(Graphics graphics)
{
Graphics2D g = (Graphics2D)graphics;
g.rotate(angle);
g.drawImage(image, (int)x, (int)y, null);
};
};
激光类
import java.applet.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
public class Laser extends Coords
{
private ImageIcon imageIcon;
private Image image;
public Laser(double x_in, double y_in, double angle_in)
{
imageIcon = new ImageIcon(getClass().getResource("images/laser.png"));
image = imageIcon.getImage();
angle = angle_in;
vel = 20;
};
public void draw(Graphics graphics)
{
Graphics2D g = (Graphics2D)graphics;
g.rotate(angle);
g.drawImage(image, (int)x, (int)y, null);
};
};
小行星类
import java.applet.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.util.Random;
public class Rock extends Coords
{
private ImageIcon imageIcon;
private Image image;
public Rock(double x_in, double y_in, double size_in)
{
imageIcon = new ImageIcon(getClass().getResource("images/rock.png"));
image = imageIcon.getImage();
x = x_in;
y = y_in;
size = size_in;
Random random = new Random();
int r = random.nextInt();
angle = r % 360;
vel = r % 5;
};
public void draw(Graphics graphics)
{
Graphics2D g = (Graphics2D)graphics;
g.rotate(angle);
g.drawImage(image, (int)x, (int)y, null);
};
};