Jump to content


Simple Java Screensaver


  • Please log in to reply
9 replies to this topic

#1 riley1275

riley1275

    Newbie

  • Members
  • Pip
  • 7 posts
  • Gender:Male
  • Interests:Computing

Posted 30 October 2011 - 08:56 PM

Hi,

For a college assignment I have to create a simple screensaver.
I have created my screensaver, but don't know how to stop it screensaver. I think i may be able to use an ActionListener but have only used them with buttons. Can i end my screensaver by moving my mouse?
Any help would be greatly appreciated.
Here is my code:

import java.awt.*; 
import javax.swing.*;
import java.awt.event.*; 
import java.util.Random;
import javax.swing.Timer;
public class Screensaver
{
	JFrame frame = new JFrame(); // Create new JFrame (frame)
	JPanel panel = new JPanel(); // Create new JPanel (panel)
	Color c = Color.black;
	
	public static void main(String[]args)
	{
		Screensaver RCA = new Screensaver();
		RCA.go();
	}
	
	
	public void go()
	{
		RandomColor drawPanel = new RandomColor();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().add(BorderLayout.CENTER,drawPanel);
		frame.setSize(10000,10000);
		frame.setVisible(true);
	}
class RandomColor extends JPanel
{
	private Random generator = new Random();
	
	// Draw lines
	
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		int a1 = 0;
		int b1 = 0;
		int a2 = 0;
		int b2 = 0;
		
		// Draw 200 Random Lines
		for (int i = 0; i < 200; i++ )
		{
			a1 = generator.nextInt( 900 );
			b1 = generator.nextInt( 900 );
			a2 = generator.nextInt( 900 );
			b2 = generator.nextInt( 900 );
				
		
		g.setColor( new Color( generator.nextInt( 256 ),generator.nextInt( 256 ), generator.nextInt( 256 ) ) );
		 g.drawLine(a1,b1,a2,b2 );
		 
	
	  }// End Outer for
		 
	  repaint();
	  try { Thread.sleep(100); } catch( InterruptedException e) {}
	}// End Method PaintComponent
}
}


#2 Scott

Scott

    Advanced Member

  • Members
  • PipPipPip
  • 40 posts
  • Gender:Male
  • Location:England

Posted 30 October 2011 - 09:59 PM

I think you just need to add a MouseMotionListener to call frame.dispose()

Right now the tutorial seems to have gone missing so I'll write what I remember in case you need it

Add extends MouseMotionListener to your class to read
public class Screensaver extends MouseMotionListener
Add the listener to the frame
frame.addMouseMotionListener(this)
And create the methods it will call
public void mouseMoved(MouseEvent e){frame.dispose();}
public void mouseDragged(MouseEvent e){}

I hope this helps  :happy:

#3 Scott

Scott

    Advanced Member

  • Members
  • PipPipPip
  • 40 posts
  • Gender:Male
  • Location:England

Posted 31 October 2011 - 08:43 AM

Oops, It should be implements MouseMotionListener. Not extends :blush:

#4 riley1275

riley1275

    Newbie

  • Members
  • Pip
  • 7 posts
  • Gender:Male
  • Interests:Computing

Posted 31 October 2011 - 04:31 PM

View PostScott, on 31 October 2011 - 08:43 AM, said:

Oops, It should be implements MouseMotionListener. Not extends :blush:

Here is my code now:
import java.awt.*; 
import javax.swing.*;
import java.awt.event.*; 
import java.util.Random;
import javax.swing.Timer;

public class Screensaver implements MouseMotionListener
{
	JFrame frame = new JFrame(); // Create new JFrame (frame)
	JPanel panel = new JPanel(); // Create new JPanel (panel)
	Color c = Color.black;
	
