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 3 of 3

Thread: my code isn't working an i can't figure out why

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default my code isn't working an i can't figure out why

    i am a self learner becuase my high school doesn't have any programming class and i wanted to learn so i am working on my first self made game application and have a problem with the main menu for it

    main class
    import java.awt.event.*;
    import java.awt.*;
     
    public class main extends Core implements KeyListener,MouseMotionListener,MouseListener,MouseWheelListener {
    	public static void main(String[] args){
    		new main().run();
    	}
     
    	private String mess= "";
    	boolean main_menu, started, hovered, menu_option1, menu_option2;
    	int mx,my,height,width;
     
    	public void init(){
    		main_menu = true;
    		super.init();
    		Window w = s.getFullScreenWindow();
    		w.addMouseListener(this);
    		w.addMouseMotionListener(this);
    		w.addMouseWheelListener(this);
    		w.addKeyListener(this);
    		height = s.getHeight();
    		width = s.getWidth();
    	}
     
    	public synchronized void draw(Graphics2D g){
    		Window w = s.getFullScreenWindow();
    		if(main_menu && !started){
    			g.setColor(w.getBackground());
    			g.fillRect(0,0,s.getWidth(),s.getHeight());
    			g.setColor(w.getForeground());
    			g.drawString(mess,40,50);
    			Font font1 = new Font("Arial", Font.BOLD, 25);
    			g.setFont(font1);
    			g.drawString("Welcome to the main menu.",250,200);
    			Font font2 = new Font("Arial", Font.BOLD, 16);
    			g.setFont(font2);
    			if (hovered) {
    				if(menu_option1){
    					g.setColor(Color.red);
    				}
    			}
    			g.drawString("Play Game",width / 2 + 300, 250);
    			g.setColor(w.getForeground());
    			if (hovered) {
    				if(menu_option2){
    					g.setColor(Color.red);
    				}
    			}
    			g.drawString("Replays",width / 2 + 300, 280);
    		}else {
    			g.setColor(w.getBackground());
    			g.fillRect(0,0,s.getWidth(),s.getHeight());
    			g.setColor(w.getForeground());
    			g.drawString(mess,40,50);
    			Font font2 = new Font("Arial", Font.BOLD, 16);
    			g.setFont(font2);
    			if(hovered){
    				g.setColor(Color.red);
    			}
    			g.drawString("Back",width / 2 - 61, 250);
    		}
    	}
     
    	public void mousePressed(MouseEvent e){
    		if (main_menu && hovered) {
    			main_menu = false;
    			started = true;
    			hovered = false;
    		}else if(started && hovered){
    			main_menu = true;
    			started = false;
    			hovered = false;
    		}
    		mess = "You pressed down the mouse";
    	}
     
    	public void mouseReleased(MouseEvent e){
    		mess = "You released the mouse";
    	}
     
    	public void mouseClicked(MouseEvent e){}
    	public void mouseEntered(MouseEvent e){}
    	public void mouseExited(MouseEvent e){}
     
    	public void mouseDragged(MouseEvent e){
    		mess = "You are dragging the mouse";
    	}
     
    	public void mouseMoved(MouseEvent e){
    		mx = e.getX();
    		my = e.getY();
    		if (main_menu) {
    			if (mx >= width / 2 + 300 && mx <= width / 2 + 380 && my >= 227 && my <= 250) {
    				menu_option1 = true;
    				hovered = true;
    			}
    			else {
    				menu_option1 = false;
    				hovered = false;
    			}
    			if (mx >= width / 2 + 300 && mx <= width / 2 + 360 && my >= 265 && my <= 280) {
    				menu_option2 = true;
    				hovered = true;
    			}
    			else {
    				menu_option2 = false;
    				hovered = false;
    			}
    		}else if(started){
    			if (mx >= width / 2 - 61 && mx <= width / 2 + 61 && my >= 227 && my <= 250) {
    				hovered = true;
    			}
    			else {
    				hovered = false;
    			}
    		}
    		mess = "You are moving the mouse";
    	}
     
    	public void mouseWheelMoved(MouseWheelEvent e){
    		mess = "Moving mouse wheel";
    	}
     
    	public void keyPressed(KeyEvent e){
    		int keyCode = e.getKeyCode();
    		if(keyCode == KeyEvent.VK_ESCAPE){
    			stop();
    		}else{
    			mess = "Pressed : "+ KeyEvent.getKeyText(keyCode);
    			e.consume();
    		}
    	}
     
    	public void keyReleased(KeyEvent e){
    		int keyCode = e.getKeyCode();
    		mess = "Released : "+ KeyEvent.getKeyText(keyCode);
    		e.consume();
    	}
     
    	public void keyTyped(KeyEvent e){
    		e.consume();
    	}
    }

    Core.java file
    import java.awt.*;
    import javax.swing.*;
     
    public abstract class Core {
     
    	private static DisplayMode modes[] = {
    		new DisplayMode(800,600,32,0),
    		new DisplayMode(800,600,24,0),
    		new DisplayMode(800,600,16,0),
    		new DisplayMode(640,480,32,0),
    		new DisplayMode(640,480,24,0),
    		new DisplayMode(640,480,16,0),
    	};
    	private boolean running;
    	protected ScreenManager s;
     
    	public void stop(){
    		running = false;
    	}
     
    	public void run(){
    		try{
    			init();
    			gameLoop();
    		}finally{
    			s.restoreScreen();
    		}
    	}
     
    	public void init(){
    		s = new ScreenManager();
    		DisplayMode dm = s.findFirstCompatibleMode(modes);
    		s.setFullScreen(dm);
     
    		Window w = s.getFullScreenWindow();
    		w.setFont(new Font("Arial", Font.PLAIN,20));
    		w.setBackground(Color.GREEN);
    		w.setForeground(Color.WHITE);
    		running = true;
    	}
     
    	public void gameLoop(){
    		long startTime = System.currentTimeMillis();
    		long cumTime = startTime;
     
    		while(running){
    			long timePassed = System.currentTimeMillis() - cumTime;
    			cumTime += timePassed;
     
    			update(timePassed);
     
    			Graphics2D g = s.getGraphics();
    			draw(g);
    			g.dispose();
    			s.update();
     
    			try{
    				Thread.sleep(20);
    			}catch(Exception ex){}
    		}
    	}
     
    	public void update(long timePassed){}
     
    	public abstract void draw(Graphics2D g);
    }

    ScreenManager.java file
    import java.awt.*;
    import java.awt.image.*;
    import java.lang.reflect.*;
    import javax.swing.*;
     
    public class ScreenManager {
     
    	private GraphicsDevice vc;
    	public ScreenManager(){
    		GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
    		vc = e.getDefaultScreenDevice();
    	}
     
    	public DisplayMode[] getCompatibleDisplayModes(){
    		return vc.getDisplayModes();
    	}
     
    	public DisplayMode findFirstCompatibleMode(DisplayMode modes[]){
    		DisplayMode goodModes[] = vc.getDisplayModes();
    		for(int x=0;x<modes.length;x++){
    			for(int y=0;y<goodModes.length;y++){
    				if(displayModesMatch(modes[x], goodModes[y])){
    					return modes[x];
    				}
    			}
    		}
    		return null;
    	}
     
    	public DisplayMode getCurrentDisplayMode(){
    		return vc.getDisplayMode();
    	}
     
    	public boolean displayModesMatch(DisplayMode m1, DisplayMode m2){
    		if(m1.getWidth() != m2.getWidth() || m1.getHeight() != m2.getHeight()){
    			return false;
    		}
    		if(m1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m2.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m1.getBitDepth() != m2.getBitDepth()){
    			return false;
    		}
    		if(m1.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m2.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m1.getRefreshRate() != m2.getRefreshRate()){
    			return false;
    		}
     
    		return true;
    	}
     
    	public void setFullScreen(DisplayMode dm){
    		JFrame f = new JFrame();
    		f.setUndecorated(true);
    		f.setIgnoreRepaint(true);
    		f.setResizable(false);
    		vc.setFullScreenWindow(f);
     
    		if(dm != null && vc.isDisplayChangeSupported()){
    			try{
    				vc.setDisplayMode(dm);
    			}catch(Exception ex){}
    		}
    		f.createBufferStrategy(2);
    	}
     
    	public Graphics2D getGraphics(){
    		Window w = vc.getFullScreenWindow();
    		if(w != null){
    			BufferStrategy s = w.getBufferStrategy();
    			return (Graphics2D)s.getDrawGraphics();
    		}else{
    			return null;
    		}
    	}
     
    	public void update(){
    		Window w = vc.getFullScreenWindow();
    		if(w != null){
    			BufferStrategy s = w.getBufferStrategy();
    			if(!s.contentsLost()){
    				s.show();
    			}
    		}
    	}
     
    	public Window getFullScreenWindow(){
    		return vc.getFullScreenWindow();
    	}
     
    	public int getWidth(){
    		Window w = vc.getFullScreenWindow();
    		if(w != null){
    			return w.getWidth();
    		}else{
    			return 0;
    		}
    	}
     
    	public int getHeight(){
    		Window w = vc.getFullScreenWindow();
    		if(w != null){
    			return w.getHeight();
    		}else{
    			return 0;
    		}
    	}
     
    	public void restoreScreen(){
    		Window w = vc.getFullScreenWindow();
    		if(w != null){
    			w.dispose();
    		}
    		vc.setFullScreenWindow(null);
    	}
     
    	public BufferedImage createCompatibleImage(int w, int h, int t){
    		Window win = vc.getFullScreenWindow();
    		if(win != null){
    			GraphicsConfiguration gc = win.getGraphicsConfiguration();
    			return gc.createCompatibleImage(w,h,t);
    		}
    		return null;
    	}
     
     
    }

    so far everything works perfectly accept for the menu in the main class file

    this bit of code
    public void mouseMoved(MouseEvent e){
    		mx = e.getX();
    		my = e.getY();
    		if (main_menu) {
    			if (mx >= width / 2 + 300 && mx <= width / 2 + 380 && my >= 227 && my <= 250) {
    				menu_option1 = true;
    				hovered = true;
    			}
    			else {
    				menu_option1 = false;
    				hovered = false;
    			}
    			if (mx >= width / 2 + 300 && mx <= width / 2 + 360 && my >= 265 && my <= 280) {
    				menu_option2 = true;
    				hovered = true;
    			}
    			else {
    				menu_option2 = false;
    				hovered = false;
    			}
    		}else if(started){
    			if (mx >= width / 2 - 61 && mx <= width / 2 + 61 && my >= 227 && my <= 250) {
    				hovered = true;
    			}
    			else {
    				hovered = false;
    			}
    		}
    		mess = "You are moving the mouse";
    	}

    for some reason the menu option 2 is overiding the menu option 1 so can it only be used once and i have to redo the design for my game or is there just a stupid mistake i made?

    if i have to redo it can you guys help me figure out a new way to make the menu?

    i just can not for the life of me fgure this out
    Last edited by jack13580; October 17th, 2011 at 09:54 PM.


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: my code isn't working an i can't figure out why

    Are you sure this is the only part where it's actually doing this? What are your inputs and how did your application behave in different cases? Give some successful cases and failed cases so that it could be traced.
    For now, the code you pasted for problems, seems to be working good logically and syntactically to me.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: my code isn't working an i can't figure out why

    what i mean is that when you start the program when you hover the mouse over replays it works but when yuo hover the mouse over start game option it doesn't change to red

    i did a test and commented out the code to make menu option2 work and the start game option worked

    do you know why the seconed one is overiding the first one?

    and could you possibly tell me any tutorial of how to make a large map where the user can move the screen through by putting the mouse at the edges of screen and have borders where it would eventualy stop scrolling?

Similar Threads

  1. Studying for a test, can't figure out my code won't work
    By coolidge in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 27th, 2011, 01:12 PM
  2. My code is not working
    By mike2452 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 9th, 2011, 06:17 AM
  3. Getting code working with gui
    By Nostromo in forum AWT / Java Swing
    Replies: 2
    Last Post: March 21st, 2011, 09:34 PM
  4. I need help with my ohm's law applet code. can't figure it out!
    By dracula2001 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 19th, 2010, 06:16 PM
  5. Generation of Palindrome number in Java
    By tina.goyal in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 26th, 2009, 08:49 AM