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