Download MEMO Java

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
1
MEMO Java 1.2.
Java Library Doc:
Mihaela Malita
mmalita@smcm.edu
http://java.sun.com/j2se/1.3/docs/api/index.html
// JAVA program: File is called Counter.java
public class Counter {
// Prints integer values from 1 to LIMIT
public static void main (String [] args) { // main =
final int LIMIT = 5; //final means LIMIT is a constant cannot be changed
int count = 1;
while (count <= LIMIT) {
// counts till 5
System.out.println (count);
count = count + 1; }
do { count = count + 1;
// same with a DO
System.out.println (count); }
while (count < LIMIT);
for (int count=1; count <= LIMIT; count++) // same with FOR
System.out.println (count);
}}
RUN the progam Counter.java
>javac Counter.java
// results the file Counter.class
>java Counter
// interprets the byte code and prints results
>java Counter hello
// args[0] is “hello”
Math class. Ex: int n; n=(int)(Math.Random()*10); 0<= n<10
Operations: + , - , /, *, % Number type: int, double (and float, long)
Math.abs(-3) Math.sin(0.5) (in radians)
Math.exp(4)
Math.random() -> 0<= rezult<1
Math.sqrt(4)
Math.pow(2,3) -> 8
Math.floor(9.3) Math.min(4,6) Math.max(4,6)
String class. Ex: s = "I love java!"; s.length() -> 12
s=s.concat(s); s -> "I love Java!I love Java!"
charAt(index) concat(str)
equals(anObject)
indexOf(int ch)
length()
endsWith(suffix)
replace(oldChar,newChar)
replaceAll(regex, new)
substring(begin)
substring(begin, end)
toUpperCase() toDownCase()
Vector class Vector myv = new Vector();
addElement(obj)
insertElement(obj,index)
setElementAt(obj,index)
remove(index) removeElement(obj)
removeElementAt(index)
contains(obj)
indexOf(obj)
elementAt(index)
size()
isEmpty()
clear()
APPLET. Just draws
import java.applet.*; // for Applet class
import java.awt.*; // Abstract Windowing Toolkit - for Graphics
public class MyDraw extends Applet {
public void init () { // initial conditions + parameters for applet
// setBackground(Color.black);
// setSize(APPLET_WIDTH, APPLET_HEIGHT);
}
public void paint (Graphics page) { // page.draw*(..) or page.fill*(..) or ..
// page.drawLine(10,12,60,75);
}}
RUN the applet MyDraw.java
>javac MyDraw.java
// results the file: MyDraw.class
>appletviewer test.html //or look in test.html with Internet Explorer
// MyDraw.class in the same folder with test.html:
<HTML> <HEAD><TITLE>test</TITLE></HEAD> <BODY >..
<APPLET CODE="MyDraw.class" WIDTH="300" HEIGHT="200"
ALIGN="BOTTOM"> </APPLET>... </BODY></HTML>
setBackground(Color.cyan);
getColor();
setColor(Color.cyan);
drawRect(x,y,width,height);
drawOval(x,y,width,height);
fillRect(x,y,w,h);
fillOval(x,y,w,h);
drawLine(x1,y1,x2,y2);
drawString("string",x,y);
drawArc("string",x,y);
fillArc(x,y,w,h);
Add images:
Image myimage;
//declare object
myimage= getImage(getCodeBase(),”happyface.gif”);
public void paint (Graphics page){ //use myimage in paint
page.drawImage(myimage,x,y,weight,height,this);}
Add sounds:
AudioClip mybonk= new AudioClip; //declare object
mybonk= getSound(getCodeBase(),”bonk.au”);
mybonk.play(); //or mybonk= new AudioClip(http://www.p.com/bonk.au);
Modifiers: public, private, protected
MOUSE: interfaces MouseListener and MouseMotionListener
import java.awt.event.*;
// for events
public class MyDraw extends Applet implements MouseListener {
Point mypoint; //
public void init() {
// add usual initialization
addMouseListener (this);
}
public void paint (Graphics page) { // all drawings
page.drawString ("Mouse is clicked at" + mypoint,10,10);
}
public void mouseClicked (MouseEvent event) {
mypoint= event.getPoint();
// also event.getX(), event.getY()
repaint(); // draws again the paint method
2
}
// unused methods from the MouseListener Interface
public void mousePressed (MouseEvent event) {}
public void mouseReleased (MouseEvent event) {}
public void mouseEntered (MouseEvent event) { }
public void mouseExited (MouseEvent event) { }
}
Interface MouseMotionListener. Abstract methods are:
public void mouseDragged (MouseEvent event){}
public void mouseMoved (MouseEvent event) {}
public class mmkey extends Applet implements KeyListener {
public void init() {
requestFocus(); // make sure the applet has the keyboard focus
addKeyListener (this); // method from KeyListener Interface
}
public void keyPressed (KeyEvent event) {
switch (event.getKeyCode()) {
case KeyEvent.VK_UP: //also VK_DOWN, VK_RIGHT ..
System.out.println(" You typed the UP key");
break;
case KeyEvent.VK_A: // also VK_B,… , VK_Z
System.out.println(" You typed the letter A");
break; …
default: ..
}}
public void keyTyped (KeyEvent event) {} //empty def for unused methods
public void keyReleased (KeyEvent event) {} //empty def for unused methods
}
Or extend your class to MouseAdapter
public class OffCenter extends Applet {
public void init() {
addMouseListener (new OffCenterListener());
}
public void paint (Graphics page) { … }
class OffCenterListener extends MouseAdapter {
public void mouseClicked (MouseEvent event) {
… current = event.getPoint(); …
repaint();}
}}
// GUI: Graphic User Interface: TEXT FIELDS + LABELS
// Ex: asks for a string in a text field, displays string with uppercase letters
//
Input a string: mary scary
//
String with capital letters: MARY SCARY
public class mmupcase extends Applet implements ActionListener {
private Label in, out, res;
private TextField mys;
public void init() {
in = new Label ("Input a string:"); //initial value of in
out = new Label ("String with capital letters:");
res = new Label ("
");
mys = new TextField (15); //size of TextField is 15 chars
mys.addActionListener (this);
add (in);
add (mys);
add (out);
add (res);
} // Transforms string when ENTER key is pressed in the text field
public void actionPerformed (ActionEvent event) {
String s = mys.getText();
res.setText(s.toUpperCase());
}}
Font class. constructor Font(name,style,size)
public void init() {…
Font myfont= new Font("Sans Serif",Font.BOLD, 14);
in = new Label ("Input a string:");
in.setFont(myfont);
add (in); }
GUI: BUTTON. Ex: When pressed draws happy randomly
public class mybutton extends Applet implements ActionListener {
private Button myButton;
private Image happy;
private int i=50;
public void init () {
happy= getImage(getCodeBase(),"happyFace.gif");
add (titleLabel);
myButton = new Button("Draw");
myButton.addActionListener (this);
add(myButton); }
public void actionPerformed (ActionEvent event) {
i = (int)(Math.random()*100); //random integers < 100
repaint(); }
public void paint(Graphics page){
page.drawImage(happy,i, i ,this); //place of image is (i,i)
}}
Related documents