我一直在研究一款简单的2D Java自顶向下射击游戏。到目前为止,我已经有了一个graphics2D主菜单,只有在用户按下回车键时才会显示。
我希望有一个工作菜单系统,其中有三个可点击的区域(播放,选项,退出),可以将您带到其他屏幕或菜单。我试图用摇摆来做到这一点,但到目前为止还没有成功。
这是当前的绘图课程。 render方法检查它应该绘制哪个菜单(int),然后继续使用所有需要的g.draw。
public class Draw extends Canvas {
private static final long serialVersionUID = 1L;
JFrame frame;
Canvas canvas;
JPanel panel;
private Font customFont;
BufferStrategy bufferStrategy;
public static int WIDTH = 640;
public static int HEIGHT = 640;
public static int menu = 0;
Game game;
@SuppressWarnings("unused")
private BufferedImage titleImage;
Info infoFrame;
public Draw(Game game) {
initFont();
this.game = game;
this.frame = new JFrame("A Hard CUBE Life");
panel = (JPanel) frame.getContentPane();
panel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
panel.setLayout(null);
this.canvas = new Canvas();
this.canvas.setBounds(0, 0, WIDTH, HEIGHT);
this.canvas.setIgnoreRepaint(true);
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
infoFrame = new Info(game);
this.infoFrame.setLocation(0, 540);
this.frame.add(this.infoFrame);
this.frame.pack();
this.frame.setResizable(false);
this.frame.setVisible(true);
panel.add(this.canvas);
this.canvas.createBufferStrategy(2);
this.bufferStrategy = this.canvas.getBufferStrategy();
this.canvas.requestFocus();
this.canvas.setBackground(Color.GRAY);
this.canvas.addKeyListener(new ButtonHandler(game));
try {
titleImage = ImageIO.read(new File("res/title.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void render() {
Graphics2D g = (Graphics2D) this.bufferStrategy.getDrawGraphics();
if (menu == 0) {
g.clearRect(0, 0, 640, 640);
g.setFont(customFont);
// g.drawImage(titleImage, 0, 0, canvas);
g.drawString("A Hard", 100, 100);
g.drawString("Cube's Life", 150, 150);
g.drawString("(Press Enter)", 225, 450);
g.setFont(customFont.deriveFont(20f));
g.drawString("Version: " + "2.1.1", 10, 530);
g.drawString("Designed and Programmed by: Micky Lindsay", 310, 530);
g.setColor(new Color(200, 35, 35));
g.fillRect(400, 100, 100, 100);
g.setColor(Color.BLACK);
g.setStroke(new BasicStroke(3.0F));
g.drawRect(420, 120, 15, 15);
g.drawRect(465, 120, 15, 15);
g.drawRect(430, 165, 40, 5);
g.setColor(Color.WHITE);
g.fillRect(100, 400, 25, 25);
g.setColor(Color.BLACK);
g.drawRect(110, 403, 5, 5);
g.setColor(Color.YELLOW);
g.fillRect(112, 365, 3, 3);
g.fillRect(112, 375, 3, 3);
g.fillRect(112, 385, 3, 3);
}
if (menu == 1) {
g.setStroke(new BasicStroke(1.0F));
Font font = new Font("Arial", 1, 15);
g.setFont(font);
g.clearRect(0, 0, WIDTH, HEIGHT);
for (Cube e : game.cubes) {
e.render(g);
}
for (Bullet b : game.bullets) {
b.render(g);
}
}
this.infoFrame.displayDetails();
if (menu == 2) {
g.setFont(new Font("Ariel", 1, 50));
g.setColor(Color.LIGHT_GRAY);
g.fillRect(125, 250, 360, 70);
g.setColor(Color.GRAY);
g.drawString("GAME OVER", 150, 300);
}
g.dispose();
this.bufferStrategy.show();
}
public void initFont() {
try {
// create the font to use. Specify the size!
customFont = Font.createFont(Font.TRUETYPE_FONT,
new File("res/Andy Bold.ttf")).deriveFont(50f);
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
// register the font
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File(
"res/Andy Bold.ttf")));
} catch (IOException e) {
e.printStackTrace();
} catch (FontFormatException e) {
e.printStackTrace();
}
如果涉及或不涉及挥杆,如何以最有效的方式完成这项工作?