0

The MouseListener is there for later.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class main extends JFrame implements MouseListener{
	class keyl implements KeyListener{

		@Override
		public void keyPressed(KeyEvent e) {
				s.RestoreScreen();
		}

		@Override
		public void keyReleased(KeyEvent e) {
				s.RestoreScreen();
		}

		@Override
		public void keyTyped(KeyEvent e) {
				s.RestoreScreen();
		}
		
	}
	public static void main(String[] args) {
		DisplayMode dm = new DisplayMode(800,600,16,DisplayMode.REFRESH_RATE_UNKNOWN);
		main m = new main();
		m.run(dm);
		
	}/*
	while(right == false){x--;}while(right == true){x++;}
		while(down == false){y--;}while(down == true){y++;}
		if(x <= 0){right = true;}if(x >= m.getWidth()){right = false;}
		if(y <= 0){down = true;}if(y >= m.getWidth());
	*/
	
	private title s;
	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;
	Timer moveball = new Timer(10,new ActionListener(){
		public void actionPerformed(ActionEvent e){
			if(right == false){x--;}if(right == true){x++;}
			if(down == false){y--;}if(down == true){y++;}
			if(x <= 0){right = true;}if(x >= 800){right = false;}
			if(y <= 0){down = true;}if(y >= 600){down = false;}
			repaint();
		}
	});
	public void paint(Graphics g){
		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 Any Key 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();
		addMouseListener(this);
		addKeyListener(new keyl());
		title s = new title();
		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:\\Program Files\\Ds Game Maker\\Resources\\Sprites\\Ball.png").getImage();
		loaded = true;
		repaint();
	}
	@Override
	public void mouseClicked(MouseEvent arg0) {
		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) {
		
	}
	
}
0

Ok, so what KeyEvent should it restoreScreen() on? You haven't put anything.

Also, again, why do you have a separate class for key listener when you could implement it into the main class?

Edited by Akill10: n/a

0

Hmm? Looks like i'll have to read a KeyListener tutorial...
I had already implemented the MouseListener so I don't think I can implement another thing.

0

Hmm? Looks like i'll have to read a KeyListener tutorial...
I had already implemented the MouseListener so I don't think I can implement another thing.

To set a Key to do something, you need to 'listen' to what key is being pressed/released, so:

public void keyPressed(KeyEvent e) {
		int key = e.getKeyCode();//method that will return which key is active
         if(key==KeyEvent.VK_ESCAPE){ // if the getKeyCode() returns the key event ESCAPE
//do something
                  }
		}

Yep, you can implement multiple interfaces! Just separate with a ,

e.g. implements ActionListener,KeyListener

Also, your ActionPerformed method is making me cringe...sorry ha!
To make it look less confusing, I am going t give you a simpler way.

//First, declare 2 more variables with your x and y, int velX =2,velY=2, these will control the "speed";
//then in your actionperformed method
 public void actionPerformed(ActionEvent e) {
if(x<0 || x>800){
velX = -velX;
}
else if(y<0 || y>600){
velY = -velY;
}
x+=velX;
y+=velY;

repaint();
}

This should bounce your ball about your screen, I assume that was what you were doing before.


EDIT: I dont know if this is my bad eyesight, but have you set the class focusable? You need to do this in order to use listeners
setFocusable(true);

Edited by Akill10: n/a

0

@Akill10 - using a separate inner class for event handlers is a very common practice. Some IDE's generate code that way by default.
Just implementing the event interface in the main class is OK if there is just one event listener of any given kind, but if you have different event handlers for different components within the class then the inner class is the scalable way to go.

I haven't time to check out the key listener problem right now, but provided all the code is there, the most common reason for it not working is that the component with the key listener is not the component with keyboard focus.

0

Wow, I hadn't realized there was that much things about listeners...
Do you have to make class focusable? I don't think I've ever made a class focusable, not on purpose anyway and my ActionListener works always.
Anyway, thanks for everything again guys.

0

Pressing Esc doesn't do anything even though I have:

public void keyPressed(KeyEvent e) {
				int rsKey = e.getKeyCode();
				if(rsKey == KeyEvent.VK_ESCAPE){
					s.RestoreScreen();
				}
		}

I've added the KeyListener.
Also I get lot's of errors or whatnot in the console box:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at main.keyPressed(main.java:9)
	at java.awt.Component.processKeyEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Window.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
	at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
	at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
	at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
	at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)

I have no idea what's wrong with my KeyPressed method...

I cut and pasted the KeyEvent code (int rsKey = e.getKeyCode();if(rskey ==) ect. to the KeyTyped method and this time, no errors in the console box yet it still doesn't 'RestoreScreen'.

0

You have a null pointer exception on line 9 of main (see - I can read too!).
This means either an uninitialised variable, or a method call that has returned null.
You can look at your source code for that line and it will probably be obvious what can or can't be null.

0

@Akill10 - using a separate inner class for event handlers is a very common practice. Some IDE's generate code that way by default.
Just implementing the event interface in the main class is OK if there is just one event listener of any given kind, but if you have different event handlers for different components within the class then the inner class is the scalable way to go.

Good to know, thanks.

0

Right; so if I'm right, s.RestoreScreen() returns a null?
And that means there's something wrong in my title class... But I swear it was working when I used thread.sleep(milliseconds). Do you guys want the code from title.java?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.