import java.applet.*; import java.awt.*; public class Example01 extends Applet { public void init() { setBackground( Color.black ); } public void paint( Graphics g ) { // The origin (0,0) is at the upper left corner. // x increases to the right, and y increases downward. g.setColor( Color.white ); for ( int i = 0; i < 5; ++i ) { g.drawLine( 10, 55, 110, 55+i*8 ); } g.setColor( Color.red ); g.drawRect( 10, 20, 100, 15 ); g.setColor( Color.pink ); g.fillRect( 240, 160, 40, 110 ); g.setColor( Color.blue ); g.drawOval( 50, 225, 100, 50 ); g.setColor( Color.orange ); g.fillOval( 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( "Superbe!", 50, 150 ); } }
import java.applet.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class Example02 extends Applet implements MouseListener, MouseMotionListener { Vector< Point > listOfPositions = new Vector< Point >(); public void init() { setBackground( Color.black ); addMouseListener( this ); addMouseMotionListener( this ); } public void mouseEntered( MouseEvent e ) { } public void mouseExited( MouseEvent e ) { } public void mouseClicked( MouseEvent e ) { } public void mousePressed( MouseEvent e ) { } public void mouseReleased( MouseEvent e ) { } public void mouseMoved( MouseEvent e ) { if ( listOfPositions.size() >= 50 ) { listOfPositions.removeElementAt( 0 ); } listOfPositions.addElement( new Point( e.getX(), e.getY() ) ); repaint(); e.consume(); } public void mouseDragged( MouseEvent e ) { } public void paint( Graphics g ) { g.setColor( Color.white ); for ( int j = 1; j < listOfPositions.size(); ++j ) { Point A = listOfPositions.elementAt( j-1 ); Point B = listOfPositions.elementAt( j ); g.drawLine( A.x, A.y, B.x, B.y ); } } }
import java.applet.*; import java.awt.*; import java.awt.event.*; public class Example03 extends Applet implements KeyListener, MouseListener { int x, y; String s = ""; public void init() { setBackground( Color.black ); x = getWidth() / 2; y = getHeight() / 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 ); } }
import java.applet.*; import java.awt.*; import java.awt.event.*; public class Example04 extends Applet implements MouseListener, MouseMotionListener { int mouse_x, mouse_y; // the mouse coordinates boolean isButtonPressed = false; public void init() { setBackground( Color.black ); mouse_x = getWidth() / 2; mouse_y = getHeight() / 2; addMouseListener( this ); addMouseMotionListener( this ); } public void mouseEntered( MouseEvent e ) { } public void mouseExited( MouseEvent e ) { } public void mouseClicked( MouseEvent e ) { } public void mousePressed( MouseEvent e ) { isButtonPressed = true; setBackground( Color.gray ); repaint(); e.consume(); } public void mouseReleased( MouseEvent e ) { isButtonPressed = false; setBackground( Color.black ); repaint(); e.consume(); } public void mouseMoved( MouseEvent e ) { mouse_x = e.getX(); mouse_y = e.getY(); showStatus( "Mouse at (" + mouse_x + "," + mouse_y + ")" ); repaint(); e.consume(); } public void mouseDragged( MouseEvent e ) { mouse_x = e.getX(); mouse_y = e.getY(); showStatus( "Mouse at (" + mouse_x + "," + mouse_y + ")" ); repaint(); e.consume(); } public void paint( Graphics g ) { g.setColor( isButtonPressed ? Color.black : Color.gray ); g.fillRect( mouse_x-20, mouse_y-20, 40, 40 ); } }
import java.applet.*; import java.awt.*; import java.awt.event.*; public class Example05 extends Applet implements MouseListener, MouseMotionListener { int x0, y0; // coordinates of the upper-left corner of the box static final int boxWidth = 40; static final int boxHeight = 40; int mouse_x, mouse_y; // most recently recorded mouse coordinates boolean isBoxBeingDragged = false; public void init() { setBackground( Color.black ); x0 = getWidth() / 2 - boxWidth / 2; y0 = getHeight() / 2 - boxHeight / 2; addMouseListener( this ); addMouseMotionListener( this ); } public void mouseEntered( MouseEvent e ) { } public void mouseExited( MouseEvent e ) { } public void mouseClicked( MouseEvent e ) { } public void mousePressed( MouseEvent e ) { mouse_x = e.getX(); mouse_y = e.getY(); if ( x0 <= mouse_x && mouse_x < x0+boxWidth && y0 <= mouse_y && mouse_y < y0+boxHeight ) { isBoxBeingDragged = true; } e.consume(); } public void mouseReleased( MouseEvent e ) { isBoxBeingDragged = false; e.consume(); } public void mouseMoved( MouseEvent e ) { } public void mouseDragged( MouseEvent e ) { int new_mouse_x = e.getX(); int new_mouse_y = e.getY(); if ( isBoxBeingDragged ) { // displace the box by the distance // the mouse moved since the last event x0 += new_mouse_x - mouse_x; y0 += new_mouse_y - mouse_y; repaint(); e.consume(); } mouse_x = new_mouse_x; mouse_y = new_mouse_y; } public void paint( Graphics g ) { g.setColor( Color.gray ); g.fillRect( x0, y0, boxWidth, boxHeight ); } }
import java.applet.*; import java.awt.*; import java.awt.event.*; public class Example06 extends Applet implements MouseListener, MouseMotionListener { int window_x, window_y; // upper-left corner of the window int window_width = 60, window_height = 40; // dimensions of window static final int window_resize_box_size = 15; boolean isWindowBeingDragged = false; boolean isWindowBeingResized = false; int mouse_x, mouse_y; public void init() { setBackground( Color.black ); window_x = getWidth()/2 - window_width/2; window_y = getHeight()/2 - window_height/2; addMouseListener( this ); addMouseMotionListener( this ); } boolean isInsideWindow( int x, int y ) { return window_x <= x && x < window_x+window_width && window_y <= y && y < window_y+window_height; } boolean isInsideWindowResizeBox( int x, int y ) { return window_x+window_width-window_resize_box_size <= x && x < window_x+window_width && window_y+window_height-window_resize_box_size <= y && y < window_y+window_height; } public void mouseEntered( MouseEvent e ) { } public void mouseExited( MouseEvent e ) { } public void mouseClicked( MouseEvent e ) { } public void mousePressed( MouseEvent e ) { mouse_x = e.getX(); mouse_y = e.getY(); if ( isInsideWindow( mouse_x, mouse_y ) ) { if ( isInsideWindowResizeBox( mouse_x, mouse_y ) ) { isWindowBeingResized = true; } else isWindowBeingDragged = true; } e.consume(); } public void mouseReleased( MouseEvent e ) { isWindowBeingDragged = isWindowBeingResized = false; e.consume(); } public void mouseMoved( MouseEvent e ) { } public void mouseDragged( MouseEvent e ) { int new_mouse_x = e.getX(); int new_mouse_y = e.getY(); int delta_x = new_mouse_x - mouse_x; int delta_y = new_mouse_y - mouse_y; if ( isWindowBeingDragged ) { window_x += delta_x; window_y += delta_y; repaint(); e.consume(); } else if ( isWindowBeingResized ) { window_width += delta_x; window_height += delta_y; if ( window_width < 2*window_resize_box_size ) window_width = 2*window_resize_box_size; if ( window_height < 2*window_resize_box_size ) window_height = 2*window_resize_box_size; repaint(); e.consume(); } mouse_x = new_mouse_x; mouse_y = new_mouse_y; } public void paint( Graphics g ) { g.setColor( Color.white ); g.drawRect( window_x, window_y, window_width-1, window_height-1 ); g.drawRect( window_x + window_width - window_resize_box_size, window_y + window_height - window_resize_box_size, window_resize_box_size-1, window_resize_box_size-1 ); } }
import java.applet.*; import java.awt.*; import java.awt.event.*; import java.util.*; class Example07_Window { int x0, y0; // upper-left corner static final int width = 30; static final int height = 30; public Example07_Window( int x, int y ) { x0 = x; y0 = y; } public boolean isInside( int x, int y ) { return x0 <= x && x < x0+width && y0 <= y && y < y0+height; } public void move( int delta_x, int delta_y ) { x0 += delta_x; y0 += delta_y; } public void draw( Graphics g, boolean isFilled ) { g.setColor( Color.white ); g.drawRect( x0, y0, width-1, height-1 ); g.setColor( Color.black ); g.fillRect( x0+1, y0+1, width-2, height-2 ); if ( isFilled ) { g.setColor( Color.white ); g.fillRect( x0+2, y0+2, width-4, height-4 ); } } } public class Example07 extends Applet implements MouseListener, MouseMotionListener { Vector< Example07_Window > windows = new Vector< Example07_Window >(); int indexOfWindowBeingDragged = -1; // -1 for none int mouse_x, mouse_y; public void init() { setBackground( Color.black ); for ( int i = 0; i < 5; ++i ) { windows.addElement( new Example07_Window( (int)( Math.random() * getWidth() * 0.9f ), (int)( Math.random() * getHeight() * 0.9f ) ) ); } addMouseListener( this ); addMouseMotionListener( this ); } public void mouseEntered( MouseEvent e ) { } public void mouseExited( MouseEvent e ) { } public void mouseClicked( MouseEvent e ) { } public void mousePressed( MouseEvent e ) { mouse_x = e.getX(); mouse_y = e.getY(); if ( indexOfWindowBeingDragged == -1 ) { for ( int i = windows.size()-1; i >= 0; --i ) { Example07_Window win = windows.elementAt(i); if ( win.isInside( mouse_x, mouse_y ) ) { indexOfWindowBeingDragged = i; repaint(); break; } } } e.consume(); } public void mouseReleased( MouseEvent e ) { if ( indexOfWindowBeingDragged > -1 ) { indexOfWindowBeingDragged = -1; repaint(); } e.consume(); } public void mouseMoved( MouseEvent e ) { } public void mouseDragged( MouseEvent e ) { int new_mouse_x = e.getX(); int new_mouse_y = e.getY(); if ( indexOfWindowBeingDragged > -1 ) { Example07_Window win = windows.elementAt(indexOfWindowBeingDragged); win.move( new_mouse_x - mouse_x, new_mouse_y - mouse_y ); repaint(); e.consume(); } mouse_x = new_mouse_x; mouse_y = new_mouse_y; } public void paint( Graphics g ) { for ( int i = 0; i < windows.size(); ++i ) { Example07_Window win = windows.elementAt(i); win.draw( g, i == indexOfWindowBeingDragged ); } } }
import java.applet.*; import java.awt.*; import java.awt.event.*; import java.util.*; class Example08_Window { int x0, y0; // upper-left corner static final int width = 30; static final int height = 30; public Example08_Window( int x, int y ) { x0 = x; y0 = y; } public boolean isInside( int x, int y ) { return x0 <= x && x < x0+width && y0 <= y && y < y0+height; } public void move( int delta_x, int delta_y ) { x0 += delta_x; y0 += delta_y; } public void draw( Graphics g, boolean isHilited, boolean isFilled ) { g.setColor( Color.white ); g.drawRect( x0, y0, width-1, height-1 ); g.setColor( Color.black ); g.fillRect( x0+1, y0+1, width-2, height-2 ); if ( isHilited || isFilled ) { g.setColor( isFilled ? Color.white : Color.gray ); g.fillRect( x0+2, y0+2, width-4, height-4 ); } } } public class Example08 extends Applet implements MouseListener, MouseMotionListener { Vector< Example08_Window > windows = new Vector< Example08_Window >(); int indexOfWindowUnderCursor = -1; // -1 for none boolean isWindowUnderCursorBeingDragged = false; int mouse_x, mouse_y; public void init() { setBackground( Color.black ); for ( int i = 0; i < 5; ++i ) { windows.addElement( new Example08_Window( (int)( Math.random() * getWidth() * 0.9f ), (int)( Math.random() * getHeight() * 0.9f ) ) ); } addMouseListener( this ); addMouseMotionListener( this ); } private int computeIndexOfWindowUnderCursor() { for ( int i = windows.size()-1; i >= 0; --i ) { Example08_Window win = windows.elementAt(i); if ( win.isInside( mouse_x, mouse_y ) ) { return i; } } return -1; } public void mouseEntered( MouseEvent e ) { } public void mouseExited( MouseEvent e ) { } public void mouseClicked( MouseEvent e ) { } public void mousePressed( MouseEvent e ) { mouse_x = e.getX(); mouse_y = e.getY(); if ( ! isWindowUnderCursorBeingDragged && indexOfWindowUnderCursor > -1 ) { isWindowUnderCursorBeingDragged = true; repaint(); } e.consume(); } public void mouseReleased( MouseEvent e ) { if ( isWindowUnderCursorBeingDragged ) { isWindowUnderCursorBeingDragged = false; repaint(); } e.consume(); } public void mouseMoved( MouseEvent e ) { mouse_x = e.getX(); mouse_y = e.getY(); int newIndex = computeIndexOfWindowUnderCursor(); if ( newIndex != indexOfWindowUnderCursor ) { indexOfWindowUnderCursor = newIndex; repaint(); } e.consume(); } public void mouseDragged( MouseEvent e ) { int new_mouse_x = e.getX(); int new_mouse_y = e.getY(); if ( isWindowUnderCursorBeingDragged ) { Example08_Window win = windows.elementAt(indexOfWindowUnderCursor); win.move( new_mouse_x - mouse_x, new_mouse_y - mouse_y ); repaint(); e.consume(); } mouse_x = new_mouse_x; mouse_y = new_mouse_y; } public void paint( Graphics g ) { for ( int i = 0; i < windows.size(); ++i ) { Example08_Window win = windows.elementAt(i); win.draw( g, i == indexOfWindowUnderCursor && !isWindowUnderCursorBeingDragged, i == indexOfWindowUnderCursor && isWindowUnderCursorBeingDragged ); } } }