I'm trying to make it so when I press ESC, it RestoreScreen() but it doesn't work. Now I don't know whether it's my method RestoreScreen() or my listeners.
This is my code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class main extends JFrame implements MouseListener,KeyListener{
	
		public void keyPressed(KeyEvent e) {
			int rsKey = e.getKeyCode();
			if(rsKey == KeyEvent.VK_ESCAPE || rsKey == 27){
				s.RestoreScreen();
			}
		}

		
		public void keyReleased(KeyEvent e) {
				
		}

		
		public void keyTyped(KeyEvent e) {
			
		}
	public static void main(String[] args) {
		DisplayMode dm = new DisplayMode(800,600,16,DisplayMode.REFRESH_RATE_UNKNOWN);
		main m = new main();
		m.run(dm);
		
	}
	
	private title s;
	private JTextField command;
	private Image bg;
	private Image ball;
	private static int x = 0;
	private static int y = 0;
	private static boolean right = false;
	private static boolean down = false;
	private boolean loaded = false;
	private boolean dojustonce = false;
	Timer moveball = new Timer(50,new ActionListener(){
		public void actionPerformed(ActionEvent e){
			if(right == false){x-=6;}if(right == true){x+=6;}
			if(down == false){y-=6;}if(down == true){y+=6;}
			if(x <= 0){right = true;}if(x >= 750){right = false;}
			if(y <= 0){down = true;}if(y >= 550){down = false;}
			repaint();
		}
	});
	public void DoJustOnce(){
		repaint();
		dojustonce = true;
	}
	public void paint(Graphics g){
		if(!dojustonce){
			g.clearRect(0,0,getWidth(),getHeight());
		}
		if(g instanceof Graphics2D){
			Graphics2D g2 = (Graphics2D)g;
			g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
			
		}
		if(loaded){
			g.drawImage(bg,0,0,null);
			g.drawImage(ball,x,y,null);
		}
		g.drawString("Click To Start or Press Esc To Exit",180,500);
		
	}
	public void run(DisplayMode dm){
		setBackground(Color.BLACK);
		setForeground(Color.YELLOW);
		setFont(new Font("Comic Sans MS",Font.PLAIN,24));
		loaded = false;
		loadpics();
		command = new JTextField("Enter your command here. Press ESC to exit or press Enter to start.");
		addMouseListener(this);
		addKeyListener(this);
		title s = new title();
		setFocusTraversalKeysEnabled(true);
		moveball.start();
		try{
			s.setFullScreen(dm,this);
		}
		catch(Exception ex){}
	}
	public void loadpics(){
		bg = new ImageIcon("C:\\Documents and Settings\\Lam\\My Documents\\My Pictures\\fun.png").getImage();
		ball = new ImageIcon("C:\\Documents and Settings\\Lam\\My Documents\\My Pictures\\ball_c.png").getImage();
		loaded = true;
		repaint();
	}
	@Override
	public void mouseClicked(MouseEvent arg0) {
		if(arg0.getClickCount() > 1){
			s.RestoreScreen();
		}
	}
	@Override
	public void mouseEntered(MouseEvent arg0) {
		
	}
	@Override
	public void mouseExited(MouseEvent arg0) {
		
	}
	@Override
	public void mousePressed(MouseEvent arg0) {
		
	}
	@Override
	public void mouseReleased(MouseEvent arg0) {
		
	}
	
}

My mouse listener won't work and nor will my KeyListener. Please help.

OR

This could be my RestoreScreen() method which is in my title.java.
Code:

import javax.swing.*;
import java.awt.*;
public class title extends JFrame{
	
		private GraphicsDevice vc;
		
		public title(){
			GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
			vc = env.getDefaultScreenDevice();
			
		}
		
		public void setFullScreen(DisplayMode dm,JFrame window){
			window.setUndecorated(true);
			window.setResizable(false);
			vc.setFullScreenWindow(window);
			
			if(dm != null && vc.isDisplayChangeSupported()){
				try{
					vc.setDisplayMode(dm);
				}catch(Exception ex){}
			}
		}
		
		public Window getFullScreenWindow(){
			return vc.getFullScreenWindow();
		}
		
		public void RestoreScreen(){
			Window w = vc.getFullScreenWindow();
			if(w != null){
				w.dispose();
			}
			vc.setFullScreenWindow(null);
		}
		
}

So put some System.out.println statements into the relevant parts of the code to see which code is being executed and whether the variables have the values you expect.
(It's called "debugging").

Hi, mangopearapples. I think you have a mistake on your code.

Here:

private title s;

is still null. And you make a new variable with the same name here:

public void run(DisplayMode dm){
    .
    .
    .
    title s = new title();

    setFocusTraversalKeysEnabled(true);
 
    moveball.start();

You should remove the variable type there, because you make a new local variable with that code instead instantiate the instance variable. So, your code should be like this:

public void run(DisplayMode dm){
    .
    .
    .
    s = new title();
 
    setFocusTraversalKeysEnabled(true);

    moveball.start();

I hope that is helping. Sorry for my bad english. :)

Edited 5 Years Ago by roxin_phoenix: n/a

This question has already been answered. Start a new discussion instead.