Results 1 to 5 of 5
  1. #1
    Fluxtime is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Java MouseMotionListener

    I am writing a program that displays a number of random rectangles on screen as a screensaver. When I open the program it runs fine but I have set the screen to not display the JFrame. This leaves the obvious problem that ther's no way to stop the program running. I want to implement a mouse listener to close the screen when i move the mouse. However I've tried a few times but can't figure out where to call the methods. I have two classes, my screensaver class and a screensaver test class which contains the main method to run my program.

    any help will be much appreciated as I am very new to java.

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.util.Random;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.Font;
    import java.text.DateFormat;
    import java.util.Date;
    import java.util.*;
    import java.text.*;
    
    
    public class Screensaver extends JPanel{
    
    private Random rand = new Random();
    
    public void paintComponent( Graphics g ){
    
    super.paintComponent( g );
    int x1, y1, x2, y2;
    
    Date now = new Date();
    DateFormat mediumDf = DateFormat.getDateInstance(DateFormat.MEDIUM);
    String s = mediumDf.format(now);
    
    
    for( int i = 0; i < 15; i++ )
    {
    x1 = rand.nextInt( 1366 );
    x2 = rand.nextInt( 1366 );
    y1 = rand.nextInt( 768 );
    y2 = rand.nextInt( 768 );
    
    g.setColor( new Color( rand.nextInt( 256 ),
    rand.nextInt( 256 ), rand.nextInt( 256 ) ) );
    g.drawRect( x1, y1, x2, y2 );
    
    
    
    Graphics2D g3 = (Graphics2D)g;
    Font font2 = new Font("Serif", Font.PLAIN, 64);
    g2.setFont(font2);
    g2.drawString("Date:" + s, 20, 700);
    
    }
    
    repaint(); // repaint component
    try { Thread.sleep(500); } catch( InterruptedException e) {}
    }
    
    }
    
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Toolkit;
    import javax.swing.JFrame;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.awt.event.MouseEvent;
    
    public class ScreensaverTest implements MouseMotionListener
    {
    // execute application
    public static void main( String[] args )
    {
    // create frame for ScreensaverJPanel
    MouseMotionListener e = new MouseMotionListener();
    JFrame frame =
    new JFrame();
    frame.setUndecorated( true );
    frame.setVisible( true ); // display frame
    frame.setExtendedState( JFrame.MAXIMIZED_BOTH );
    
    Screensaver screensaver =
    new Screensaver();
    screensaver.setBackground( Color.BLACK );
    frame.add( screensaver ); // add panel to frame
    // set frame size
    }
    
    } // end main
    // end class Screensaver
    Last edited by Norm; 03-30-2012 at 09:33 PM. Reason: added code tags

  2. #2
    Fluxtime is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Re: Java MouseMotionListener

    Sorry, the screensaver test class shouldn't implement mouselistener. i was uploading the working code.

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Toolkit;
    import javax.swing.JFrame;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.awt.event.MouseEvent;

    public class ScreensaverTest implements MouseMotionListener
    {
    // execute application
    public static void main( String[] args )
    {
    // create frame for ScreensaverJPanel
    MouseMotionListener e = new MouseMotionListener();
    JFrame frame =
    new JFrame();
    frame.setUndecorated( true );
    frame.setVisible( true ); // display frame
    frame.setExtendedState( JFrame.MAXIMIZED_BOTH );

    Screensaver screensaver =
    new Screensaver();
    screensaver.setBackground( Color.BLACK );
    frame.add( screensaver ); // add panel to frame
    // set frame size
    }

    } // end main
    // end class Screensaver

  3. #3
    Fluxtime is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Re: Java MouseMotionListener

    once again sorry, forgot to edit it.
    Java Code:
    import java.awt.Color;
     import java.awt.Dimension;
     import java.awt.Toolkit;
     import javax.swing.JFrame;
      
    public class ScreensaverTest 
     {
     // execute application
     public static void main( String[] args )
     {
     // create frame for ScreensaverJPanel
      JFrame frame =
     new JFrame();
     frame.setUndecorated( true );
     frame.setVisible( true ); // display frame
     frame.setExtendedState( JFrame.MAXIMIZED_BOTH );
     
    Screensaver screensaver =
     new Screensaver();
     screensaver.setBackground( Color.BLACK );
     frame.add( screensaver ); // add panel to frame
     // set frame size
     }
     
    } // end main
     // end class Screensaver
    Last edited by Norm; 03-30-2012 at 09:32 PM. Reason: added code tags

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Java MouseMotionListener

    I have set the screen to not display the JFrame
    If the JFrame is not displayed, what is shown?
    If there is nothing visible, can the program receive any GUI type events?

    What are the three versions of ScreensaverTest for? Which one is to be used for testing?

    How do you compile and execute the program?
    Last edited by Norm; 03-30-2012 at 09:35 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Java MouseMotionListener

    Please make a single program the compiles, executes and show the problem. Your posted code does not compile.

    Also be sure to use proper indentation. Do NOT have all the statements start in the first column. Indent the statements to reflect the logic/nesting levels.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. MouseMotionListener
    By ninjaturtlez in forum AWT / Swing
    Replies: 2
    Last Post: 12-14-2011, 11:19 PM
  2. MouseMotionListener
    By hubikmar in forum AWT / Swing
    Replies: 2
    Last Post: 04-15-2011, 11:35 AM
  3. using the mousemotionlistener and mouselistener
    By ravirajparab in forum New To Java
    Replies: 1
    Last Post: 12-21-2009, 07:11 PM
  4. MouseMotionListener 'scrolling'
    By Thez in forum Java 2D
    Replies: 3
    Last Post: 03-12-2009, 11:48 AM
  5. Replies: 0
    Last Post: 04-23-2008, 08:26 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •