import java.awt.*; /** * A blank graph, demoing how to draw such a thing. * @author Adam A. Smith * @version 1.0 */ public class BlankGraph { // constants private static final int WINDOW_WIDTH=1000, WINDOW_HEIGHT=1000; private static final int MAX_X = 20, MAX_Y = 20; private static final int NUM_TICKS = 11, TICK_SIZE = 10; private static final int MARGIN = 50; /** * The main() function to start. * @param args command-line arguments (ignored) */ public static void main(String[] args) { GraphicsWindow window = new GraphicsWindow("Blank Graph", 1000, 1000, 50, 50); window.paintBackground(Color.WHITE); drawAxes(window.getPen(), MARGIN, MARGIN, WINDOW_WIDTH-MARGIN, WINDOW_HEIGHT-MARGIN, MAX_X, MAX_Y, NUM_TICKS, TICK_SIZE); window.finalize(); } // args are: pen:Graphics2D, // x0,y0,x1,y1:bounds of the rectangle (not including ticks) // maxX,maxY: the high values for leftmost x & top y // numTicks: # ticks to have on each side // tickLength: # pixels for each tick private static void drawAxes(Graphics2D pen, int x0, int y0, int x1, int y1, int maxX, int maxY, int numTicks, int tickLength) { // some pre-calcs int width = x1-x0, height = y1-y0; FontMetrics metrics = pen.getFontMetrics(); int textHeight = metrics.getAscent(); // draw rectangle (to go around the graph) pen.setColor(Color.BLACK); pen.drawRect(x0, y0, width, height); // draw ticks & labels for (int i=0; i