Mrs Howe's Cookbook for Java
IMPORT CALLS
import javax.swing.JOptionPane;
import java.awt.Graphics;
import javax.swing.JApplet;
APPLICATION FRAMEWORK
public class ClassName{
public static void main(String[] args){
YOURCODEHERE
System.exit(0);
}
}
APPLET FRAMEWORK
public class WelcomeApplet extends JApplet {
code here
}
MORE APPLET STUFF
public void paint ( Graphics yourVariableName ) {
super.paint( yourVariableName );
yourVariableName .drawString ( "YOURWORDS", YOURX, YOURY );
}
SYSTEM COMMANDS
System.out.print("WHAT
YOU WANT TO SAY");
System.out.print(variableName);
System.exit(0);
~MESSAGE BOX/ INPUT BOX
JOptionPane.showMessageDialog(null,"WHAT YOU WANT TO SAY "+ variableName);
stringVariableName = JOptionPane.showInputDialog("YOUR PROMPT");
ICONS FOR POP-UP BOXES
JOptionPane.showMessageDialog(null,"WHAT YOU WANT TO SAY ","YOUR TITLE BAR", JOptionPane.PLAIN_MESSAGE);
JOptionPane.showMessageDialog(null,"WHAT YOU WANT TO SAY ","YOUR TITLE BAR", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null,"WHAT YOU WANT TO SAY ","YOUR TITLE BAR", JOptionPane.WARNING_MESSAGE);
JOptionPane.showMessageDialog(null,"WHAT YOU WANT TO SAY ","YOUR TITLE BAR", JOptionPane.QUESTION_MESSAGE);
CONTROL COMMANDS
for
for (initial conditions; conditions; increment)
{
}
while
while (condition)
{
}
do while
do{
}while(condition);
switch
switch(variableName) {
case 1:
commands
break;
case 2
commands
break
}
GUI STUFF
DATA TYPES
(primitive)
int
double
short, long, byte, char, and float
STRING
(from data.lang)
No need to import this library ( automatic.)
String stringVariableName;
Strings can be converted to double or int by a "parse" command. For example:
doubleVariableName = Double.parseDouble(stringVariableName);
integerVariableName = Integer.parseInt(stringVariableName);
MATH CLASS
Math.abs( something)
Math.ceil( something)
Math.floor(something)
Math.round(something)
Math.sin(something)
Math.cos(something)
Math.tan(something)
Math.exp(something)
Math.log(something)
Math.max(something,something)
Math.min(something,something)
Math.sqrt(something)
Math.toDegrees(something)
Math.toRadians(something)
Math.PI
Math.E
Math.random( )
Math.pow( somefloat, somefloat)
variable type.MAX_VALUE
variable type.MIN_VALUE
"REGULAR" MATH
+ - * % /
equal (assignment) =
equal (comparison) ==
modulus %
not equal !=
ESCAPE CODE SEQUENCES
new line \n
tab \t
backslash \\
return \r
quotes \"
COMMENTS
//
/* multiple line
comment */
COLORS (predefined by Color class)
Color.white
Color.black
Color.gray
Color.lightGray
Color.darkGray
Color.blue
Color.green
Color.cyan
Color.red
Color.magneta
Color.pink
Color.orange
Color.yellow
or make your own color using integers (0-255 for r,g,b)
see Maria's book page 379 for more information
_____.setColor(new Color( r,g,b);
PAINT COMMANDS
import java.awt.Graphics
GraphicsObject.drawRect(x,y,width,height);
GraphicsObject.drawString("message");
GraphicsObject.drawRect(x,y,width,height);
GraphicsObject.fillRect(x,y,width,height);
GraphicsObject.drawRoundRect(x,y,width,height,arcwidth,archeight);
GraphicsObject.drawOval(x,y,width,height);
GraphicsObject.fillOval(x,y,width,height);
GraphicsObject.setColor(color);
All classes inherit from Object
Object has toString, equals and hashCode methods
MORE STRING STUFF
You don't have to "new" all strings
String s="AOK";
is equivalent to String s = new String("AOK");
Methods
.length()
substring(int startIndex)
.substring(int startIndex, int endIndex)
"first one you want, first one you don't want" count from 0
.equals( )
.indexOf( String)
.intValue()
.compareTo()