/** Overrides <code>Graphics.drawChars</code>. */
  public void drawChars(char data[], int offset, int length, int x, int y) {
    DebugGraphicsInfo info = info();

    Font font = graphics.getFont();

    if (debugLog()) {
      info().log(toShortString() + " Drawing chars at " + new Point(x, y));
    }

    if (isDrawingBuffer()) {
      if (debugBuffered()) {
        Graphics debugGraphics = debugGraphics();

        debugGraphics.drawChars(data, offset, length, x, y);
        debugGraphics.dispose();
      }
    } else if (debugFlash()) {
      Color oldColor = getColor();
      int i, count = (info.flashCount * 2) - 1;

      for (i = 0; i < count; i++) {
        graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);
        graphics.drawChars(data, offset, length, x, y);
        Toolkit.getDefaultToolkit().sync();
        sleep(info.flashTime);
      }
      graphics.setColor(oldColor);
    }
    graphics.drawChars(data, offset, length, x, y);
  }
Exemple #2
0
    public void centerText(String s1, String s2, Graphics g, Color c, int x, int y, int w, int h) {
      // locs[0].centerText(s1, s2, this.getGraphics(),
      // Color.white, 400,0,200,50);
      // g.setXORMode(unselected_color);
      // centerText("pic" + im, null, g, Color.black, x, y, w, h);

      Font f = g.getFont();
      FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(f);
      int ascent = fm.getAscent();
      int height = fm.getHeight();
      int width1 = 0, width2 = 0, x0 = 0, x1 = 0, y0 = 0, y1 = 0;
      width1 = fm.stringWidth(s1);
      if (s2 != null) width2 = fm.stringWidth(s2);
      x0 = x + (w - width1) / 2;
      x0 = x + (w - width2) / 2;
      if (s2 == null) y0 = y + (h - height) / 2 + ascent;
      else {
        y0 = y + (h - (int) (height * 2.2)) / 2 + ascent;
        y1 = y0 + (int) (height * 1.2);
      }
      g.setColor(c);
      g.drawString(s1, x0, y0);
      if (s2 != null) g.drawString(s2, x1, y1);
    }
 /**
  * Returns the Font used for text drawing operations.
  *
  * @see #setFont
  */
 public Font getFont() {
   return graphics.getFont();
 }
Exemple #4
0
  public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2d = (Graphics2D) g;
    RenderingHints rh = g2d.getRenderingHints();
    rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2d.setRenderingHints(rh);

    // Background.
    D = this.getSize();
    g.setColor(backgroundColor);
    g.fillRect(0, 0, D.width, D.height);
    Graphics2D g2 = (Graphics2D) g;
    g2.setStroke(lineStroke);

    // Axes, bounding box.
    g.setColor(Color.gray);
    g.drawLine(inset, D.height - inset, D.width - inset, D.height - inset);
    g.drawLine(D.width - inset, inset, D.width - inset, D.height - inset);
    g.drawLine(inset, inset, inset, D.height - inset);
    g.drawLine(inset, inset, D.width - inset, inset);

    double xDelta = (maxX - minX) / numIntervals;

    // X-ticks and labels.
    for (int i = 1; i <= numIntervals; i++) {
      double xTickd = i * xDelta;
      int xTick = (int) (xTickd / (maxX - minX) * (D.width - 2 * inset));
      g.drawLine(inset + xTick, D.height - inset - 5, inset + xTick, D.height - inset + 5);
      double x = minX + i * xDelta;
      g.drawString(df.format(x), xTick + inset - 5, D.height - inset + 20);
    }

    // Y-ticks
    double yDelta = (maxY - minY) / numIntervals;
    for (int i = 0; i < numIntervals; i++) {
      int yTick = (i + 1) * (int) ((D.height - 2 * inset) / (double) numIntervals);
      g.drawLine(inset - 5, D.height - yTick - inset, inset + 5, D.height - yTick - inset);
      double y = minY + (i + 1) * yDelta;
      g.drawString(df.format(y), 1, D.height - yTick - inset);
    }

    // Zoom+move
    Font savedFont = g.getFont();
    g.setFont(plusFont);
    g.drawString("+", D.width - 25, 20);
    g.setFont(minusFont);
    g.drawString("-", D.width - 25, 50);
    drawArrow(g2d, D.width - 70, 20, D.width - 70, 0, 1.0f, lineStroke); // Up
    drawArrow(g2d, D.width - 70, 30, D.width - 70, 50, 1.0f, lineStroke); // Down
    drawArrow(g2d, D.width - 65, 25, D.width - 45, 25, 1.0f, lineStroke); // Right
    drawArrow(g2d, D.width - 75, 25, D.width - 95, 25, 1.0f, lineStroke); // Left
    g.setFont(savedFont);

    // See if standard axes are in the middle.
    g.setColor(Color.gray);
    if ((minX < 0) && (maxX > 0) && (drawMiddleAxes)) {
      // Draw y-axis
      int x = (int) ((0 - minX) / (maxX - minX) * (D.width - 2 * inset));
      g.drawLine(inset + x, D.height - inset, inset + x, inset);
    }
    if ((minY < 0) && (maxY > 0) && (drawMiddleAxes)) {
      // Draw x-axis
      int y = (int) ((0 - minY) / (maxY - minY) * (D.height - 2.0 * inset));
      g.drawLine(inset, D.height - y - inset, D.width - inset, D.height - y - inset);
    }

    // Draw the objects.
    drawObjects(g, points, lines, ovals, rectangles, images, labels, eqnLines);
    if (animationMode) {
      drawObjects(g, animPoints, animLines, animOvals, animRectangles, null, labels, eqnLines);
      // No images in animation mode.
    }

    drawScribbles(g);
  }
