Advertisement
Guest User

Untitled

a guest
Jun 30th, 2015
1,178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.21 KB | None | 0 0
  1. package realApps;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.util.ArrayList;
  6. import javax.swing.*;
  7.  
  8. public class GraphDrawing implements ActionListener {
  9.  
  10.     GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); // Takes in display size.
  11.     final int unit = 20, wWidth = 1000, wHeight = 1060, sWidth = gd.getDisplayMode().getWidth(), sHeight = gd.getDisplayMode().getHeight();
  12.     JFrame frame;
  13.     JTextField fa, fb, fc; // Text fields where to enter the coefficients.
  14.     ArrayList<Line> ay = new ArrayList<Line>(); // ArrayList to hold the lines.
  15.     JPanel lines, cofs; // Panel that lists drawn lines and their respective equation & panel with coefficients.
  16.     MyDrawPanel panel;
  17.     JSplitPane splitPane;
  18.     Line line;
  19.     JLabel vertexLabel;
  20.  
  21.     public static void main(String[] args) {
  22.  
  23.         new GraphDrawing();
  24.     }
  25.  
  26.     public GraphDrawing() {
  27.  
  28.         frame = new JFrame("Graph tool");
  29.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  30.         fa = new JTextField(7);
  31.         fb = new JTextField(7);
  32.         fc = new JTextField(7);
  33.         JLabel la = new JLabel("a: ");
  34.         JLabel lb = new JLabel("b: ");
  35.         JLabel lc = new JLabel("c: ");
  36.  
  37.         JButton drawLine = new JButton("Draw line!");
  38.         drawLine.addActionListener(this);
  39.  
  40.         cofs = new JPanel(); // Panel where the coefficients can be entered.
  41.         cofs.setBackground(Color.lightGray);
  42.  
  43.         cofs.add(la);
  44.         cofs.add(fa);
  45.         cofs.add(lb);
  46.         cofs.add(fb);
  47.         cofs.add(lc);
  48.         cofs.add(fc);
  49.         cofs.add(drawLine);
  50.  
  51.         lines = new JPanel();
  52.         lines.setBackground(Color.lightGray);
  53.         lines.setLayout(new BoxLayout(lines, BoxLayout.Y_AXIS)); // Sets layout for the side-bar, giving a new line to each equation.
  54.         lines.setPreferredSize(new Dimension(20, frame.getHeight()));
  55.  
  56.         panel = new MyDrawPanel();
  57.         splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, lines, panel); // Used for resizing of side-bar.
  58.  
  59.         frame.getContentPane().add(BorderLayout.CENTER, splitPane);
  60.         frame.getContentPane().add(BorderLayout.SOUTH, cofs);
  61.  
  62.         frame.setSize(wWidth, wHeight);
  63.         frame.setVisible(true);
  64.     }
  65.  
  66.     String toEquation() { // Returns the coefficients in the format y = ax^2 + bx + c as a String.
  67.  
  68.         double a = line.getA(), b = line.getB(), c = line.getC(); // Local variables to store the coefficients of the line.
  69.  
  70.         String as = Double.toString(a); // Converts values to strings.
  71.         String bs = Double.toString(b);
  72.         String cs = Double.toString(c);
  73.  
  74.         int ai = as.length(), bi = bs.length(), ci = cs.length(); // Lengths of values.
  75.  
  76.         if(Character.toString(as.charAt(ai - 1)).equals("0")) { // If a number is an integer, remove the .0 bit from the end.
  77.  
  78.             as = as.substring(0, ai - 2);
  79.         }
  80.         if(Character.toString(bs.charAt(bi - 1)).equals("0")) {
  81.  
  82.             bs = bs.substring(0, bi - 2);
  83.         }
  84.         if(Character.toString(cs.charAt(ci - 1)).equals("0")) {
  85.  
  86.             cs = cs.substring(0, ci - 2);
  87.         }
  88.  
  89.         String af = a == 1 ? "x\u00B2 " : a == -1 ? "-x\u00B2 " : a == 0 ? "" : as + "x\u00B2 "; // Code for showing a (u00B2 is Unicode for superscript 2).
  90.         String bf = b == -1 ? a == 0 ? "-x " : "- x " : a == 0 && b != 0 && b != 1 ? bs + "x " : b < 0 ? "- " + bs.substring(1) + "x " : b == 1 ? a == 0 ? "x " : "+ x " : a == 0 ? b == 0 ? "" : bs + "x " : b == 0 ? "" : "+ " + bs + "x "; // Forgive me lord for I have sinned.
  91.         String cf = a == 0 && b == 0 ? cs : c < 0 ? "- " + cs.substring(1) : c == 0 ? "" : "+ " + cs; // Code for y-intercept string.
  92.  
  93.         return "y = " + af + bf + cf;
  94.     }
  95.  
  96.     void listEquation(String s) { // Adds the equation to the side-bar.
  97.  
  98.         JLabel label = new JLabel(s);
  99.         JPanel lineButton = new JPanel();
  100.  
  101.         lineButton.setMaximumSize(new Dimension(frame.getWidth(), 30)); // Makes the different panels stack over each other.
  102.         lineButton.add(BorderLayout.EAST, label);
  103.         lines.add(BorderLayout.SOUTH, lineButton);
  104.         splitPane.resetToPreferredSizes();
  105.     }
  106.  
  107.     public void actionPerformed(ActionEvent ae) {
  108.         try { // Take in a, b & c from text boxes.
  109.             if(!(fa.getText().equals("") && fb.getText().equals("") && fc.getText().equals(""))) { // Don't draw a line if all 3 boxes are empty.
  110.  
  111.                 String[] sabc = { fa.getText(), fb.getText(), fc.getText() };
  112.                 double[] abc = new double[3];
  113.  
  114.                 for(int i = 0; i < sabc.length; i++) {
  115.  
  116.                     abc[i] = (sabc[i].equals("") ? 0 : Double.parseDouble(sabc[i]));
  117.                 }
  118.  
  119.                 line = new Line(abc[0], abc[1], abc[2], unit, sHeight, sWidth);
  120.                 listEquation(toEquation()); // Add equation to the side-bar.
  121.                 ay.add(line); // Add line to the array.
  122.             }
  123.         } catch (Exception ex) {} // Does nothing if a, b or c aren't numbers.
  124.  
  125.         frame.repaint();
  126.     }
  127.  
  128.     class MyDrawPanel extends JPanel { // Draws the graph.
  129.  
  130.         private static final long serialVersionUID = 6193278998208292577L;
  131.  
  132.         public void paintComponent(Graphics g) {
  133.  
  134.             Graphics2D g2d = (Graphics2D) g;
  135.  
  136.             for(int i = 0; i <= sWidth; i += unit) { // Draws grid.
  137.  
  138.                 g2d.drawLine(i, 0, i, sHeight);
  139.                 g2d.drawLine(0, i, sWidth, i);
  140.             }
  141.  
  142.             int h = ((int) ((frame.getHeight() - cofs.getHeight()) / unit / 2)) * unit; // Gives values for height and width based on the unit.
  143.             int w = ((int) ((frame.getWidth() - lines.getWidth()) / unit / 2)) * unit;
  144.             g2d.fillRect(0, h - 1, w * sWidth, 3); // Draws x-axis.
  145.             g2d.fillRect(w - 1, 0, 3, h * sHeight); // Draws y-axis.
  146.  
  147.             g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // Makes the curve nice and smooth.
  148.  
  149.             for(int i = 0, ays = ay.size(); i < ays; i++) {
  150.  
  151.                 Line l = ay.get(i);
  152.                 g2d.setColor(l.getColor());
  153.                 g2d.setStroke(new BasicStroke(l.getThickness())); // Sets thickness of parabola.
  154.  
  155.                 for(int j = 1, size = l.getCoords().size(); j < size; j++) { // Draws a line from point to point.
  156.  
  157.                     g2d.drawLine(w + (int) Math.round(unit * l.getCoords().get(j - 1)[0]), h - (int) Math.round(unit * l.getCoords().get(j - 1)[1]), w + (int) Math.round(unit * l.getCoords().get(j)[0]), h - (int) Math.round(unit * l.getCoords().get(j)[1])); // Draws line from dot to dot.
  158.                 }
  159.             }
  160.         }
  161.     }
  162. }
  163.  
  164.  
  165.  
  166. package realApps;
  167.  
  168. import java.awt.Color;
  169. import java.util.ArrayList;
  170.  
  171. final public class Line {
  172.  
  173.     private double a, b, c;
  174.     private Color lineColor;
  175.     private int thickness, unit, sHeight, sWidth;
  176.     private double vX, vY;
  177.     private ArrayList<double[]> cords = new ArrayList<double[]>();
  178.  
  179.     public Line(double tA, double tB, double tC, Color color, int thick, int nUnit, int height, int width) {
  180.  
  181.         a = tA;
  182.         b = tB;
  183.         c = tC;
  184.         lineColor = color;
  185.         thickness = thick;
  186.         unit = nUnit;
  187.         sHeight = height;
  188.         sWidth = width;
  189.         setVertex();
  190.         setCoords();
  191.     }
  192.  
  193.     public Line(double tA, double tB, double tC, int nUnit, int height, int width) {
  194.  
  195.         a = tA;
  196.         b = tB;
  197.         c = tC;
  198.         lineColor = Color.blue;
  199.         thickness = 2;
  200.         unit = nUnit;
  201.         sHeight = height;
  202.         sWidth = width;
  203.         setVertex();
  204.         setCoords();
  205.     }
  206.  
  207.     double getA() {
  208.  
  209.         return a;
  210.     }
  211.     void setA(double tA) {
  212.  
  213.         a = tA;
  214.     }
  215.  
  216.     double getB() {
  217.  
  218.         return b;
  219.     }
  220.     void setB(double tB) {
  221.  
  222.         b = tB;
  223.     }
  224.  
  225.     double getC() {
  226.  
  227.         return c;
  228.     }
  229.     void setC(double tC) {
  230.  
  231.         c = tC;
  232.     }
  233.  
  234.     Color getColor() {
  235.  
  236.         return lineColor;
  237.     }
  238.     void setColor(Color cl) {
  239.  
  240.         lineColor = cl;
  241.     }
  242.  
  243.     int getThickness() {
  244.  
  245.         return thickness;
  246.     }
  247.     void setThickness(int nThickness) {
  248.  
  249.         thickness = nThickness;
  250.     }
  251.  
  252.     String getVertexAsString() {
  253.  
  254.         return "(" + vX + ", " + vY + ")";
  255.     }
  256.  
  257.     double[] getVertex() {
  258.  
  259.         return new double[] {vX, vY};
  260.     }
  261.  
  262.     private void setVertex() {
  263.  
  264.         vX = -(b/a/2);
  265.         vY = c - a * Math.pow((b/a/2), 2);
  266.     }
  267.  
  268.     void setCoords() {
  269.  
  270.         double px, nx, y;
  271.  
  272.         if(!(a == 0 && b == 0)) {
  273.             for(int i = 0; true; i++) {
  274.  
  275.                 y = a * i * i + b * i + c;
  276.                 if(y > sHeight / unit) {
  277.                     px = i;
  278.                     break;
  279.                 }
  280.             }
  281.  
  282.             for(int i = 0; true; i--) {
  283.  
  284.                 y = a * i * i + b * i + c;
  285.                 if(y > sHeight / unit) {
  286.                     nx = i;
  287.                     break;
  288.                 }
  289.             }
  290.  
  291.             for(double i = nx; i < 0; i += 0.1) {
  292.  
  293.                 y = a * i * i + b * i + c;
  294.                 cords.add(new double[] {i, y});
  295.  
  296.             }
  297.  
  298.             for(double i = 0; i < px; i += 0.1) {
  299.  
  300.                 y = a * i * i + b * i + c;
  301.                 cords.add(new double[] {i, y});
  302.             }
  303.         } else {
  304.            
  305.             cords.add(new double[] {-sWidth, c});
  306.             cords.add(new double[] {sWidth, c});
  307.         }
  308.     }
  309.  
  310.     ArrayList<double[]> getCoords() {
  311.  
  312.         return cords;
  313.     }
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement