Skip to main content
gamedev.net
🔒 Locked

[java] problems coding a menu

Started by Jakkyl Nov 30, 2002 at 2:20 PM 3 replies 750+ views
Original Post
Jakkyl
Jakkyl
Trying to make a java text menu. When I hit the up or down arrow, my cursor doesn''t move. On nextItem() it goes from 0 to 1, then back to 0. I want it to keep going until it gets to the length of the items in the menu, then go back to 0, and the opposite for previous.
  
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TextMenu extends JPanel
{
	public static final int CENTER = 0;
	public static final int RIGHT = 1;
	public static final int LEFT = 2;
	public static final int WIDTH = 800;
	public static final int HEIGHT = 600;

	private int xC = 50;
	private int yC = (int)(HEIGHT * .3);
	private int justify = -1;

	private String message = "Default Menu Item";
	private Color color = null;
	private int arrowX = getX();
	private int arrowY;
	protected Font font = null;
	protected int currentItem = 0;
	protected int items = 1;
	protected int spacing = 0;
	String[] sItem = null;

	public TextMenu(int items, int spacing, Font font)
	{
		sItem = new String[items];
		this.items = items;
		this.font = font;
		this.setFont(font);
		this.spacing = spacing;
		for (int i = 0; i < items; i++)
			sItem[i] = "Default" + i;
	}

	public int getX() { return xC;}
	public int getY() { return yC; }
	public int getJustify() { return justify; }
	public String getFontFamily() { return font.getFamily(); }
	public String getMenuItem(int index) { return sItem[index]; }

	public void setX(int xC) { this.xC = xC; }
	public void setY(int yC) { this.yC = yC; }
	public void setJustify(int justify) { this.justify = justify; }
	public void setFont(Font font) { this.font = font; }
	public void setMenuColor(Color color) { this.color = color; }
	public void setMenuItemText(String text, int item) { sItem[item] = text; }

	public void display(Graphics g)
	{
		g.setColor(color);
		g.setFont(font);
		FontMetrics fm = g.getFontMetrics(font);
		int w = fm.stringWidth(sItem[0]);
		int h = fm.getAscent();
		int y = getY();

		if (justify == CENTER) {
			setX((WIDTH - w) / 2);
			for (int i = 0; i < items; i++) {
				w = fm.stringWidth(sItem[i]);
				g.drawString(sItem[i], (WIDTH - w) / 2, y);
				y += spacing + h;
			}
		} else if (justify == RIGHT) {
			setX((WIDTH - w));
			for (int i = 0; i < items; i++) {
				w = fm.stringWidth(sItem[i]);
				g.drawString(sItem[i], WIDTH-w, y);
				y += spacing + h;
			}
		} else if (justify == LEFT) {
			setX(50);
			for (int i = 0; i < items; i++) {
				g.drawString(sItem[i], getX(), y);
				y += spacing + h;
			}
		} else {
			for (int i = 0; i < items; i++) {
				g.drawString(sItem[i], getX(), y);
				y += spacing + h;
			}
		}
			arrowY = getY() + ((spacing + h) * currentItem);
			g.drawString(Integer.toString(arrowY),10,30);
			g.drawString("-->",getX() - w + fm.stringWidth(".-"), arrowY);
	}

	public boolean done()
	{
		return false;
	}

    public void nextItem() {
        System.out.println(getCurrentItem() + "getCurrentItem() 1"); //returns 0

        int item = 0;
        if(currentItem + 1 > items) {
            item = 0;
            System.out.println(item + "item if"); //never gets to this point

		} else {
            System.out.println(item + "item else 1"); //returns 0

            item = currentItem + 1;
			System.out.println(item + "item else 2"); //returns 1

		}
        setCurrentItem(item);
        System.out.println(getCurrentItem() + "getCurrentItem() 2"); //returns 1

    }

    public void previousItem() {
        int item = 0;
        if(currentItem == 0)
            item = items;
        else
            item = currentItem - 1;
        setCurrentItem(item);
    }

    public void setCurrentItem(int item) {
        if(item >= 0 && item < items)
        {
            currentItem = item;
        }
    }

	public int getCurrentItem() { return currentItem; }
}
    

  
//relevant source from my test file

	public boolean menu()
	{
		clear();
		menu = new TextMenu(5,10,font);
		menu.setJustify(menu.CENTER);
		menu.setMenuItemText("Blah",0);
		menu.setMenuItemText("Doh",1);
		menu.setMenuColor(Color.white);
		menu.display(g);
		if (menu.done() == true)
			return true;

		return false;
	}

	public void keyPressed(KeyEvent e)
	{
		if (e.getKeyCode() == e.VK_UP) {
			menu.previousItem();
		} else if (e.getKeyCode() == e.VK_DOWN) {
			menu.nextItem();
		} else if(e.getKeyCode() == e.VK_ESCAPE) {
			System.exit(0);
		}
		System.out.println(menu.getCurrentItem());
	}
  
MtK
MtK
I think you should shorten your nextItem function. It should work like you coded it, but simpler means less possibilities for bugs.


  
public void nextItem() {
currentItem++;
if(currentItem > items)
currentItem = 0;
}


this has to increase your counter.
Jakkyl
Jakkyl
Yea, I've been messin' around with it, and I shortened it, lengthened it, changed variables, I still can't figure out why it's not working.

But now it's giving me the outputs of '1' on nextItem() and '5' on previousItem(). I don't understand why it's not giving me 0 and 5, at least...


[edited by - Jakkyl on November 30, 2002 6:17:30 PM]
Jakkyl
Jakkyl
Ok, I changed this :


  
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == e.VK_UP) {
menu.previousItem();
} else if (e.getKeyCode() == e.VK_DOWN) {
menu.nextItem();
} else if(e.getKeyCode() == e.VK_ESCAPE) {
System.exit(0);
}
System.out.println(menu.getCurrentItem());
}


to this:


  
public void keyPressed(KeyEvent e)
{
System.out.println(menu.getCurrentItem());
if (e.getKeyCode() == e.VK_UP) {
menu.previousItem();
} else if (e.getKeyCode() == e.VK_DOWN) {
menu.nextItem();
} else if(e.getKeyCode() == e.VK_ESCAPE) {
System.exit(0);
}
}



and I got a zero each time I hit the key. Before I would get a 0 for down and a 5 for up. So it''s something in there, I think. Still don''t get it though. Suggestions?
Endurion
Endurion


change this:



public void previousItem()
{
int item = 0;
if(currentItem == 0)
// item = items;
item = items - 1;
else
item = currentItem - 1;
setCurrentItem(item);
}

public void nextItem()
{
int item = 0;
//if(currentItem + 1 > items)
if(currentItem + 1 >= items)
{
item = 0;
}
else
{
item = currentItem + 1;
}
setCurrentItem(item);
}


Overly complicated but should work, you just have to decide whether to use items as 0 to itemcount - 1, or 1 to itemcount.
The check in setCurrentItem tricked yourself, since it was checking and not setting the (wrong) item number.

Regards,
Endurion
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.