Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Liitle help with Jmenu

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Liitle help with Jmenu

    Hi friends... I am haing some problem making jmenu for the following code. The public void fill method is what i want to be in jMenu
    the user will choose what kind of shape he wants.. rect or oval.. i just put system.out.println for some test. any help will be appreciated


     
     
     
    import java.awt.Rectangle;
    import java.awt.Graphics;
    import java.awt.Color;
    import java.awt.Point;
    import java.awt.Insets;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.util.Vector;
    import java.applet.AudioClip;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
     
     
     
     
    public class Snake extends JFrame implements Runnable {
    /*****************************************/
    	Vector<Piece> snake = new Vector<Piece>();
    	Piece food;
    	Piece head;
    	Thread thread;
    	Rectangle bounds;
     
    	static AudioClip eat;
     
    	static {
    		ClassLoader cl = Snake.class.getClassLoader();
    		eat = java.applet.Applet.newAudioClip(cl.getResource("sounds/Burp.wav"));
    	}
     
    	int score;
    	int direction;
    	int prevDirection;
     
    	final int UP =		0;
    	final int DOWN =	1;
    	final int LEFT =	2;
    	final int RIGHT =	3;
     
    	int pieceSize = 10;
     
    	int speed = 3;
     
    	int fieldWidth = 30;
    	int fieldHeight = 30;
     
    	boolean inited = false;
     
    	public Snake() {
     
    		super("Snake");
     
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setResizable(false);
    		setVisible(true);
     
    		init();
     
    		Insets in = getInsets();
     
    		setSize(in.left + in.right + bounds.width + 20, in.top + in.bottom + bounds.height + 20);
     
    		start();
    	}
     
    	private void init() {
     
    		prevDirection = RIGHT;
     
    		direction = RIGHT;
     
    		snake = new Vector<Piece>();
     
    		food = null;
     
    		Insets in = getInsets();
     
    		bounds = new Rectangle();
    		bounds.x = in.left + 10;
    		bounds.y = in.top + 10;
    		bounds.width = fieldWidth * pieceSize;
    		bounds.height = fieldHeight * pieceSize;
     
    		int nPieces = 10;
     
    		for (int i = nPieces; --i >= 0;)
    			snake.addElement(new Piece(i * nPieces + bounds.x, pieceSize * nPieces + bounds.y, pieceSize));
    		head = snake.firstElement();
     
    		addKeyListener(new KeyAdapter() {
     
    			public void keyPressed(KeyEvent e) {
    				switch (e.getKeyCode()) {
    					case KeyEvent.VK_UP:
    						direction = UP; break;
    					case KeyEvent.VK_DOWN:
    						direction = DOWN; break;
    					case KeyEvent.VK_LEFT:
    						direction = LEFT; break;
    					case KeyEvent.VK_RIGHT:
    						direction = RIGHT; break;
    				}
    			}
    		});
     
    		score = 0;
     
    		inited = true;
    	}
     
    	public Vector<Piece> getSpaces() {
    		int total = fieldHeight * fieldWidth;
    		Vector<Piece> spaces = new Vector<Piece>(total);
     
    		for (int i = 0; i < total; i++) {
    			int x = i % fieldHeight * pieceSize + bounds.x;
    			int y = i / fieldWidth * pieceSize + bounds.y;
    			Piece sq = new Piece(x, y, pieceSize);
    			if (snake.indexOf(sq) == -1)
    				spaces.addElement(sq);
    		}
    		return spaces;
    	}
     
     
     
    	public void paint(Graphics g) {
    		super.paint(g);
    		onPaint();
    	}
     
    	private void onPaint() {
    		Graphics g = getGraphics();
    		if (!inited) {
    			return;
    		}
     
    		g.setColor(Color.red);
     
    		//g.drawOval(bounds.x, bounds.y, bounds.width, bounds.height);
    		g.drawRect(bounds.x, bounds.y, bounds.width, bounds.height);
     
    		g.setColor(Color.gray);
     
    		if (food == null) feed(g);
    		else food.fill(g);
     
    		Piece newHead = new Piece(head.x, head.y, pieceSize);
    		switch (direction) {
    			case UP:
    				if (prevDirection != DOWN) newHead.y -= pieceSize;
    				else {
    					direction = prevDirection;
    					newHead.y += pieceSize;
    				}
    				break;
    			case LEFT:
    				if (prevDirection != RIGHT) newHead.x -= pieceSize;
    				else {
    					direction = prevDirection;
    					newHead.x += pieceSize;
    				}
    				break;
    			case DOWN:
    				if (prevDirection != UP) newHead.y += pieceSize;
    				else {
    					direction = prevDirection;
    					newHead.y -= pieceSize;
    				}
    				break;
    			case RIGHT:
    				if (prevDirection != LEFT) newHead.x += pieceSize;
    				else {
    					direction = prevDirection;
    					newHead.x -= pieceSize;
    				}
    				break;
    		}
    		if (newHead.equals(food)) {
    			head = food;
    			feed(g);
    			eat.stop();
    			eat.play();
    			score += speed;
    		} else {
    			Piece tail = snake.elementAt(snake.size()-1);
    			tail.clear(g);
    			snake.removeElementAt(snake.size()-1);
     
    			head = newHead;
    			if (checkHit(head)) {
    				stop();
    				int b = JOptionPane.showConfirmDialog(this, "want to play again: " + score +
    													"Score?", "Snake", JOptionPane.YES_NO_OPTION);
    				if (b == JOptionPane.YES_OPTION) {
    					clear(g);
    					init();
    					start();
    				} else	System.exit(0);
    				return;
    			}
    		}
    		prevDirection = direction;
    		snake.insertElementAt(head, 0);
     
    		for (int i = 0; i < snake.size(); i++) {
    			snake.elementAt(i).fill(g);
    		}
    	}
     
    	private boolean checkHit(Piece square) {
     
    		for (Piece s: snake)
    			if (s.equals(square))
    				return true;
     
    		if (square.y < bounds.y || square.y >= (bounds.height + bounds.y) ||
    			square.x < bounds.x || square.x >= (bounds.width + bounds.x))
    				return true;
     
    		return false;
    	}
     
    	private void feed(Graphics g) {
    		Vector<Piece> spaces = getSpaces();
    		if (spaces.size() > 0) {
    			food = spaces.elementAt((int)(Math.random() * spaces.size()));
    			food.fill(g);
    		} else {
    			stop();
    			JOptionPane.showMessageDialog(this, "Galing mo, man!");
    			System.exit(0);
    		}
    	}
     
    	private void move() {
    		repaint();
    	}
     
    	public void start() {
    		thread = new Thread(this);
    		thread.start();
    	}
     
    	public void stop() {
    		thread = null;
    		inited = false;
    	}
     
    	public void run() {
    		while (thread != null && thread == Thread.currentThread()) {
    			try {
    				thread.sleep(1000 / (5 * (speed-1) + 1));
    			} catch (InterruptedException e) {};
    			move();
    		}
    	}
     
    	private void clear(Graphics g) {
    		for (Piece s: snake)
    			s.clear(g);
    		if (food != null) food.clear(g);
    	}
     
     
    	private class Piece {
    		public int x;
    		public int y;
    		public int size;
    		Piece(int x, int y, int size) {
    			this.x = x;
    			this.y = y;
    			this.size = size;
    		}
    		public void move(int x, int y) {
    			this.x = x;
    			this.y = y;
    		}
     
                     public void fill(Graphics g) {
    		System.out.println("press 1 for oval:");
    		int l = 0;
    		switch(l){
    		case 1: g.drawOval(x+1, y+1, size-2, size-2);
    	break;
    		case 2: g.drawRect(x+1, y+1, size-2, size-2);
    		break;
    		//g.drawRect(x+1, y+1, size-2, size-2);
    		}
    		}
    		public void clear(Graphics g) {
    			g.clearRect(x+1, y+1, size-2, size-2);
    		}
    		public String toString() {
    			return "x=" + x +
    					",y=" + y +
    					",size=" + size;
    		}
    		public boolean equals(Object o) {
    			if (o instanceof Piece) {
    				Piece sq = (Piece)o;
    				return sq.x == x && sq.y == y && sq.size == size;
    			}
    			return false;
    		}
    	}
     
     
    	public static void main(String[] args) {
     
    		new Snake();
     
    	}
    }
    Last edited by cute; November 13th, 2011 at 05:43 PM. Reason: was not highlighting


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Liitle help with Jmenu

    The public void fill method is what i want to be in jMenu
    Can you explain what you are trying to do?
    Menus usually contain menu items.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Liitle help with Jmenu

    sure...
    the fill method that i have is
    public void fill(Graphics g) {
    		System.out.println("press 1 for oval:");
    		int l = 0;
    		switch(l){
    		case 1: g.drawOval(x+1, y+1, size-2, size-2);
    	 break;
    		case 2: g.drawRect(x+1, y+1, size-2, size-2);
    		break;
    		//g.drawRect(x+1, y+1, size-2, size-2);
    		}

    if I just put g.drawRect(x+1, y+1, size-2, size-2);

    the code is working perfectly fine. Now what i want is give user the option of choosing the shape of the snnake either rect or oval. to do that i want to make a menu and from that menu user will decide the shape.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Liitle help with Jmenu

    Have you read the tutorial about how to create menus?
    That would be a good place to start. Go to this site and Find Menu:
    The Really Big Index

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Liitle help with Jmenu

    so on this code.. its not possible to do anything to give the user permission.. all the menu items that i read ( it's hard to implement those in this code.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Liitle help with Jmenu

    give the user permission..
    What does the user need permission for?

    Sorry, I have no idea what your problem is.

  7. #7
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Liitle help with Jmenu

    let's see if I can make it more clear. so the following code gives the snake rectangular shape.
    g.drawRect(x+1, y+1, size-2, size-2);

    what I want is make some kind of option for user to choose whatever shape they want to have either rectangle or oval: some thing like this:
    public void fill(Graphics g) {
    		System.out.println("press 1 for oval:");
    		int l = 0;
    		switch(l){
    		case 1: g.drawOval(x+1, y+1, size-2, size-2);
    	       break;
    		case 2: g.drawRect(x+1, y+1, size-2, size-2);
    		break;
    		//g.drawRect(x+1, y+1, size-2, size-2);
    		}
    you understand now ???

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Liitle help with Jmenu

    what I want is make some kind of option for user to choose whatever shape
    There are many GUI components that would allow the user to choose a shape.
    A menu item is one.
    You need to look at the choice of components and chose one that will work with your program.

  9. #9
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Liitle help with Jmenu

    I know that my friend but I am not too familier with GUI that's why I am asking for help... Hard for me to figure out which component is good for my code

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Liitle help with Jmenu

    What is wrong with the menu?

  11. #11
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Liitle help with Jmenu

    hmm i don't have menu.. that's what i am trying to make.. i show you the code too... how it is and what i want

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Liitle help with Jmenu

    Can you explain what your problem is with adding a menu to your program?
    If you are getting errors, please copy and paste here the full text of the error messages.

Similar Threads

  1. Java program to Add a JMenu toolbar to a Java Swing application
    By JavaPF in forum Java Swing Tutorials
    Replies: 6
    Last Post: March 6th, 2012, 12:25 PM
  2. setVerifyInputWhenFocusTarget problem with JMenu
    By Jack_Maloney in forum AWT / Java Swing
    Replies: 0
    Last Post: March 25th, 2011, 10:44 AM
  3. jmenu&jframe problem
    By beni.vd in forum AWT / Java Swing
    Replies: 1
    Last Post: January 2nd, 2011, 12:11 PM
  4. JMenu and JComboBox
    By javapenguin in forum AWT / Java Swing
    Replies: 1
    Last Post: July 3rd, 2010, 05:00 PM
  5. Java program to Add a JMenu toolbar to a Java Swing application
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 3
    Last Post: April 30th, 2010, 09:00 AM