Results 1 to 3 of 3
  1. #1
    anotsu is offline Member
    Join Date
    Feb 2008
    Posts
    10
    Rep Power
    0

    Default assignement change the java screensaver

    I would like to change this screensaver that draw multiple lines in different and color in different positions, to a screensaver that put a picture (let's call it pict01) in the screensaver and change its position.
    but i am new in java, can somebody help me

    file number 1----->saver1.java

    import javax.swing.JFrame;

    public class Saver1
    {
    public static void main( String args[] )
    {
    // create frame for Saver1JPanel
    JFrame frame = new JFrame( "Saver1" );
    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

    Saver1JPanel saver1JPanel = new Saver1JPanel();
    frame.add( saver1JPanel ); // add saver1JPanel to frame
    frame.setSize( 300, 300 ); // set frame size
    frame.setVisible( true ); // display frame
    } // end main
    }



    file number 2-------->Saver1JPanel

    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JPanel;

    public class Saver1JPanel extends JPanel
    {
    private final int DELAY = 9999999;

    // draw lines
    public void paintComponent( Graphics g )
    {
    super.paintComponent( g ); // call superclass's paintComponent

    int x, y, x1, y1;

    // draw 100 random lines
    for ( int i = 0; i < 100; i++ )
    {
    x = ( int ) ( Math.random() * 300 );
    y = ( int ) ( Math.random() * 300 );
    x1 = ( int ) ( Math.random() * 300 );
    y1 = ( int ) ( Math.random() * 300 );

    g.setColor( new Color( ( float ) Math.random(),
    ( float ) Math.random(), ( float ) Math.random() ) );
    g.drawLine( x, y, x1, y1 );

    // slow the drawing down. the body of the for loop is empty
    for ( int q = 1; q < DELAY; q++ ) ;
    } // end outer for

    repaint(); // repaint component
    } // end method paintComponent
    }


    thank you

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import java.util.Random;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class SaverRx extends JPanel implements Runnable
    {
        BufferedImage image;
        Random seed = new Random();
        Point loc = new Point();
        Thread thread;
        boolean running = false;
        private final int DELAY = 1000;
    
        public SaverRx(BufferedImage image)
        {
            this.image = image;
            start();
        }
    
        protected void paintComponent( Graphics g )
        {
            super.paintComponent( g );
            g.drawImage(image, loc.x, loc.y, this);
        }
    
        private void start()
        {
            while(!isVisible())
            {
                try
                {
                    Thread.sleep(25);
                }
                catch(InterruptedException e)
                {
                    System.exit(1);
                }
            }
            running = true;
            thread = new Thread(this);
            thread.setPriority(Thread.NORM_PRIORITY);
            thread.start();
        }
    
        public void run()
        {
            while(running)
            {
                try
                {
                    Thread.sleep(DELAY);
                }
                catch(InterruptedException e)
                {
                    System.out.println("interrupted");
                    running = false;
                }
                int w = getWidth() - image.getWidth();
                int h = getHeight() - image.getHeight();
                loc.x = seed.nextInt(w);
                loc.y = seed.nextInt(h);
                repaint();
            }
        }
    
        public static void main( String[] args ) throws IOException
        {
            String path = "images/hawk.jpg";
            BufferedImage image = ImageIO.read(new File(path));
            SaverRx test = new SaverRx(image);
            JFrame frame = new JFrame( "Saver1" );
            frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            frame.add( test );
            frame.setSize( 500, 500 );
            frame.setVisible( true );
        }
    }

  3. #3
    anotsu is offline Member
    Join Date
    Feb 2008
    Posts
    10
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 02-19-2014, 06:36 PM
  2. How to change JDK?
    By mew in forum Eclipse
    Replies: 5
    Last Post: 05-21-2010, 06:21 AM
  3. change the square to triangle java
    By anotsu in forum New To Java
    Replies: 3
    Last Post: 07-09-2009, 12:17 PM
  4. How to change the default Java
    By integrator in forum New To Java
    Replies: 4
    Last Post: 02-01-2008, 04:19 PM
  5. Replies: 1
    Last Post: 08-02-2007, 05:07 PM

Posting Permissions

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