Sie sind auf Seite 1von 6

Exercise 1: Drawing Lines Here's the source code for a first applet: import java.applet.*; import java.awt.

*; public class DrawingLines extends Applet { int width, height; public void init() { width = getSize().width; height = getSize().height; setBackground( Color.black ); } public void paint( Graphics g ) { g.setColor( Color.green ); for ( int i = 0; i < 10; ++i ) { g.drawLine( width, height, i * width / 10, 0 ); } } } ------------------Exercise 2: Drawing Other Stuff The source code: import java.applet.*; import java.awt.*; public class DrawingStuff extends Applet { int width, height; public void init() { width = getSize().width; height = getSize().height; setBackground( Color.black ); } public void paint( Graphics g ) { // As we learned in the last lesson, // the origin (0,0) is at the upper left corner. // x increases to the right, and y increases downward. g.setColor( g.drawRect( g.setColor( g.fillRect( g.setColor( g.drawOval( g.setColor( g.fillOval( Color.red ); 10, 20, 100, 15 ); Color.pink ); 240, 160, 40, 110 ); Color.blue ); 50, 225, 100, 50 ); Color.orange ); 225, 37, 50, 25 );

g.setColor( Color.yellow ); g.drawArc( 10, 110, 80, 80, 90, 180 );

g.setColor( Color.cyan ); g.fillArc( 140, 40, 120, 120, 90, 45 ); g.setColor( Color.magenta ); g.fillArc( 150, 150, 100, 100, 90, 90 ); g.setColor( Color.black ); g.fillArc( 160, 160, 80, 80, 90, 90 ); g.setColor( Color.green ); g.drawString( "Groovy!", 50, 150 ); } }

---------------------Exercise 4: Mouse Input The source code: import java.applet.*; import java.awt.*; import java.awt.event.*; public class Mouse1 extends Applet implements MouseListener, MouseMotionListener { int width, height; int mx, my; // the mouse coordinates boolean isButtonPressed = false; public void init() { width = getSize().width; height = getSize().height; setBackground( Color.black ); mx = width/2; my = height/2; addMouseListener( this ); addMouseMotionListener( this ); } public void mouseEntered( MouseEvent e ) { // called when the pointer enters the applet's rectangular area } public void mouseExited( MouseEvent e ) { // called when the pointer leaves the applet's rectangular area } public void mouseClicked( MouseEvent e ) { // called after a press and release of a mouse button // with no motion in between // (If the user presses, drags, and then releases, there will be // no click event generated.) } public void mousePressed( MouseEvent e ) { // called after a button is p

ressed down isButtonPressed = true; setBackground( Color.gray ); repaint(); // "Consume" the event so it won't be processed in the // default manner by the source which generated it. e.consume(); } public void mouseReleased( MouseEvent e ) { // called after a button is released isButtonPressed = false; setBackground( Color.black ); repaint(); e.consume(); } public void mouseMoved( MouseEvent e ) { // called during motion when no buttons are down mx = e.getX(); my = e.getY(); showStatus( "Mouse at (" + mx + "," + my + ")" ); repaint(); e.consume(); } public void mouseDragged( MouseEvent e ) { // called during motion with buttons down mx = e.getX(); my = e.getY(); showStatus( "Mouse at (" + mx + "," + my + ")" ); repaint(); e.consume(); } public void paint( Graphics g ) { if ( isButtonPressed ) { g.setColor( Color.black ); } else { g.setColor( Color.gray ); } g.fillRect( mx-20, my-20, 40, 40 ); } } -----------------Exercise 5: Keyboard Input The source code: import java.applet.*; import java.awt.*; import java.awt.event.*; public class Keyboard1 extends Applet implements KeyListener, MouseListener { int width, height; int x, y; String s = ""; public void init() {

width = getSize().width; height = getSize().height; setBackground( Color.black ); x = width/2; y = height/2; addKeyListener( this ); addMouseListener( this ); } public void keyPressed( KeyEvent e ) { } public void keyReleased( KeyEvent e ) { } public void keyTyped( KeyEvent e ) { char c = e.getKeyChar(); if ( c != KeyEvent.CHAR_UNDEFINED ) { s = s + c; repaint(); e.consume(); } } public void mouseEntered( MouseEvent e ) { } public void mouseExited( MouseEvent e ) { } public void mousePressed( MouseEvent e ) { } public void mouseReleased( MouseEvent e ) { } public void mouseClicked( MouseEvent e ) { x = e.getX(); y = e.getY(); s = ""; repaint(); e.consume(); } public void paint( Graphics g ) { g.setColor( Color.gray ); g.drawLine( x, y, x, y-10 ); g.drawLine( x, y, x+10, y ); g.setColor( Color.green ); g.drawString( s, x, y ); } } -------Exercise 6: Threads and Animation The only functions we have seen in applets so far are init(), paint(), and funct ions called in response to input events. All of these functions are supposed to do a small amount of work and return quickly. There has been no opportunity, so far, for a function to loop and do some continuous work. This applet creates a thread, a separate stream round task. The body of the thread's code is in , the purpose of the thread is to increment the seconds, and cause the applet to redraw itself. . import java.applet.*; import java.awt.*; of execution, to perform a backg the run() function. In this case variable i once every 1000 milli The result is a simple animation

public class Threads1 extends Applet implements Runnable { int width, height; int i = 0; Thread t = null; boolean threadSuspended; // Executed when the applet is first created. public void init() { System.out.println("init(): begin"); width = getSize().width; height = getSize().height; setBackground( Color.black ); System.out.println("init(): end"); } // Executed when the applet is destroyed. public void destroy() { System.out.println("destroy()"); } // Executed after the applet is created; and also whenever // the browser returns to the page containing the applet. public void start() { System.out.println("start(): begin"); if ( t == null ) { System.out.println("start(): creating thread"); t = new Thread( this ); System.out.println("start(): starting thread"); threadSuspended = false; t.start(); } else { if ( threadSuspended ) { threadSuspended = false; System.out.println("start(): notifying thread"); synchronized( this ) { notify(); } } } System.out.println("start(): end"); } // Executed whenever the browser leaves the page containing the applet. public void stop() { System.out.println("stop(): begin"); threadSuspended = true; } // Executed within the thread that this applet created. public void run() { System.out.println("run(): begin"); try { while (true) { System.out.println("run(): awake"); // Here's where the thread does some work ++i; // this is shorthand for "i = i+1;"

if ( i == 10 ) { i = 0; } showStatus( "i is " + i ); // Now the thread checks to see if it should suspend itself if ( threadSuspended ) { synchronized( this ) { while ( threadSuspended ) { System.out.println("run(): waiting"); wait(); } } } System.out.println("run(): requesting repaint"); repaint(); System.out.println("run(): sleeping"); t.sleep( 1000 ); // interval given in milliseconds } } catch (InterruptedException e) { } System.out.println("run(): end"); } // Executed whenever the applet is asked to redraw itself. public void paint( Graphics g ) { System.out.println("paint()"); g.setColor( Color.green ); g.drawLine( width, height, i * width / 10, 0 ); } } -----