	public static void main(String[]args)
	{
		Screensaver RCA = new Screensaver();
		RCA.go();
	}
	
	
	public void go()
	{
		RandomColor drawPanel = new RandomColor();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().add(BorderLayout.CENTER,drawPanel);
		
		frame.setSize(800,800);
		frame.setVisible(true);
		frame.addMouseMotionListener(this);
		
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
class RandomColor extends JPanel
{
	private Random generator = new Random();
	
	// Draw lines
	
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		int a1 = 0;
		int b1 = 0;
		int a2 = 0;
		int b2 = 0;
		
		// Draw 150 Random Coloured Circles
		for (int i = 0; i < 15; i++ )
		{
			a1 = generator.nextInt( 100 );
			b1 = generator.nextInt( 100 );
			a2 = generator.nextInt( 100 );
			b2 = generator.nextInt( 100 );
				
		
		g.setColor( new Color( generator.nextInt( 256 ),generator.nextInt( 256 ), generator.nextInt( 256 ) ) );
		g.fillOval(a1,b1,a2,b2);
		
		
	  }// End Outer for
		 
	  repaint();
	  try { Thread.sleep(400); } catch( InterruptedException e) {}
	}// End Method PaintComponent
	
	public void mouseMoved(MouseEvent event){frame.dispose();}
 	public void mouseDragged(MouseEvent event){}

When i compile this, I get the error message: "Screensaver.java:14: Screensaver is not abstract and does not override abstract method mouseMoved(java.awt.event.MouseEvent) in java.awt.event.MouseMotionListener"
it points to the line:
 public class Screensaver implements MouseMotionListener 
Any Ideas as to what this means?

Thanks

#5 Scott

Scott

    Advanced Member

  • Members
  • PipPipPip
  • 40 posts
  • Gender:Male
  • Location:England

Posted 31 October 2011 - 06:37 PM

It means you need a mouseMoved(java.awt.event.MouseEvent) method. You have this but you called your MouseEvent event which is a reserved name so it does not recognise it. Change it to almost anything else (such as e) and it should work

#6 riley1275

riley1275

    Newbie

  • Members
  • Pip
  • 7 posts
  • Gender:Male
  • Interests:Computing

Posted 01 November 2011 - 01:52 PM

Hi Again,

I've tried what you said earlier and changed it to e. but i'm getting 2 identifier expected errors.
Here is my code:
import java.awt.*; 
import javax.swing.*;
import java.awt.event.*; 
import java.util.Random;
import javax.swing.Timer;
import java.awt.event.MouseMotionListener;


public class Screensaver implements MouseMotionListener
{
	MouseMotionListener e = new MouseMotionListener();
	JFrame frame = new JFrame(); // Create new JFrame (frame)
	JPanel panel = new JPanel(); // Create new JPanel (panel)
	Color c = Color.black;
	
	public static void main(String[]args)
	{
		Screensaver RCA = new Screensaver();
		RCA.go();
	}
	
	
	public void go()
	{
		RandomColor drawPanel = new RandomColor();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().add(BorderLayout.CENTER,drawPanel);
		
		frame.setSize(300,300);
		frame.setVisible(true);
		frame.add(MouseMotionListener(this));
		
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
class RandomColor extends JPanel
{
	private Random generator = new Random();
	
	// Draw Circles
	
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		int a1 = 0;
		int b1 = 0;
		int a2 = 0;
		int b2 = 0;
		
		// Draw 150 Random Coloured Circles
		for (int i = 0; i < 25; i++ )
		{
			a1 = generator.nextInt( 100 );
			b1 = generator.nextInt( 100 );
			a2 = generator.nextInt( 100 );
			b2 = generator.nextInt( 100 );
				
		
		g.setColor( new Color( generator.nextInt( 256 ),generator.nextInt( 256 ), generator.nextInt( 256 ) ) );
		g.fillOval(a1,b1,a2,b2);
		
		
	  }// End Outer for
		 
	  repaint();
	  try { Thread.sleep(400); } catch( InterruptedException e) {}
	}// End Method PaintComponent
	
	public void mouseMoved(e)
	{
		frame.dispose();
		}
	public void mouseDragged(e){}	
}
}
 
Any ideas why its not working?
Thanks for all your help.

#7 Scott

Scott

    Advanced Member

  • Members
  • PipPipPip
  • 40 posts
  • Gender:Male
  • Location:England

Posted 01 November 2011 - 10:02 PM

The problem this time is that it doesn't know what e is. You must tell it that e is a MouseEvent so it can find the right method.
public void mouseMoved(MouseEvent e){}
This is because sometimes methods have the same name but different data passed in.

Also you'll need to put your mouse motion listener methods in ScreenSaver rather than the RandomColor class. ScreenSaver is the mouse motion listener not RandomColor.
And frame is declared in ScreenSaver so it cannot be accessed from RandomColor.

I see you have tried to create a MouseMotionListener object. This is impossible because it is abstract. Even if you could create it it wouldn't do anything. By having ScreenSaver implement MouseMotionListener it becomes one.
The call to frame.addMouseMotionListener(this) tells the JFrame to listen to this class (ScreenSaver) for mouse motion events.
You have got your add call wrong. The add method tries to add a graphical component to the frame. The correct method is
frame.addMouseMotionListener(this)

Fix all that and your code will run :biggrin:
One thing that's become abundantly clear to me is seemingly little tasks can take a long time to figure out

And your welcome. I'm happy to share what little I know if people ask rather than say "do my work for me"  :happy:

#8 riley1275

riley1275

    Newbie

  • Members
  • Pip
  • 7 posts
  • Gender:Male
  • Interests:Computing

Posted 03 November 2011 - 11:59 AM

View PostScott, on 01 November 2011 - 10:02 PM, said:

The problem this time is that it doesn't know what e is. You must tell it that e is a MouseEvent so it can find the right method.
public void mouseMoved(MouseEvent e){}
This is because sometimes methods have the same name but different data passed in.

Also you'll need to put your mouse motion listener methods in ScreenSaver rather than the RandomColor class. ScreenSaver is the mouse motion listener not RandomColor.
And frame is declared in ScreenSaver so it cannot be accessed from RandomColor.

I see you have tried to create a MouseMotionListener object. This is impossible because it is abstract. Even if you could create it it wouldn't do anything. By having ScreenSaver implement MouseMotionListener it becomes one.
The call to frame.addMouseMotionListener(this) tells the JFrame to listen to this class (ScreenSaver) for mouse motion events.
You have got your add call wrong. The add method tries to add a graphical component to the frame. The correct method is
frame.addMouseMotionListener(this)

Fix all that and your code will run :biggrin:
One thing that's become abundantly clear to me is seemingly little tasks can take a long time to figure out

And your welcome. I'm happy to share what little I know if people ask rather than say "do my work for me"  :happy:


Thanks, Got it working now.
I have to add code to clear the program and start it again after drawing the circles, how would i do this?
Would a break in the for loop work?

#9 Scott

Scott

    Advanced Member

  • Members
  • PipPipPip
  • 40 posts
  • Gender:Male
  • Location:England

Posted 03 November 2011 - 08:30 PM

I'm not entirely sure I know what you mean so this may be totally wrong.

I think you want to clear the screen and start again?

To make it loop forever use a while(true) loop. And you can clear the screen by painting over it with white or whatever colour you'd prefer

while(true){//loop forever
g.setColor(new Color(255,255,255));//set the colour to white
g.fillRect(0,0,this.getWidth(),this.getHeight());//paint over the whole JPanel
for (int i = 0; i < 25; i++ ){}//for loop
}


#10 riley1275

riley1275

    Newbie

  • Members
  • Pip
  • 7 posts
  • Gender:Male
  • Interests:Computing

Posted 03 November 2011 - 08:50 PM

View PostScott, on 03 November 2011 - 08:30 PM, said:

I'm not entirely sure I know what you mean so this may be totally wrong.

I think you want to clear the screen and start again?

To make it loop forever use a while(true) loop. And you can clear the screen by painting over it with white or whatever colour you'd prefer

while(true){//loop forever
g.setColor(new Color(255,255,255));//set the colour to white
g.fillRect(0,0,this.getWidth(),this.getHeight());//paint over the whole JPanel
for (int i = 0; i < 25; i++ ){}//for loop
}

The assignment says: After drawing a number of lines (or other shapes), the program should clear itself and start again.