Event handlers

advertisement
Lec 16
Adding Mouse and KeyEvent
handlers to an Applet Class
Agenda
• More Applet stuff
– Drawing Strings on the applet window
– Responding to mouse events
– Responding to key strokes
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
Drawing on Computer
• Normal vs
Computer Graphics Coordinates
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);
}
}
Java Graphics Class
• http://download.oracle.com/javase/1.4.2/doc
s/api/java/awt/Graphics.html
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
Adding Strings to an Applet Window
abstract void
drawString(String str, int x, int y)
Draws the text given by the specified string, using this graphics
context's current font and color.
• let's us draw words on the screen
– g.drawString("hello world",100,200);
– g.drawString("WHAT?", 200,400);
Changing the Font
Font(String name, int style, int size)
Creates a new Font from the specified name, style and point size.
• 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!
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;
}
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);
}
mouse handler changes x and y,
repaints
public void mousePressed(MouseEvent e) {
x= e.getX();
y= e.getY();
repaint();
}
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();
}
Lab16Target
• Write an Applet that draws an arrow
• the arrow should move to mouse when clicked
• the arrow should move when arrow keys
pressed
Download