Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lec 16 Adding Mouse and KeyEvent handlers to an Applet Class.

Similar presentations


Presentation on theme: "Lec 16 Adding Mouse and KeyEvent handlers to an Applet Class."— Presentation transcript:

1 Lec 16 Adding Mouse and KeyEvent handlers to an Applet Class

2 Agenda More Applet stuff – Drawing Strings on the applet window – Responding to mouse events – Responding to key strokes

3 Two Ways to Use Java Applet—runs on the web – uses graphical interfaces (usually) – can't access files on local computer – downloaded from website by browser – no main method in class! Application—runs on a local computer – has main method – Can run "stand alone" on local computer – can use graphical or nongraphical interfaces

4 Drawing on Computer Normal vs Computer Graphics Coordinates

5 Simple Drawing Demo Applet import java.applet.*; //so we can extend the Applet class import java.awt.*; //so we can use graphics commands public class DrawDemo extends Applet { public void paint(Graphics g) { //draws a blue oval on the screen g.setColor(new Color(0, 0, 255)); g.fillRect(100,100,30,50); g.setColor(new Color(0, 255, 0)); g.drawLine(115,125, 315,125); g.setColor(new Color(255, 0, 0)); g.drawOval(275, 85, 80, 80); }

6 Java Graphics Class http://download.oracle.com/javase/1.4.2/doc s/api/java/awt/Graphics.html http://download.oracle.com/javase/1.4.2/doc s/api/java/awt/Graphics.html

7 For today's demo Create new project, Sign Download appletShell from Lab16, call it Sign Notice the "implements" and extra methods – required to be able to access keyboard and mouse

8 Adding Strings to an Applet Window let's us draw words on the screen – g.drawString("hello world",100,200); – g.drawString("WHAT?", 200,400); abstract void drawStringdrawString(String str, int x, int y) Draws the text given by the specified string, using this graphics context's current font and color.String

9 Changing the FontFont set the font size: – g.setFont( new Font(null,Font.PLAIN, 30)); (null means no value: use the default font) – g.setFont( new Font("Courier",Font.BOLD, 16)); takes awhile to load the font! FontFont(String name, int style, int size) Creates a new Font from the specified name, style and point size.String

10 Instance variables We can change the location and size of our String by making instance variables in our applet class. int x,y; // the x and y coordinates of the next String int size; // the size of the string Then, inside the init() method, we start them at their initial values: public void init() { //init is called when applet starts (like a constructor) addMouseListener(this); //registers this applet to receive mse events addKeyListener(this); //registers this applet to receive key events x=100; y=200; size=10; }

11 our paint method needs mods public void paint(Graphics g) { g.drawString("hello world",x,y); g.setFont( new Font(null,Font.BOLD,size)); g.drawString("hello back!",x,y+100); }

12 mouse handler changes x and y, repaints public void mousePressed(MouseEvent e) { x= e.getX(); y= e.getY(); repaint(); }

13 Similarly, in keyPressed... public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_UP) size=size+10; if (e.getKeyCode() == KeyEvent.VK_DOWN) size=size-10; repaint(); }

14 Write an Applet that draws an arrow the arrow should move to mouse when clicked the arrow should move when arrow keys pressed Lab16Target


Download ppt "Lec 16 Adding Mouse and KeyEvent handlers to an Applet Class."

Similar presentations


Ads by Google