Why won't this work? I'm trying to make a ball appear and move along my screen but it's failing :'( . I'm :( and also trying to make it so when you press any key it restores the full screen but that's also failing. :'( Can anyone help?

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;
	
	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();
		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) {
		
	}
	@Override
	public void mouseEntered(MouseEvent arg0) {
		
	}
	@Override
	public void mouseExited(MouseEvent arg0) {
		
	}
	@Override
	public void mousePressed(MouseEvent arg0) {
		
	}
	@Override
	public void mouseReleased(MouseEvent arg0) {
		
	}
	
}

It's not easy.
To move the ball you just lock the main method into a one-line while loop. All that will do is burn CPU cycles. You need to start a javax.swing.Timer to trigger regularly (a few times a second)to update the position and re-draw the screen.
s.RestoreScreen() s is declared as an instance of "title" - for which we have source code. So I have no idea why it doesn't work.
ps: Java classes should have names that begin with a capital letter, methods names should not start with a capital letter. Ignoring this just makes your code that bit harder to understand. Also, correct indentation is a must, not a burden.

Right, will making a different method instead of using the main method with the movement if the balls positions in a loop still burn CPU cycles as much or I should just use javax.swing.Timer?
So s.RestoreScreen() should work?
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);
		}
		
}

OK.
I have no idea why you have classes main and title, why not just one? Sorry, but I don't have time now to follow right thru them both to see whether RestoreScreen is OK, but having it in a separate JFrame class seems unnecessarily hard.

The loops in main are wrong, no matter where you put them - they just loop flat-out until the CPU melts. You have to increment x and y and then repaint in a Timer..

Edited 5 Years Ago by JamesCherrill: n/a

OK, I used a timer and.... Nothing.
I don't know whether this is correct. I haven't changed the title class. Maybe if you can find the time?

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(500,new ActionListener(){
		public void actionPerformed(ActionEvent e){
			while(right == false){x--;}while(right == true){x++;}
			while(down == false){y--;}while(down == true){y++;}
			if(x <= 0){right = true;}if(x >= 800){right = false;}
			if(y <= 0){down = true;}if(y >= 600);
			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) {
		
	}
	
}

Like I said "The loops in main are wrong, no matter where you put them".
You just put the same loops in a Timer. They are still wrong.

Oh, well do you know why they're wrong?

Well. yes. obviously. But if I just tell you you won't learn anything. When you work out for yourself why they're wrong you will have improved your understanding of Java and of debugging. If that sounds unhelpful then I'm sorry, but I'm here to help more than just fix homework.
So:

while(right == false){x--;}

How many times will that loop execute? If the answer is zero, how about

while(right == true){x++;}

You just need to think about it

Hmm, I have no idea. I understand that 'true' returns 1 and 'false' returns 0...
Maybe if you tell me why then I'll be able to do the rest :D .

Oh dear. Your understanding of true and false is incorrect for Java. You also won't get any loops right until you learn how a loop works. You're going to have to do some serious revision here.
Check out boolean here
http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Read about while loops here
http://download.oracle.com/javase/tutorial/java/nutsandbolts/while.html
Follow those Java official tutorials through anything else that isn't clear to you. They really are excellent.
Finally, have another think about your program. Your Timer "moveball" will fire every 1/2 second. Write down in plain English but complete detail what you want to happen each time it fires. You can convert that to Java once the thinking and the logic is right.
This is going to need quite a bit of your time I'm afraid. It's no good just trying different stuff and copying code from the web - there's no alternative to getting a good understanding of the fundamentals. You'll find help here - we'll put as much into this as you do. Good luck.

Okay, so I read through everything on the links. Thanks, I learned some more about numerical data but I think I already new what I read about while loops.
Anyway, this is what I think I want in just plain English.
While right is equal to true; increment variable x.
while right is equal to false; decrement variable x.
While down is equal to true; increment variable y.
While down is equal to false; decrement variable y.

Huh? That code is a loop isn't it?

while(right){x++;}
while(!right){x--;}
while(down){y++;}
while(!down){y--;}

Edited 5 Years Ago by mangopearapples: n/a

Huh? That code is a loop isn't it?

while(right){x++;}
while(!right){x--;}
while(down){y++;}
while(!down){y--;}

or use if statements.
if(right)
x++;
//etc...

Edited 5 Years Ago by Akill10: n/a

Oh yeah! I know why now! See James, telling me made me understand :D
Though I might still be wrong.
Using If statements will work because it will execute the code every 1/2 a second because it's in a timer right?
I still don't understand why while loops work though.
Thanks James and Akill10!!

Edited 5 Years Ago by mangopearapples: n/a

Oh yeah! I know why now! See James, telling me made me understand :D
Though I might still be wrong.
Using If statements will work because it will execute the code every 1/2 a second because it's in a timer right?
I still don't understand why while loops work though.
Thanks James and Akill10!!

Yes, The actionPerformed() method is called every 500ms and repaints() each time. Make sure you remember to start the timer.
Also, I didn't tell you anything, when you wrote that out in english, I assumed you meant 'if' when you wrote 'while' ha!

Edited 5 Years Ago by Akill10: n/a

I still don't understand why while loops work though.

They do, but not in this case.
Normally there has to be something inside the loop that affects the while condition so that the loop eventually stops, eg
while (x<10) x++;
or
while (not end of file) read from file;
etc

I see, so they cannot never end.
The while loop link you gave me says you can though, I think...
Anyway, problem solved. Thanks again!

Ok, for you and anybody else who stumbles upon this:

while(right == false){
x--;
}

This loop will not end.
While loops work by evaluating the boolean expression and executing the statements inside the while block.

Therefore, if the expression is true and you can enter the block, it runs through all statements then evaluates the boolean expression again. Since 'right' was not altered, it is still equal to false, Therefore the expression to run the loop will always be true, meaning the loop will continue.

Edited 5 Years Ago by Akill10: n/a

Hmm, it's still not moving the ball image :S , the only code that I changed from before was changing the while to an if.

Have you started the timer?

Also, I don't see why you create an ActionListener for the Timer object instead of implementing it with the class. It just makes the code look cluttered.

Edited 5 Years Ago by Akill10: n/a

Oh yes, how silly of me :D . Sorry for that. Thanks again! It moves now but I think I should make the amount of milliseconds before the timer executes the 'actionPerformed' method a bit lower because it moves too slow!
Anyway, thanks again! I want to hug you guys!

Yes well, 500ms is alot of time when moving sprites, you would normally set this to 5ms or 10ms to do what you are looking for.

I just realized that my KeyListener isn't working. Sorry for troubling you again but do you know why? I'm pretty sure RestoreScreen() works because I had already tested it with thread.sleep().

This article has been dead for over six months. Start a new discussion instead.