Exemple #5
0
  static void paintShadowTitle(
      Graphics g,
      String title,
      int x,
      int y,
      Color frente,
      Color shadow,
      int desp,
      int tipo,
      int orientation) {

    // Si hay que rotar la fuente, se rota
    Font f = g.getFont();
    if (orientation == SwingConstants.VERTICAL) {
      AffineTransform rotate = AffineTransform.getRotateInstance(Math.PI / 2);
      f = f.deriveFont(rotate);
    }

    // Si hay que pintar sombra, se hacen un monton de cosas
    if (shadow != null) {
      int matrix = (tipo == THIN ? MATRIX_THIN : MATRIX_FAT);

      Rectangle2D rect = g.getFontMetrics().getStringBounds(title, g);

      int w, h;
      if (orientation == SwingConstants.HORIZONTAL) {
        w = (int) rect.getWidth() + 6 * matrix; // Hay que dejar espacio para las sombras y el borde
        h = (int) rect.getHeight() + 6 * matrix; // que ConvolveOp ignora por el EDGE_NO_OP
      } else {
        h = (int) rect.getWidth() + 6 * matrix; // Hay que dejar espacio para las sombras y el borde
        w = (int) rect.getHeight() + 6 * matrix; // que ConvolveOp ignora por el EDGE_NO_OP
      }

      // La sombra del titulo
      BufferedImage iTitulo = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
      BufferedImage iSombra = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

      Graphics2D g2 = iTitulo.createGraphics();
      g2.setRenderingHint(
          RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

      g2.setFont(f);
      g2.setColor(shadow);
      g2.drawString(title, 3 * matrix, 3 * matrix); // La pintamos en el centro

      ConvolveOp cop =
          new ConvolveOp((tipo == THIN ? kernelThin : kernelFat), ConvolveOp.EDGE_NO_OP, null);
      cop.filter(iTitulo, iSombra); // A ditorsionar

      // Por fin, pintamos el jodio titulo
      g.drawImage(
          iSombra,
          x - 3 * matrix + desp, // Lo llevamos a la posicion original y le sumamos 1
          y - 3 * matrix + desp, // para que la sombra quede pelin desplazada
          null);
    }

    // Si hay que pintar el frente, se pinta
    if (frente != null) {
      g.setFont(f);
      g.setColor(frente);
      g.drawString(title, x, y);
    }
  }
Exemple #6
0
  /**
   * parse the text. When the text is parsed the width, height, leading are all calculated. The text
   * will only be truly parsed if the graphics context has changed or the text has changed or the
   * font has changed. Otherwise nothing is done when this method is called.
   *
   * @param g Graphics context.
   */
  public void parseText(Graphics g) {
    FontMetrics fm;
    TextState current = new TextState();
    char ch;
    Stack state = new Stack();
    int w = 0;

    if (lg != g) parse = true;
    lg = g;

    if (!parse) return;

    parse = false;
    width = 0;
    leading = 0;
    ascent = 0;
    descent = 0;
    height = 0;
    maxAscent = 0;
    maxDescent = 0;

    if (text == null || g == null) return;

    list.removeAllElements();

    if (font == null) current.f = g.getFont();
    else current.f = font;

    state.push(current);
    list.addElement(current);

    fm = g.getFontMetrics(current.f);

    for (int i = 0; i < text.length(); i++) {
      ch = text.charAt(i);

      switch (ch) {
        case '$':
          i++;
          if (i < text.length()) current.s.append(text.charAt(i));
          break;
          /*
           **                    Push the current state onto the state stack
           **                    and start a new storage string
           */
        case '{':
          w = current.getWidth(g);
          if (!current.isEmpty()) {
            current = current.copyState();
            list.addElement(current);
          }

          state.push(current);
          current.x += w;
          break;
          /*
           **                    Pop the state off the state stack and set the current
           **                    state to the top of the state stack
           */
        case '}':
          w = current.x + current.getWidth(g);
          state.pop();
          current = ((TextState) state.peek()).copyState();
          list.addElement(current);
          current.x = w;
          break;
        case '^':
          w = current.getWidth(g);
          if (!current.isEmpty()) {
            current = current.copyState();
            list.addElement(current);
          }
          current.f = getScriptFont(current.f);
          current.x += w;
          current.y -= (int) ((double) (current.getAscent(g)) * sup_offset + 0.5);
          break;
        case '_':
          w = current.getWidth(g);
          if (!current.isEmpty()) {
            current = current.copyState();
            list.addElement(current);
          }
          current.f = getScriptFont(current.f);
          current.x += w;
          current.y += (int) ((double) (current.getDescent(g)) * sub_offset + 0.5);
          break;

        default:
          current.s.append(ch);
          break;
      }
    }

    for (int i = 0; i < list.size(); i++) {
      current = ((TextState) (list.elementAt(i)));

      if (!current.isEmpty()) {
        width += current.getWidth(g);
        ascent = Math.max(ascent, Math.abs(current.y) + current.getAscent(g));
        descent = Math.max(descent, Math.abs(current.y) + current.getDescent(g));
        leading = Math.max(leading, current.getLeading(g));

        maxDescent = Math.max(maxDescent, Math.abs(current.y) + current.getMaxDescent(g));
        maxAscent = Math.max(maxAscent, Math.abs(current.y) + current.getMaxAscent(g));
      }
    }

    height = ascent + descent + leading;

    return;
  }
Exemple #7
0
  private void drawLayout(Graphics g) {
    g.setColor(Color.blue);
    g.drawRect(10, 10, width - 20, height - 20);

    g.setColor(Color.red.brighter());
    Iterator f = players.iterator();
    int lifeDisplayPosition = 0;
    while (f.hasNext()) {
      Player h = (Player) f.next();
      if (h.isActive()) {
        lifeDisplayPosition++;
        String lifeInformation = "Player " + lifeDisplayPosition + ": ";
        for (int i = 0; i < h.getLives(); i++) {
          lifeInformation += "\u2606";
        }
        g.drawString(lifeInformation, 20, lifeDisplayPosition * 40);
      }
    }

    borders[2] = width - 20;
    borders[3] = height - 20;

    Iterator i = players.iterator();
    Player p;
    while (i.hasNext()) {
      p = (Player) i.next();
      if (p.isActive()
          && !countdownF
          && (p.getX() < borders[0]
              || p.getY() < borders[1]
              || p.getX() > borders[2]
              || p.getY() > borders[3])
          && (!p.getImmunity())) {
        p.decLives(width / 2, height / 2);
        p.setImmunity(true);
      }
      if (p.getLives() <= 0) {
        if (p instanceof MouseControlledPlayer) {
          MouseControlledPlayer m = (MouseControlledPlayer) p;
          removeMouseListener(m);
          removeMouseMotionListener(m);
        } else if (p instanceof KeyboardControlledPlayer) {
          KeyboardFocusManager.getCurrentKeyboardFocusManager()
              .removeKeyEventDispatcher((KeyboardControlledPlayer) p);
        }
        i.remove();
        for (int h = 0; h < 4; h++) {
          if (deathLocation[h] == -1) {
            deathLocation[h] = p.getX();
            deathLocation[h + 1] = p.getY();
            break;
          }
        }
      }
    }

    if ((deathLocation[0] != -1) && (deathLocation[1] != -1)) {
      g.drawLine(
          deathLocation[0] - 15,
          deathLocation[1] - 15,
          deathLocation[0] + 15,
          deathLocation[1] + 15);
      g.drawLine(
          deathLocation[0] + 15,
          deathLocation[1] - 15,
          deathLocation[0] - 15,
          deathLocation[1] + 15);
    }
    if ((deathLocation[2] != -1) && (deathLocation[3] != -1)) {
      g.drawLine(
          deathLocation[2] - 15,
          deathLocation[3] - 15,
          deathLocation[2] + 15,
          deathLocation[3] + 15);
      g.drawLine(
          deathLocation[2] + 15,
          deathLocation[3] - 15,
          deathLocation[2] - 15,
          deathLocation[3] + 15);
    }

    if (!onePlayerAlive) {
      Font old = g.getFont();
      g.setFont(new Font("monospaced", Font.BOLD, 20));
      g.setColor(Color.red.darker());
      g.drawString("GAME OVER", width / 2 - 40, height / 2);
      g.setFont(old);
      g.setColor(Color.WHITE);
      g.drawString("Score", width - 60, 25);
      g.drawString(String.valueOf(score), width - 60, 40);
    }
  }