Example #1
0
  private void drawString(String str, int x, int y) {

    // text does not work correctly for very small scales.
    if (textEnabled && scale > 0.05) {
      gRef.drawString(str, x, y);
    }
  }
  public void drawFunctionToGraphic(Graphics2D g, int x, int y, int UNITYX, int UNITYY) {
    int _x, _y;

    g.setColor(Color.red);
    g.drawString("f(x) = " + F.toString(), 15, 20);

    g.setColor(Color.blue);
    for (double i = 0; ; i += EPSILON) {
      _x = (int) (i * UNITYX) + x / 2;
      _y = -(int) (F.evaluate(i) * UNITYY) + y / 2;

      g.drawLine(
          _x,
          _y,
          (int) ((i + EPSILON) * UNITYX) + x / 2,
          -(int) (F.evaluate(i + EPSILON) * UNITYY) + y / 2);

      if (_x < 0 || _x > x || _y < 0 || _y > y) break;
    }

    for (double i = 0; ; i -= EPSILON) {
      _x = (int) (i * UNITYX) + x / 2;
      _y = -(int) (F.evaluate(i) * UNITYY) + y / 2;

      g.drawLine(
          _x,
          _y,
          (int) ((i + EPSILON) * UNITYX) + x / 2,
          -(int) (F.evaluate(i + EPSILON) * UNITYY) + y / 2);

      if (_x < 0 || _x > x || _y < 0 || _y > y) break;
    }
  }
  public void paintValue(Graphics g, Rectangle box) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(
        RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    double[] values = (double[]) getValue();
    StringBuilder s = new StringBuilder();
    for (int i = 0; i < 3; i++) {
      if (values.length > i) s.append(values[i]);
      if (values.length > i + 1) s.append(", ");
    }
    if (values.length > 3) s.append("...");

    g2.setPaint(Color.white);
    g2.fill(box);
    g2.setPaint(Color.black);
    FontRenderContext context = g2.getFontRenderContext();
    Rectangle2D stringBounds = g2.getFont().getStringBounds(s.toString(), context);
    double w = stringBounds.getWidth();
    double x = box.x;
    if (w < box.width) x += (box.width - w) / 2;
    double ascent = -stringBounds.getY();
    double y = box.y + (box.height - stringBounds.getHeight()) / 2 + ascent;
    g2.drawString(s.toString(), (float) x, (float) y);
  }
 public void draw(
     Graphics2D g,
     Color stringColor,
     Color foreground,
     Color background,
     String info,
     double x,
     double y) {
   FontMetrics fm = g.getFontMetrics();
   int h = fm.getHeight();
   int w = fm.stringWidth(info);
   r1.setRect(
       x - w / 2 - in.left, y - in.top - h / 2, w + in.right + in.left, h + in.bottom + in.top);
   g.setColor(background);
   g.fill(r1);
   g.setColor(stringColor);
   g.draw(r1);
   g.setColor(foreground);
   r2.setRect(r1.getX() + 1, r1.getY() + 1, r1.getWidth() - 2, r1.getHeight() - 2);
   g.draw(r2);
   g.setColor(stringColor);
   g.drawString(
       info, (float) (r2.getX() + in.left), (float) (r2.getY() + h - (r2.getHeight() - h) / 2));
 }
  private void paintCurrentMethodState(Graphics2D g2d) {
    switch (type) {
      case 0: // bisectiei
        if (Cn.size() > 0) {
          g2d.setColor(Color.green);
          for (int pnt = 0; pnt < An.size(); pnt++) drawPoint(An.elementAt(pnt), g2d);
          g2d.setColor(Color.orange);
          for (int pnt = 0; pnt < Bn.size(); pnt++) drawPoint(Bn.elementAt(pnt), g2d);

          g2d.setColor(Color.red);
          drawPoint(Cn.lastElement(), g2d);

          g2d.setColor(Color.blue);
          g2d.drawString("Pasul " + Cn.size(), 15, 35);
          g2d.drawString(
              "[An, Bn] = [" + An.lastElement().x + ", " + Bn.lastElement().x + "]", 15, 50);
          g2d.drawString("Cn = " + Cn.lastElement().x, 15, 65);

          if (complete) {
            g2d.setColor(Color.red);
            g2d.drawString("Algoritm terminat in " + Cn.size() + " pasi.", 15, 80);
            g2d.drawString(
                "Rezultatule este " + Cn.lastElement().x + ", cu eroarea de +-" + err + ".",
                15,
                95);
          }
        }
        break;
      case 3: // newton
      case 2: // secantei
      case 1: // coardei
        g2d.setColor(Color.green);
        for (int pnt = 0; pnt < An.size() - 1; pnt++) drawPoint(An.elementAt(pnt), g2d);
        g2d.setColor(Color.red);
        drawPoint(An.lastElement(), g2d);

        g2d.setColor(Color.blue);
        g2d.drawString("Pasul " + An.size(), 15, 35);
        if (type != 3)
          g2d.drawString(
              "[x0, x1] = [" + An.elementAt(0).x + ", " + An.elementAt(1).x + "]", 15, 50);
        else g2d.drawString("x0 = " + An.elementAt(0).x, 15, 50);
        g2d.drawString("xn = " + An.lastElement().x, 15, 65);
        if (complete) {
          g2d.setColor(Color.red);
          g2d.drawString("Algoritm terminat in " + An.size() + " pasi.", 15, 80);
          g2d.drawString(
              "Rezultatule este " + An.lastElement().x + ", cu eroarea de +-" + err + ".", 15, 95);
        }
        break;
    }
  }