I have been working on a simple 2D Java top down shooter for a very long time. Until now I have had a graphics2D main menu that is just rendered until the user presses the enter key.
I would like to have a working menu system where three clickable areas (play, options,exit) that take you to other screens or menus. I have attempted to do this with swing but have so far been unable to succeed.
This is the current drawing class. The render method checks which menu (int) it should draw and then proceeds with all of the g.draw... needed.
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();
}
How can this be done the most efficient way with or without swing being involved?