0

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?

piechesse
  • 39
  • 1
  • 7
  • 1
    `" I have attempted to do this with swing but have so far been unable to succed."` -- as we usually say, please show your code attempts, please explain in detail your problems. Else you might be accused of asking us to create a complete tutorial, and your question might be closed for being too broad. – Hovercraft Full Of Eels Oct 01 '13 at 00:53
  • What do you want the menu to look like? Simple `JButton`s, fancy `JPanel`s? – MadProgrammer Oct 01 '13 at 00:54
  • I added the Graphics2D attempt, and am only asking what would be the most efficient way to draw different menus. – piechesse Oct 01 '13 at 01:05
  • It doesn't seem to me that *efficient* really matters here. Are you sure that this part of your code is a bottleneck? Perhaps you wish to explain better what your goal with this question is. – Hovercraft Full Of Eels Oct 01 '13 at 01:09
  • I am also curious though why you're mixing AWT with Swing components. This is generally frowned upon unless done for a specific purpose. What is your purpose behind this? – Hovercraft Full Of Eels Oct 01 '13 at 01:10
  • The only AWT component to my knowledge is the canvas and that is only present for the actual drawing. The render method is ran along side the various other update methods in the game thread. I am well aware that the coding is not efficient which is why the above stated question was asked? – piechesse Oct 01 '13 at 01:17
  • *"The only AWT component to my knowledge is the canvas.."* So remove it. We can use an extended `JPanel` to draw to, though I typically draw to an image and display the image in a `JLabel`. – Andrew Thompson Oct 01 '13 at 02:18

0 Answers0