|
| 1 | +package ex5.question6; |
| 2 | + |
| 3 | +import javax.swing.*; |
| 4 | +import java.awt.*; |
| 5 | + |
| 6 | +/** |
| 7 | + * Created by christopher on 1/30/14. |
| 8 | + * <p/> |
| 9 | + * Notice that our class IS a JFrame. We are creating a custom frame tailored |
| 10 | + * for function plotting, rather than reusing a basic JFrame. |
| 11 | + */ |
| 12 | +public class DrawFunction extends JFrame { |
| 13 | + |
| 14 | + public DrawFunction() throws HeadlessException { |
| 15 | + |
| 16 | + /* |
| 17 | + * We use a standard border layout, since we will only need |
| 18 | + * to consider a single component. |
| 19 | + */ |
| 20 | + setLayout(new BorderLayout()); |
| 21 | + |
| 22 | + /* |
| 23 | + * Here, we create and attach the actual "drawing surface to |
| 24 | + * the frame. The second parameter, BorderLayout.CENTER, tells |
| 25 | + * the system that the component should be placed at the center |
| 26 | + * of the frames layout. |
| 27 | + */ |
| 28 | + add(new DrawingBoard(), BorderLayout.CENTER); |
| 29 | + |
| 30 | + /* |
| 31 | + * This defines what action to take when we close the frame (by |
| 32 | + * clicking the "X" in the top-right corner, if we are on a |
| 33 | + * Windows machine, for example). In our case, we shut down the |
| 34 | + * frame when it is closes. |
| 35 | + */ |
| 36 | + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 37 | + |
| 38 | + /* |
| 39 | + * Set the title to appear in the top-bar of the frame. |
| 40 | + */ |
| 41 | + setTitle("Draw"); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * This will be our "drawing board", where we do the actual plotting. |
| 46 | + * Our board is a JPanel, which is a multi-purpose component which |
| 47 | + * can be used to hold text, graphics, or other components. |
| 48 | + */ |
| 49 | + class DrawingBoard extends JPanel { |
| 50 | + |
| 51 | + /** |
| 52 | + * The function which we will be plotting. Could easily be replaced by an |
| 53 | + * interface like we did in previous exercises. |
| 54 | + * |
| 55 | + * @param x |
| 56 | + * @return |
| 57 | + */ |
| 58 | + private double f(double x) { |
| 59 | + return x * x; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + protected void paintComponent(final Graphics g) { |
| 64 | + super.paintComponent(g); |
| 65 | + |
| 66 | + /* |
| 67 | + * First, we draw the arrows on our chart (to give it that nice, math-ish feeling). |
| 68 | + */ |
| 69 | + g.drawLine(390, 200, 370, 190); |
| 70 | + g.drawLine(390, 200, 370, 210); |
| 71 | + g.drawLine(200, 30, 190, 50); |
| 72 | + g.drawLine(200, 30, 210, 50); |
| 73 | + |
| 74 | + /* |
| 75 | + * Draw the X and Y axes |
| 76 | + */ |
| 77 | + g.drawLine(10, 200, 390, 200); |
| 78 | + g.drawLine(200, 30, 200, 390); |
| 79 | + |
| 80 | + /* |
| 81 | + * Draw the labels for the axes |
| 82 | + */ |
| 83 | + g.drawString("X", 390, 170); |
| 84 | + g.drawString("Y", 220, 40); |
| 85 | + |
| 86 | + /* |
| 87 | + * To do the actual drawing, we use a Polygon. |
| 88 | + */ |
| 89 | + Polygon polygon = new Polygon(); |
| 90 | + |
| 91 | + for (int x = -40; x <= 40; x++) { |
| 92 | + polygon.addPoint(x + 200, 200 - (int) ((0.1) * f(x))); |
| 93 | + } |
| 94 | + |
| 95 | + g.drawPolyline(polygon.xpoints, polygon.ypoints, polygon.npoints); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public static void main(String[] args) { |
| 100 | + final int FRAME_WIDTH = 400; |
| 101 | + final int FRAME_HEIGHT = 400; |
| 102 | + DrawFunction frame = new DrawFunction(); |
| 103 | + frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); |
| 104 | + frame.setVisible(true); |
| 105 | + } |
| 106 | +} |
0 commit comments