Docstoc

import java(10)

Document Sample
import java(10) Powered By Docstoc
					import java.applet.Applet; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.Image; import java.awt.Label; import java.awt.Panel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Bounce extends Applet implements ActionListener { Drawarea drawing = new Drawarea ( ); Panel controls = new Panel ( ); Panel first = new Panel ( ); Panel second = new Panel ( ); Panel third = new Panel ( ); Button redbutton, orangebutton, pinkbutton, whitebutton ; Label label3; Button stop, resume; Color purple = new Color ( 127, 0, 255 ); Color ltblue = new Color ( 153, 204, 255 ); public void init ( ) { label3 = new Label ( "Select a color: " ); second.add ( label3 ); redbutton = new Button ( "red" ) ; redbutton.addActionListener ( this ); second.add ( redbutton ); orangebutton = new Button ( "orange" ) ; orangebutton.addActionListener ( this ); second.add ( orangebutton ); whitebutton = new Button ( "white" ) ; whitebutton.addActionListener ( this ); second.add ( whitebutton ); pinkbutton = new Button ( "pink" ) ; pinkbutton.addActionListener ( this ); second.add ( pinkbutton );

// create third panel with pause and resume buttons stop = new Button ( "stop" ); stop.addActionListener ( this ); third.add ( stop ); resume = new Button ( "Resume" ); resume.addActionListener ( this ); third.add ( resume ); // create the control panel controls.setLayout ( new GridLayout ( 3, 1 )); controls.add ( first ); controls.add ( second ); controls.add ( third ); controls.setBackground ( ltblue ); // set the layout and contents of the whole applet setLayout ( new BorderLayout ( ) ); add ( drawing, BorderLayout.CENTER ); add ( controls, BorderLayout.SOUTH ); } // the applet start method public void start() { drawing.start ( ); } // the applet stop method public void stop( ) { drawing.stop ( ); } public void paint ( Graphics g ) { drawing.paint ( g ); } public void actionPerformed ( ActionEvent event ) { // respond to color buttons by setting the mycolor variable if ( event.getSource ( ) == redbutton ) { drawing.mycolor = Color.red; } if ( event.getSource ( ) == orangebutton ) { drawing.mycolor = Color.orange; } if ( event.getSource ( ) == whitebutton ) { drawing.mycolor = Color.white; } if ( event.getSource ( ) == pinkbutton ) { drawing.mycolor = Color.pink; }

// respond to pause button by calling the thread suspend method if ( event.getSource ( ) == stop ) { drawing.thread.suspend ( ) ; } // respond to resume button by calling the thread resume method if ( event.getSource ( ) == resume ) { drawing.thread.resume ( ) ; } } } class Drawarea extends Canvas implements Runnable { Color mycolor; int x, y, w, h; int dx; int dy; Thread thread; Image buffer; Graphics gbuffer; public Drawarea ( ) { setBackground ( Color.yellow ); mycolor = Color.black; x = 50; y = 50; w = 10; h = 10; dx = 2; dy = 3; }

public void start() { // create a thread and start it running if ( thread == null ) { thread = new Thread( this ); thread.start(); } } public void stop() { // stop the thread from running and destroy it if ( thread != null ) { thread.stop(); thread = null;

} } // this run method is what the thread executes public void run() { // continuous loop while ( thread != null ) { repaint(); try { Thread.sleep(20); } catch ( InterruptedException e ) { }; } } public void update (Graphics g) { paint (g); } public void paint ( Graphics g ) { if ( buffer == null ) { // Create an off-screen image for double buffering: buffer = createImage( getSize( ).width, getSize( ).height ); // Get off-screen graphics context: gbuffer = buffer.getGraphics(); } gbuffer.setColor ( getBackground ( ) ); gbuffer.fillRect ( 0, 0, getSize( ).width , getSize( ).height ); // Calculate where new location will be without bouncing: int nx = x + dx, ny = y + dy; // Check if new location is out of bounds and change direction: if ( (nx < 0) || (nx + w > getSize( ).width) ) dx = -dx; if ( (ny < 0) || (ny + h > getSize( ).height) ) dy = -dy; // move to new location and draw the ball x = x + dx; y = y + dy; gbuffer.setColor ( mycolor ); gbuffer.fillOval ( x, y, w, h ); // Draw the buffer in the canvas drawarea by using graphics g g.drawImage( buffer, 0, 0, this ); } }


				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:56
posted:11/29/2009
language:English
pages:4
Jun Wang Jun Wang Dr
About Some of Those documents come from internet for research purpose,if you have the copyrights of one of them,tell me by mail vixychina@gmail.com.Thank you!