Java Quick Reference
Java Quick Reference
"Hello,
world!"
program
(dynamic
version)
javac Hello.java
java Hello
ls
rm *.class
[up arrow]
control-c
int
Primitive
variable types double
boolean
Arithmetic
Shortcuts
+=
-=
*=
Relations
==
!=
<
Logic
&& (and)
|| (or)
++
>
->=
! (not)
java.lang.Math Math.PI
Math.E
Math.cos(t)
Math.acos(x)
Math.log(x) (natural log)
Math.sin(t)
Math.asin(x)
Math.exp(x) (ex)
Math.tan(t)
Math.atan(x)
Math.sqrt(x)
Math.pow(x,y) (xy)
Math.max(x,y)
Math.min(x,y)
Math.abs(x) (absolute value)
Math.round(x)
Math.floor(x)
Math.ceil(x) (round normally, down, or up)
Math.random() (pseudo-random double between 0 and 1)
Converting
data types
(casting)
Control
structures
if (balance <= 0) {
broke = true;
} else {
broke = false;
}
Declaring a
method
Arrays
double[] x;
x = new double[100];
x[0] = aValue;
x[99] = x[98] + dx;
Formatting
numbers
import java.text.*;
// put this line at top of program
DecimalFormat myFormat = new DecimalFormat("0.00");
myString = myFormat.format(myNumber); // or just print it
//
//
//
//
double x0 = 0.0;
// default value
Parsing
// or Integer.parseInt
command-line try {x0 = Double.parseDouble(arg[0]);}
catch (ArrayIndexOutOfBoundsException e) {} // use default if no arg
arguments
catch (NumberFormatException e) {}
Plotting a
graph
// or if invalid
myPlot.setPointShape(Plot.CIRCLE);
myPlot.setPointShape(Plot.COLUMN);
myPlot.setColor(Color.blue);
myPlot.setConnected(true);
//
//
//
//
default is SQUARE
for column (bar) graphs
(must import java.awt.Color)
connect points with lines
Creating a
GUI window
with text
import java.awt.*;
Frame myFrame = new Frame("See the label!");
Panel myPanel = new Panel();
Label myLabel = new Label("Hello, world!");
myPanel.add(myLabel);
myFrame.add(myPanel);
myFrame.pack();
myFrame.setVisible(true);
Creating a
push button
import java.awt.event.*;
Button myButton = new Button("Press me!");
myButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Hello, world!");
}});
myPanel.add(myButton);
// put
Put "extends Canvas" into the class declaration, right after the class name. In the constructor method,
Creating a
space to draw use "setSize(width,height);" to set the size of the Canvas in pixels, and
"setBackground(Color.white)" to set the background color if desired. Create a Panel within a
Frame; use "myPanel.add(this);" to add the Canvas to the Panel. Then create a "public void
paint(Graphics g)" method, which will be called automatically whenever the Canvas needs to be
drawn.
Graphics
methods
g.setColor(Color.red);
draw in a predefined color
g.setColor(new Color(r,g,b));
draw in any color; r,g,b from 0 to 255
g.fillRect(left,top,width,height);
solid rectangle (drawRect draws outline)
g.fillOval(left,top,width,height);
solid oval (drawOval draws outline)
g.drawLine(x1,y1,x2,y2);
draw a line, one pixel wide
g.drawString("Hello",x,y);
draw text starting at x,y
(All coordinates are integers in pixels, with y measured down from the top of the Canvas.)
Creating a
thread
Put "implements Runnable" into the class declaration. In the constructor method, add the statements
"Thread myThread = new Thread(this); myThread.start();". Then create a run method:
public void run() {
while (true) {
// loop forever (until interrupted)
doStuff();
// shouldn't take > 100 ms
try { Thread.sleep(20); }
// time in ms
catch (InterruptedException e) {}
}
}
Animation
Create a Canvas and a Thread, as described above. Within the loop in the run method, add the
statement "repaint();" to ask Java to call your paint (or update) method. Set the sleep duration to
give about 20 or 30 frames per second.
http://physics.weber.edu/schroeder/javacourse/