Results 1 to 2 of 2
  1. #1
    adam405 is offline Member
    Join Date
    Mar 2008
    Posts
    7
    Rep Power
    0

    Default moving slider with key event

    I have a rectangle drawn on the screen. I can move it one instance to the right and one left. I want to be able to move it right across the applet screen. Where am i going wrong

    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.Graphics;
    
    public class slider extends Applet implements Runnable
    {
        
    	
    	int x_pos = 30;			
    	int y_pos = 100;		
    	int x_speed = 1;		
    	int radius = 20;		
    	int appletsize_x = 300; 
    	int appletsize_y = 300;	
    
    	// Variablen für die Doppelpufferung
    	private Image dbImage;
    	private Graphics dbg;
            
             Thread thread;
        boolean run = false;
        
    
    	public void start ()
    	{
    	      // define a new thread 
            run = true;
            thread = new Thread (this);
            // start this thread
            thread.start ();
            System.out.println("start");
    	}
    
    	public void stop()
    	{
           run = false;
            if(thread != null)
                thread.interrupt();
            thread = null;
            System.out.println("stop");
    
    	}
    
    	public boolean keyDown (Event e, int key)
    	{
    		// linke Cursortaste
    		if (key == Event.LEFT)
    		{
    			// Ball bewegt sich dann nach links
    			x_speed = -1;
                            x_pos += x_speed;
    		}
    		// rechte Cursortaste
    		else if (key == Event.RIGHT)
    		{
    			// Ball bewegt sich dann nach rechts
    			x_speed = +1;
                            x_pos += x_speed;
    		}
    		// SpaceBar hat Wert 32
    		
    		else
    		{
    			// Ausgabe von gedrüktem Key und Zahlenwert an die Standardausgabe
    			System.out.println ("Charakter: " + (char)key + " Integer Value: " + key);
    		}
    
    		return true;
    	}
    
    	public void run ()
    	{
    		// Erniedrigen der ThreadPriority um zeichnen zu erleichtern
    		Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    
    		// Solange true ist läuft der Thread weiter
    		while (run)
    		{
                        x_pos = x_speed;
                        
    			repaint();
    
    			try
    			{
    				// Stoppen des Threads für in Klammern angegebene Millisekunden
    				Thread.sleep (5);
    			}
    			catch (InterruptedException ex)
    			{
    				// do nothing
    			}
    
    			// Zurücksetzen der ThreadPriority auf Maximalwert
    			Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    		}
    	}
    
    	/** Update - Methode, Realisierung der Doppelpufferung zur Reduzierung des Bildschirmflackerns */
    	public void update (Graphics g)
    	{
    		// Initialisierung des DoubleBuffers
    		if (dbImage == null)
    		{
    			dbImage = createImage (this.getSize().width, this.getSize().height);
    			dbg = dbImage.getGraphics ();
    		}
    
    		// Bildschirm im Hintergrund löschen
    		dbg.setColor (getBackground ());
    		dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
    
    		// Auf gelöschten Hintergrund Vordergrund zeichnen
    		dbg.setColor (getForeground());
    		paint (dbg);
    
    		// Nun fertig gezeichnetes Bild Offscreen auf dem richtigen Bildschirm anzeigen
    		g.drawImage (dbImage, 0, 0, this);
    	}
    
    	public void paint (Graphics g)
    	{
    	
                    
                Graphics2D g2 = (Graphics2D) g;
                Rectangle box = new Rectangle(x_pos,y_pos,60,25);
                g2.draw(box);
    	}
           
    	
    
    }

  2. #2
    adam405 is offline Member
    Join Date
    Mar 2008
    Posts
    7
    Rep Power
    0

Similar Threads

  1. Event
    By nt5515 in forum New To Java
    Replies: 0
    Last Post: 02-15-2008, 05:44 PM
  2. moving image - PROBLEM
    By Triss in forum New To Java
    Replies: 3
    Last Post: 01-17-2008, 07:52 PM
  3. Moving icons on your desktop
    By Leprechaun in forum New To Java
    Replies: 3
    Last Post: 12-14-2007, 11:07 AM
  4. moving a file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-10-2007, 08:52 PM
  5. examples of moving objects
    By fred in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:06 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
  •