Java Quick Reference (page 2)

Plotting a graph
Plot myPlot = new Plot("Title",xMin,xMax,xStep,yMin,yMax,yStep);
     // xStep and yStep are grid line spacings
myPlot.addPoint(x,y);
myPlot.setPointSize(5);    // size in pixels; default is 3
myPlot.setPointShape(Plot.CIRCLE);   // default is SQUARE
myPlot.setPointShape(Plot.COLUMN);   // for column (bar) graphs
myPlot.setColor(Color.blue);         // (must import java.awt.Color)
myPlot.setConnected(true);           // connect points with lines
Creating a GUI window with text
import java.awt.*;                           // put this at top
Frame myFrame = new Frame("See the label!");     // make a window
Panel myPanel = new Panel();                     // make a panel
Label myLabel = new Label("Hello, world!");      // make a label
myPanel.add(myLabel);                        // put label into panel
myFrame.add(myPanel);                        // put panel into frame
myFrame.pack();                              // size frame to hold contents
myFrame.setVisible(true);                    // and show it!
Creating a push button
import java.awt.event.*;                           // put this at top
Button myButton = new Button("Press me!");         // make a button
myButton.addActionListener(new ActionListener() {  // say what to do
  public void actionPerformed(ActionEvent e) {     //  when button
    System.out.println("Hello, world!");           //  is pressed
  }});
myPanel.add(myButton);                      // put button into panel
Scrollbar (for a parameter of type double)
myFrame.setLayout(new GridLayout(0,1));
     // arranges multiple DoubleScrollers in a vertical column
DoubleScroller v0Scroll = new DoubleScroller("v0 in m/s = ",0,50,0.5,20);
     // parameters are min, max, step size, initial value
myFrame.add(v0Scroll);            // (could also add to a Panel)
v0 = v0Scroll.getValue();         // call this when value is needed
Creating a space to draw Put "extends Canvas" into the class declaration, right after the class name. In the constructor method, 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/