views:

28

answers:

2
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

@SuppressWarnings("serial")
public class Main extends JFrame {

    final int FRAME_HEIGHT = 400;
    final int FRAME_WIDTH = 400;

    public static void main(String args[]) {
        new Main();
    }

    public Main() {
        super("Game");

        GameCanvas canvas = new GameCanvas();

        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        JMenuItem startMenuItem = new JMenuItem("Pause");
        menuBar.add(fileMenu);
        fileMenu.add(startMenuItem);

        getContentPane().add(canvas);
        super.setVisible(true);
        super.setSize(FRAME_WIDTH, FRAME_WIDTH);
        super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        super.setJMenuBar(menuBar);
    }
}


import java.awt.Canvas;
import java.awt.Graphics;

import javax.swing.JPanel;

@SuppressWarnings("serial")
public class GameCanvas extends JPanel {

    public void paint(Graphics g) {
        g.drawString("hI", 0, 0);
    }
}

This code causes the string to appear behind the JMenuBar. To see the string, you must draw it at (0,10). I'm sure this must be something simple, so do you guys have any ideas?

+3  A: 

Try

FontMetrics fm = g.getFontMetrics();
g.drawString("hI", 10, fm.getMaxAscent());

as suggested by the diagram in FontMetrics.

Addendum: @RaviG is right, too.

trashgod
Gah! I never though about to coords being that of the baseline. >__>Thanks alot!
Matt H
I have to refer to that diagram frequently. :-)
trashgod
+2  A: 

You have not added your canvas to the frame.

In your constructor, at the end,

add getContentPane().add(canvas);

RaviG
Sorry, that was a minor hiccup in the code I'd posted. I had played around with the code before posting it, you see, and I removed certain parts in the process, forgetting to add them back in when I made this question.trashgod's solution is correct, though.
Matt H
+1 `this.add(canvas)` also works via forwarding.
trashgod