490 likes | 629 Views
Java Applets for Experiments. NSF Workshop, UC-Fullerton Gary McClelland 17-20 January 2002. Java Workshop Outline. Java, what it is and isn’t Object-Oriented Programming (OOP) First applet: HelloWorld Graphics drawing Listening for events mouse, keys, Buttons, Scrollbars.
E N D
Java Applets for Experiments NSF Workshop, UC-Fullerton Gary McClelland 17-20 January 2002
Java Workshop Outline • Java, what it is and isn’t • Object-Oriented Programming (OOP) • First applet: HelloWorld • Graphics drawing • Listening for events • mouse, keys, Buttons, Scrollbars
Java ≠ Javascript • Javascript • HTML scripting language • Forms • Interpreted • Java • Applets embedded in HTML page • Standalone applications • Complete programming language • Compiled
Java’s Goods & Bads • Strengths • Graphics • Interactivity (mouse, keys, scrollbars) • Precise control • Weaknesses • Timing at the ms level • Learning curve
History of an Applet • Edit java source code & html • notepad Hello.java • notepad Hello.html • Compile source to ByteCodes • javac Hello.java • produces Hello.class • View applet (Java Virtual Machine) • appletviewer Hello.html • browser Hello.html
<html> <body> <applet code=“Hello.class” width=“300” height=“300”> </applet> </body> </html>
Object-Oriented Programming (OOP) • Objects • State Variables • Behavior Methods • Bicycle • State: cadence, gear, speed • Behavior: brake, accelerate, changeGear
Java class: object blueprint • Class variables • Constructor (object “factory”) • Methods (subroutine definitions)
public class bicycle extends Object { Gear front; Gear rear; Pedal ped; public bicycle(Color col, int size, int frontTeeth,int rearTeeth) { //blueprint for bicycle goes here front = new Gear(frontTeeth); rear = new Gear(rearTeeth); } public void changeGears(String dir) { //method, details next screen } }
public void changeGears(String dir) { if(dir==“lower”) { if(rear.hasLower()) rear.lower(); else if(front.hasLower()) front.lower(); else print(“suck it up!”); } else //similar for higher }
public class Pedal extends Object { private int currentCadence; //rpm public Pedal() { currentCadence = 0; } public void setCadence(int c){ currentCadence = c; } public void changeCadence(int c){ currentCadence = currentCadence + c; } public int getCadence() { return currentCadence; } }
public class Pedal extends Object { private int currentCadence; //rpm public Pedal() { currentCadence = 0; } public void setCadence(int c){ currentCadence = c; } public void changeCadence(int c){ currentCadence = currentCadence + c; } public int getCadence() { return currentCadence; } }
public class Pedal extends Object { privateint currentCadence; //rpm public Pedal() { currentCadence = 0; } public void setCadence(int c){ currentCadence = c; } public void changeCadence(int c){ currentCadence = currentCadence + c; } public int getCadence() { return currentCadence; } }
public class Pedal extends Object { private int currentCadence; //rpm public Pedal() { currentCadence = 0; } public void setCadence(int c){ currentCadence = c; } public void changeCadence(int c){ currentCadence = currentCadence + c; } public int getCadence() { return currentCadence; } }
public class Pedal extends Object { private int currentCadence; //rpm public Pedal() { currentCadence = 0; } public void setCadence(int c){ currentCadence = c; } public void changeCadence(int c){ currentCadence = currentCadence + c; } public int getCadence() { return currentCadence; } }
Card Constructor • Card(String s1, Color c1, String s2, Color c2, int fontSize) • s1: text on “front” of card • c1: color on “front” of card • s2: text on “reverse” of card after click • c2: color on “reverse” of card after click • fontSize: size of font for both Strings
Card Object As Loaded After Click add(new Card(“front”,Color.blue,“reverse”, Color.red, 14));
Using Card in Wason Task public void init() { setLayout(new GridLayout(1,4,20,0)); add(new Card("E",Color.white,"E",Color.yellow,24)); add(new Card("K",Color.white,"K",Color.yellow,24)); add(new Card("4",Color.white,"4",Color.yellow,24)); add(new Card("5",Color.white,"5",Color.yellow,24)); repaint(); }
Wason Task with Feedback public void init() { setLayout(new GridLayout(1,4,20,0)); add(new Card("E",Color.white,"E",Color.green,24)); add(new Card("K",Color.white,"K",Color.red,24)); add(new Card("4",Color.white,"4",Color.red,24)); add(new Card("5",Color.white,"5",Color.green,24)); repaint(); }
Diagnosis Code public void init() { int rows = 20; int cols = 20; int n = rows * cols; int pos = (38*n)/100; //38% of tests are positive int neg = n - pos; setLayout(new GridLayout(rows,cols,5,5)); int cpos = (80*pos)/100; //80% of pos are true pos int cneg = pos - cpos; for(int i=1; i<=pos; i++) { if(Math.random() < (double)cpos/(double)(cpos+cneg)) { add(new Card("M+",Color.yellow,"C+",Color.red,12)); cpos = cpos - 1; } else { add(new Card("M+",Color.yellow,"C-",Color.green,12)); cneg = cneg - 1; } }
Diagnosis Code, Part 2 cpos = (20*pos)/100; //20% of negs are false pos cneg = neg - cpos; for(int i=pos+1; i<=n; i++) { if(Math.random() < (double)cpos/(double)(cpos+cneg)) { add(new Card("M-",Color.white,"C+",Color.red,12)); cpos = cpos - 1; } else { add(new Card("M-",Color.white,"C-",Color.green,12)); cneg = cneg - 1; } } repaint(); }
Your Turn! • You first applet • “Hello World”
History of an Applet • Edit java source code & html • notepad Hello.java • notepad Hello.html • Compile source to ByteCodes • javac Hello.java • produces Hello.class • View applet (Java Virtual Machine) • appletviewer Hello.html • browser Hello.html
<html> <body> <applet code=“Hello.class” width=“300” height=“300”> </applet> </body> </html>
<html> <body> <applet code=“Hello.class” width=“300” height=“300”> </applet> </body> </html> Save as Hello.html
import java.applet.Applet; Import java.awt.*; public class Hello extends Applet { public void init() { repaint(); } public void paint(Graphics g) { g.drawString(“Hello World!”,30,30); } }
import java.applet.Applet; Import java.awt.*; public class Hello extends Applet { public void init() { repaint(); } public void paint(Graphics g) { g.drawString(“Hello World!”,30,30); } } Save as Hello.java
History of an Applet • Edit java source code & html • notepad Hello.java • notepad Hello.html • Compile source to ByteCodes • javac Hello.java • produces Hello.class • View applet (Java Virtual Machine) • appletviewer Hello.html • browser Hello.html
History of an Applet • Edit java source code & html • notepad Hello.java • notepad Hello.html • Compile source to ByteCodes • javac Hello.java • produces Hello.class • View applet (Java Virtual Machine) • appletviewer Hello.html • browser Hello.html
Graphics Object g (0,0) (x,y) (width,height)
Graphics Methods: Size • getSize().width; • int wd = getSize().width; • getSize().height; • int ht = getSize().height; • g.drawRect(0,0,wd,ht); • graws largest possible rectangle • g.drawString(“Center”,wd/2, ht/2);
Graphics Methods: Shapes • g.drawRect(x,y,w,h); • g.fillRect(x,y,w,h); • g.drawOval(x,y,w,h); • g.fillOval(x,y,w,h); • g.drawLine(x1,y1,x2,y2);
Graphics: More Shapes • g.drawPolygon(xPts,yPts,nPts); • g.fillPolygon(xPts,yPts,nPts); • g.drawArc(x,y,w,h,startAngle,endAngle); • g.fillArc(x,y,w,h,startAngle,endAngle);
Graphics Methods: Colors • g.setColor(Color.black); • Color.red, Color.blue, Color.green, Color.orange, Color.magenta, others… • g.setColor(new Color(r, g, b)); • 0 ≤ r ≤ 255 • 0 ≤ g ≤ 255 • 0 ≤ b ≤ 255 • setBackground(Color.yellow);
Graphics Methods: Fonts • g.setFont(new Font(font, style, size)); • Fonts: “Helvetica” “Courier” “Times” • Style: Font.PLAIN, Font.BOLD, Font.ITALIC • Size: any integer • g.drawString(string, x, y);
FontMetrics • FontMetrics fm; • fm=getFontMetrics(getFont()); • int len = fm.stringWidth(“Center”); • int fht = fm.getHeight(); • g.drawString(“Center”, wd/2-len/2, ht/2+fht/2);
Arrays • int x[] = new int[3]; • Note: arrays start at 0 • Above creates x[0], x[1], x[2] • double y[] = new double[5]; • Card cards[][]; • cards = new Card[20][10]; • cards[2][3] = new Card();
Control Structures if (logical statement) { } else { } for(int i=1; i<=n; i++) { }
Widgets • Button • Scrollbar • TextField • ChoiceList • Menu • Checkbox
MouseListener • Object implements an interface • public class MouseDemo extends Applet implements MouseListener { • Object registers to listen for events • addMouseListener(this);
MouseListener Methods • Object must have these methods • public void mouseClicked(MouseEvent me) • public void mousePressed(MouseEvent me) • public void mouseReleased(MouseEvent me) • public void mouseEntered(MouseEvent me) • public void mouseExited(MouseEvent me)
MouseEvent methods • getX() • getY() • getPoint() • getWhen()
Saving Data via cgi //send data to server //data saved locally in String dataString; public void recordData() {try { Socket t = new Socket("samiam.colorado.edu",80); DataOutputStream out = new DataOutputStream(t.getOutputStream());
Constructing (Faking) POST out.writeBytes( "POST "+ "http://www.myuni.edu/scripts/saveData.cgi" + " HTTP/1.0\r\n"); out.writeBytes( "Content-type: application/octet-stream\r\n"); out.writeBytes( "Content-length: " + dataString.length() + "\r\n\r\n"); out.writeBytes(dataString);
Remainder of recordData() t.close(); //close Socket to server } catch(IOException e) {System.out.println("Error" + e); } } }
recordData() complete public void recordData() { //send data to server try { Socket t = new Socket(”www.myuni.edu",80); DataOutputStream out = new DataOutputStream(t.getOutputStream()); out.writeBytes( "POST "+ "http://www.myuni.edu/scripts/saveData.cgi"+ " HTTP/1.0\r\n"); out.writeBytes("Content-type: application/octet-stream\r\n"); out.writeBytes( "Content-length: " + dataString.length() + "\r\n\r\n"); out.writeBytes(dataString); t.close(); } catch(IOException e) {System.out.println("Error" + e); } } }