示例#1
0
  public void paint(Graphics gOld) {
    if (image == null || xsize != getSize().width || ysize != getSize().height) {
      xsize = getSize().width;
      ysize = getSize().height;
      image = createImage(xsize, ysize);
      g = (Graphics2D) image.getGraphics();
      g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    }
    // fill background
    g.setColor(Color.cyan);
    g.fillRect(0, 0, xsize, ysize);

    int x[] = {getX(0), getX(getWidth2()), getX(getWidth2()), getX(0), getX(0)};
    int y[] = {getY(0), getY(0), getY(getHeight2()), getY(getHeight2()), getY(0)};
    // fill border
    g.setColor(Color.black);
    g.fillPolygon(x, y, 4);
    // draw border
    g.setColor(Color.red);
    g.drawPolyline(x, y, 5);
    if (animateFirstTime) {
      gOld.drawImage(image, 0, 0, null);
      return;
    }
    if (gameOver) return;
    g.drawImage(outerSpaceImage, getX(0), getY(0), getWidth2(), getHeight2(), this);
    for (int index = 0; index < missile.length; index++) {
      if (missile[index].active) {
        g.setColor(Color.red);
        drawCircle(getX(missile[index].xPos), getYNormal(missile[index].yPos), 90, .3, 1.5);
      }
    }
    if (rocketRight) {
      drawRocket(rocketImage, getX(rocketXPos), getYNormal(rocketYPos), 0.0, 2.0, 2.0);
    } else {
      drawRocket(rocketImage, getX(rocketXPos), getYNormal(rocketYPos), 0.0, -2.0, 2.0);
    }
    for (int index = 0; index < numStars; index++) {
      g.setColor(Color.yellow);
      if (starActive[index])
        drawCircle(getX(starXPos[index]), getYNormal(starYPos[index]), 0, 1.5, 1.5);
    }
    g.setColor(Color.magenta);
    g.setFont(new Font("Impact", Font.BOLD, 15));
    g.drawString("Score: " + score, 10, 45);
    g.setColor(Color.magenta);
    g.setFont(new Font("Impact", Font.BOLD, 15));
    g.drawString("HighScore: " + highScore, 300, 45);
    g.setColor(Color.magenta);
    g.setFont(new Font("Impact", Font.BOLD, 15));
    g.drawString("Lives: " + rocketLife, 150, 45);
    if (rocketLife == 0) {
      g.setColor(Color.red);
      g.setFont(new Font("Impact", Font.BOLD, 60));
      g.drawString("GAME OVER", getX(getWidth2() / 6), getYNormal(getHeight2() / 2));
    }
    gOld.drawImage(image, 0, 0, null);
  }
 /* (non-Javadoc)
  * @see java.awt.print.Printable#print(java.awt.Graphics, java.awt.print.PageFormat, int)
  */
 public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
   int ret = PAGE_EXISTS;
   String line = null;
   try {
     if (fph.knownPage(page)) {
       in.seek(fph.getFileOffset(page));
       line = in.readLine();
     } else {
       long offset = in.getFilePointer();
       line = in.readLine();
       if (line == null) {
         ret = NO_SUCH_PAGE;
       } else {
         fph.createPage(page);
         fph.setFileOffset(page, offset);
       }
     }
     if (ret == PAGE_EXISTS) {
       // Seite ausgeben, Grafikkontext vorbereiten
       Graphics2D g2 = (Graphics2D) g;
       g2.scale(1.0 / RESMUL, 1.0 / RESMUL);
       int ypos = (int) pf.getImageableY() * RESMUL;
       int xpos = ((int) pf.getImageableX() + 2) * RESMUL;
       int yd = 12 * RESMUL;
       int ymax = ypos + (int) pf.getImageableHeight() * RESMUL - yd;
       // Seitentitel ausgeben
       ypos += yd;
       g2.setColor(Color.black);
       g2.setFont(new Font("Monospaced", Font.BOLD, 10 * RESMUL));
       g.drawString(fbname + " Seite " + (page + 1), xpos, ypos);
       g.drawLine(
           xpos,
           ypos + 6 * RESMUL,
           xpos + (int) pf.getImageableWidth() * RESMUL,
           ypos + 6 * RESMUL);
       ypos += 2 * yd;
       // Zeilen ausgeben
       g2.setColor(new Color(0, 0, 127));
       g2.setFont(new Font("Monospaced", Font.PLAIN, 10 * RESMUL));
       while (line != null) {
         g.drawString(line, xpos, ypos);
         ypos += yd;
         if (ypos >= ymax) {
           break;
         }
         line = in.readLine();
       }
     }
   } catch (IOException e) {
     throw new PrinterException(e.toString());
   }
   return ret;
 }
示例#3
0
 public static void saveImageResultsToPng(String prefix, ImageSearchHits hits, String queryImage)
     throws IOException {
   LinkedList<BufferedImage> results = new LinkedList<BufferedImage>();
   int width = 0;
   for (int i = 0; i < hits.length(); i++) {
     // hits.score(i)
     // hits.doc(i).get("descriptorImageIdentifier")
     BufferedImage tmp =
         ImageIO.read(new FileInputStream(hits.doc(i).get("descriptorImageIdentifier")));
     //            if (tmp.getHeight() > 200) {
     double factor = 200d / ((double) tmp.getHeight());
     tmp = ImageUtils.scaleImage(tmp, (int) (tmp.getWidth() * factor), 200);
     //            }
     width += tmp.getWidth() + 5;
     results.add(tmp);
   }
   BufferedImage result = new BufferedImage(width, 220, BufferedImage.TYPE_INT_RGB);
   Graphics2D g2 = (Graphics2D) result.getGraphics();
   g2.setColor(Color.white);
   g2.setBackground(Color.white);
   g2.clearRect(0, 0, result.getWidth(), result.getHeight());
   g2.setColor(Color.black);
   g2.setFont(Font.decode("\"Arial\", Font.BOLD, 12"));
   int offset = 0;
   int count = 0;
   for (Iterator<BufferedImage> iterator = results.iterator(); iterator.hasNext(); ) {
     BufferedImage next = iterator.next();
     g2.drawImage(next, offset, 20, null);
     g2.drawString(hits.score(count) + "", offset + 5, 12);
     offset += next.getWidth() + 5;
     count++;
   }
   ImageIO.write(
       result, "PNG", new File(prefix + "_" + (System.currentTimeMillis() / 1000) + ".png"));
 }
示例#4
0
  public void paint(Graphics g) {
    gRef = (Graphics2D) g;

    // change size of font
    gRef.setFont(gRef.getFont().deriveFont(9.0f));

    fmRef = g.getFontMetrics();

    // Clear background

    if (Preferences.monochrome) {
      gRef.setColor(Preferences.whiteColor);
    } else {
      gRef.setColor(Preferences.backgroundColor);
    }
    gRef.fillRect(0, 0, getWidth(), getHeight());

    // set colour to correct drawing colour
    if (Preferences.monochrome) {
      gRef.setColor(Preferences.blackColor);
    } else {
      gRef.setColor(Preferences.penColor);
    }

    gRef.translate(0, margin);

    // Call c code to draw tree
    gRef.scale(scale, scale);
    nativeDrawTree();
  }
示例#5
0
  @Override
  public void paintComponent(Graphics window) {
    super.paintComponent(window);

    Image bufferedImage = createImage(getWidth(), getHeight());
    Graphics2D g2d = (Graphics2D) bufferedImage.getGraphics();

    g2d.setPaint(gradient1);
    g2d.fillRect(0, 0, 300, 1000);

    if (showInstructions == true) {
      g2d.setColor(Color.YELLOW);
      g2d.setFont(font1);
      g2d.drawString("Instructions:", 10, 150);
      g2d.setFont(font3);
      g2d.drawString("To Play, Click New Game", 10, 200);
      g2d.drawString("Each game costs $1", 10, 230);
      g2d.drawString("Hit adds a card to your hand", 10, 260);
      g2d.drawString("Stand stops the game and", 10, 290);
      g2d.drawString("calculates each persons score", 10, 320);
      g2d.drawString("To play dealer, click Play Dealer", 10, 350);
      g2d.drawString("Enter your desired wager", 10, 380);
      g2d.drawString("Every time you score 20 or 21", 10, 470);
      g2d.drawString("you get one arcade token", 10, 500);
      g2d.drawString("Arcade Tokens can be redeemed", 10, 530);
      g2d.drawString("By closing instructions and clicking", 10, 560);
      g2d.drawString("Play Arcade Game", 10, 590);
    } else {
      g2d.setColor(Color.YELLOW);
      g2d.setFont(font1);
      g2d.drawString("Arcade Game:", 10, 250);
      g2d.setFont(font3);
      g2d.drawString("Select the game you would like", 10, 320);
      g2d.drawString("Click Play to Play.", 10, 350);
      g2d.drawString("Hold and Drag the Mouse", 10, 380);
      g2d.drawString("to move in Cube Runner", 10, 410);
      g2d.drawString("Press the mouse to sprint", 10, 440);
      g2d.drawString("in Spiral Game, and click", 10, 500);
      g2d.drawString("to Jump", 10, 530);
      g2d.drawString("Each game costs one token", 10, 560);
      g2d.drawString("", 10, 590);
    }
    g2d.setColor(Color.YELLOW);
    g2d.setFont(font1);
    g2d.drawString("Tokens: " + tempObject.tokens, 10, 700);
    window.drawImage(bufferedImage, 0, 0, this);
  }
示例#6
0
 /**
  * Write the given text string in the current font, left-aligned at (x, y).
  *
  * @param x the x-coordinate of the text
  * @param y the y-coordinate of the text
  * @param s the text
  */
 public static void textLeft(double x, double y, String s) {
   offscreen.setFont(font);
   FontMetrics metrics = offscreen.getFontMetrics();
   double xs = scaleX(x);
   double ys = scaleY(y);
   int hs = metrics.getDescent();
   offscreen.drawString(s, (float) (xs), (float) (ys + hs));
   draw();
 }
 public void draw(Graphics2D g) {
   g.setColor(fillColor);
   g.fill(gp);
   g.setColor(Color.black);
   g.draw(gp);
   for (int i = 0; i < points.size(); i++) {
     GlyphPoint p = points.get(i);
     g.setColor(Color.red);
     g.draw(p.gp);
     g.setColor(Color.blue);
     g.setFont(gfont);
     g.drawString(String.valueOf(i), p.x + 3, p.y + 3);
   }
   g.setColor(Color.black);
   //	    System.out.println("Advance: "+advance);
   g.draw(advp);
   if (name != null) {
     g.setFont(gfont);
     g.drawString(name, 0, -40);
   }
 }
示例#8
0
  /**
   * Method to draw a message as a string on the buffered image
   *
   * @param message the message to draw on the buffered image
   * @param xPos the x coordinate of the leftmost point of the string
   * @param yPos the y coordinate of the bottom of the string
   */
  public void addMessage(String message, int xPos, int yPos) {
    // get a graphics context to use to draw on the buffered image
    Graphics2D graphics2d = bufferedImage.createGraphics();

    // set the color to white
    graphics2d.setPaint(Color.white);

    // set the font to Helvetica bold style and size 16
    graphics2d.setFont(new Font("Helvetica", Font.BOLD, 16));

    // draw the message
    graphics2d.drawString(message, xPos, yPos);
  }
示例#9
0
  @Override
  public void draw(Graphics2D g) {
    g.setColor(Color.lightGray);
    g.fillRect(0, 0, game.getScreenSize().width, game.getScreenSize().height);
    drawSnake(g);
    drawApple(g);
    g.setFont(new Font("Default", Font.BOLD, 12));

    String message = "Press <F5> to save, <F6> to load.";
    Rectangle2D messageBounds =
        g.getFontMetrics()
            .getStringBounds(
                message,
                g); // g.getFontMetrics().getStringBounds - ��������� �������� ������ ��������
                    // ������ � ��������
    int messageWidth = (int) (messageBounds.getWidth());
    int messageHeight = (int) (messageBounds.getHeight());

    g.drawString(message, 15, 10);

    if (paused) {
      g.setFont(new Font("Default", Font.BOLD, 60));
      g.setColor(Color.white);
      message = "Pause";
      messageBounds =
          g.getFontMetrics()
              .getStringBounds(
                  message,
                  g); // g.getFontMetrics().getStringBounds - ��������� �������� ������ ��������
                      // ������ � ��������
      messageWidth = (int) (messageBounds.getWidth());
      messageHeight = (int) (messageBounds.getHeight());
      g.drawString(
          message,
          game.getScreenSize().width / 2 - messageWidth / 2,
          game.getScreenSize().height / 2 - messageHeight / 2);
    }
  }
示例#10
0
 private synchronized void doBuffer(Graphics2D g2, boolean opq, Rectangle rt) {
   origTransform = g2.getTransform();
   if (opq && rt != null) g2.clearRect(rt.x, rt.y, rt.width, rt.height);
   g2.setPaint(origPaint);
   g2.setFont(origFont);
   g2.setStroke(origStroke);
   if (inBuffer) { // System.out.println("upps");
     for (int i = 0; i < a1x.size(); i++) doPaint(g2, a1x.get(i), a2x.get(i));
     origTransform = null;
     return;
   }
   for (int i = 0; i < a1.size(); i++) doPaint(g2, a1.get(i), a2.get(i));
   origTransform = null;
 }
示例#11
0
 protected float drawBoxedString(Graphics2D g2, String s, Color c1, Color c2, double x) {
   // Calculate the width of the string.
   FontRenderContext frc = g2.getFontRenderContext();
   TextLayout subLayout = new TextLayout(s, mFont, frc);
   float advance = subLayout.getAdvance();
   // Fill the background rectangle with a gradient.
   GradientPaint gradient = new GradientPaint((float) x, 0, c1, (float) (x + advance), 0, c2);
   g2.setPaint(gradient);
   Rectangle2D bounds = mLayout.getBounds();
   Rectangle2D back = new Rectangle2D.Double(x, 0, advance, bounds.getHeight());
   g2.fill(back);
   // Draw the string over the gradient rectangle.
   g2.setPaint(Color.white);
   g2.setFont(mFont);
   g2.drawString(s, (float) x, (float) -bounds.getY());
   return advance;
 }
示例#12
0
  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setPaint(Color.black);
    g2d.setStroke(new BasicStroke());
    g2d.setFont(new Font("Century Schoolbook", Font.PLAIN, 12));
    if (d.isInitialized() && display_plot) {
      d.refreshData();
      Float xLower = d.getXLower();
      Float xUpper = d.getXUpper();
      Float xInterval = d.getXInterval();
      Float yLower = d.getYLower();
      Float yUpper = d.getYUpper();
      Float yInterval = d.getYInterval();
      Float dx = xUpper - xLower;
      Float dy = yUpper - yLower;

      drawCenteredString(g2d, d.getTitle(), 250, 25, (float) 0.);
      drawCenteredString(g2d, d.getXTitle(), 250, 475, (float) 0.);
      drawCenteredString(g2d, d.getYTitle(), 25, 250, (float) -Math.PI / 2);
      drawCenteredString(g2d, xLower.toString(), 50, 475, (float) 0);
      drawCenteredString(g2d, xUpper.toString(), 450, 475, (float) 0);
      drawCenteredString(g2d, yLower.toString(), 25, 450, (float) 0);
      drawCenteredString(g2d, yUpper.toString(), 25, 50, (float) 0);

      g2d.setPaint(Color.gray);
      for (Float x = new Float(50); x <= 450; x += 400 * xInterval / dx)
        g2d.draw(new Line2D.Float(x, 450, x, 50));
      for (Float y = new Float(50); y <= 450; y += 400 * yInterval / dy)
        g2d.draw(new Line2D.Float(45, y, 450, y));

      g2d.setPaint(Color.red);
      Float diam = new Float(8);
      int num_points = d.getNumberOfPoints();
      for (int i = 0; i < num_points; i++) {
        Float ex = 400 * (d.getPoint(i).x - xLower) / dx + 50;
        ex -= diam / 2;
        Float ey = -400 * (d.getPoint(i).y - yLower) / dy + 450;
        ey -= diam / 2;
        g2d.fill(new Ellipse2D.Float(ex, ey, diam, diam));
      }
    }
  }
示例#13
0
 private synchronized void toBuffer(int s, Object a) {
   a1.add(s);
   a2.add(a);
   if (s == clear) {
     clearBuffer();
   }
   if (s == opaque) theOpaque = (Boolean) a;
   if (s == setBackground) theBackground = (Color) a;
   if (inBuffer) return;
   if (isSetter(s)) return;
   Graphics g = getGraphics();
   if (g == null) return;
   Graphics2D g2 = (Graphics2D) g;
   g2.setPaint(origPaint);
   g2.setFont(origFont);
   g2.setStroke(origStroke);
   for (int i = 0; i < a1.size() - 1; i++) {
     int s1 = a1.get(i);
     Object s2 = a2.get(i);
     if (isSetter(s1)) doPaint(g2, s1, s2);
   }
   doPaint((Graphics2D) g, s, a);
 }
  /**
   * The GraphicDrawer.paint() Function allows the user to draw pre-defined graphics into a given
   * Graphics Object. The user may give the rectangle properties which defines at best the desired
   * area to draw with a Int id which requests the desired graphic, whenever it's vectorial or
   * raster graphics
   *
   * @author LuisArturo
   */
  public void paint(int id, Dimension r, Graphics g) {
    Graphics2D g2;
    switch (id) {
      case FILE:
        {
          g2 = (Graphics2D) g;
          try {
            FileInputStream img = new FileInputStream("archivo.png");
            BufferedImage in = ImageIO.read(img);
            g2.drawImage(
                in, r.width * 2 / 10, r.height * 2 / 10, r.width * 2 / 3, r.height * 2 / 3, null);
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
        break;
      case REDO:
        {
          g2 = (Graphics2D) g;
          g2.setColor(color);
          g2.fillArc(
              r.width * 2 / 10, r.height * 2 / 10, r.width * 6 / 10, r.height * 6 / 10, 270, 225);
          Polygon p = new Polygon();
          p.addPoint(r.width * 2 / 10, r.height * 2 / 10);
          p.addPoint(r.width * 2 / 10, r.height * 5 / 10);
          p.addPoint(r.width * 5 / 10, r.height * 5 / 10);
          g2.fillPolygon(p);
          g2.setColor(g2.getBackground());
          g2.fillArc(
              r.width * 3 / 10, r.height * 3 / 10, r.width * 4 / 10, r.height * 4 / 10, 270, 225);
        }
        break;
      case TEXT:
        {
          g2 = (Graphics2D) g;
          g2.setColor(color);
          g2.setFont(new Font("Serif", Font.ITALIC | Font.BOLD, r.height * 6 / 10));
          g2.drawChars("A".toCharArray(), 0, 1, r.width * 3 / 10, r.height * 7 / 10);
        }
        break;
      case SELECT:
        {
          g2 = (Graphics2D) g;
          try {
            FileInputStream img = new FileInputStream("mano.png");
            BufferedImage in = ImageIO.read(img);
            g2.drawImage(
                in, r.width * 1 / 10, r.height * 1 / 10, r.width * 2 / 3, r.height * 2 / 3, null);
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
        break;
      case PENCIL:
        {
          g2 = (Graphics2D) g;
          try {
            FileInputStream img = new FileInputStream("lapiz.png");
            BufferedImage in = ImageIO.read(img);
            g2.drawImage(
                in, r.width * 2 / 10, r.height * 2 / 10, r.width * 2 / 3, r.height * 2 / 3, null);
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
        break;

      case ARROW:
        {
          g2 = (Graphics2D) g;
          try {
            FileInputStream img = new FileInputStream("arrow.png");
            BufferedImage in = ImageIO.read(img);
            g2.drawImage(
                in, r.width * 2 / 10, r.height * 2 / 10, r.width * 2 / 3, r.height * 2 / 3, null);
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
        break;
      case ZOOM:
        {
          g2 = (Graphics2D) g;
          try {
            FileInputStream img = new FileInputStream("zoom.png");
            BufferedImage in = ImageIO.read(img);
            g2.drawImage(
                in, r.width * 2 / 10, r.height * 2 / 10, r.width * 2 / 3, r.height * 2 / 3, null);
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
        break;
    }
  }
示例#15
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);
    }
  }
示例#16
0
 /** Draw a value. */
 protected void drawValue(Graphics2D g, Color sourceColor, String msg, double x, double y) {
   g.setFont(valueDrawerFont);
   valueDrawer.draw(g, getLegendColor(), getForeground(), getBackground(), msg, x, y);
 }
  private void drawPlot(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(
        RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2d.setColor(Color.white);
    g2d.fillRect(0, 0, getWidth(), getHeight());

    double activeWidth = getWidth() - leftMargin - rightMargin;
    double activeHeight = getHeight() - topMargin - bottomMargin;
    int bottomY = getHeight() - bottomMargin;
    int rightX = getWidth() - rightMargin;

    // draw data line
    int x1, x2, y1, y2;
    g2d.setColor(Color.red);
    for (int i = 1; i < numComponents; i++) {
      x1 = (int) (leftMargin + ((double) (i - 1) / (numComponents - 1)) * activeWidth);
      y1 = (int) (bottomY - (plotData[i - 1][plotIndex] * 10.0) / 1000 * activeHeight);
      x2 = (int) (leftMargin + ((double) (i) / (numComponents - 1)) * activeWidth);
      y2 = (int) (bottomY - (plotData[i][plotIndex] * 10) / 1000 * activeHeight);
      g2d.drawLine(x1, y1, x2, y2);
    }

    // draw data points
    int radius = 2;
    for (int i = 0; i < numComponents; i++) {
      x1 = (int) (leftMargin + ((double) (i) / (numComponents - 1)) * activeWidth);
      y1 = (int) (bottomY - (plotData[i][plotIndex] * 10.0) / 1000 * activeHeight);
      g2d.drawOval(x1 - radius - 1, y1 - radius - 1, 2 * radius + 2, 2 * radius + 2);
    }

    // draw axes
    g2d.setColor(Color.black);
    g2d.drawLine(leftMargin, bottomY, rightX, bottomY);
    g2d.drawLine(leftMargin, bottomY, leftMargin, topMargin);
    g2d.drawLine(leftMargin, topMargin, rightX, topMargin);
    g2d.drawLine(rightX, bottomY, rightX, topMargin);

    // draw ticks
    int tickSize = 4;
    for (int i = 1; i <= numComponents; i++) {
      x1 = (int) (leftMargin + ((double) (i - 1) / (numComponents - 1)) * activeWidth);
      g2d.drawLine(x1, bottomY, x1, bottomY + tickSize);
    }

    for (int i = 0; i <= 1000; i += 100) {
      y1 = (int) (bottomY - i / 1000.0 * activeHeight);
      g2d.drawLine(leftMargin, y1, leftMargin - tickSize, y1);
    }

    // labels
    DecimalFormat df = new DecimalFormat("#,###,###.###");
    Font font = new Font("SanSerif", Font.PLAIN, 11);
    FontMetrics metrics = g.getFontMetrics(font);
    int hgt, adv;
    hgt = metrics.getHeight();

    // x-axis labels
    String label;
    for (int i = 1; i <= numComponents; i++) {
      label = String.valueOf(i);
      x1 = (int) (leftMargin + ((double) (i - 1) / (numComponents - 1)) * activeWidth);
      adv = metrics.stringWidth(label) / 2;
      g2d.drawString(label, x1 - adv, bottomY + hgt + 4);
    }
    label = "Component";
    adv = metrics.stringWidth(label);
    int xAxisMidPoint = (int) (leftMargin + activeWidth / 2);
    g2d.drawString(label, xAxisMidPoint - adv / 2, bottomY + 2 * hgt + 6);

    // y-axis labels

    // rotate the font
    Font oldFont = g.getFont();
    Font f = oldFont.deriveFont(AffineTransform.getRotateInstance(-Math.PI / 2.0));
    g2d.setFont(f);

    int yAxisMidPoint = (int) (topMargin + activeHeight / 2);
    int offset;
    label = "Explained Variance (%)";
    offset = metrics.stringWidth("100.0") + 12 + hgt;
    adv = metrics.stringWidth(label);
    g2d.drawString(label, leftMargin - offset, yAxisMidPoint + adv / 2);

    // replace the rotated font.
    g2d.setFont(oldFont);

    df = new DecimalFormat("0.0");
    for (int i = 0; i <= 1000; i += 100) {
      label = df.format(i / 10);
      y1 = (int) (bottomY - i / 1000.0 * activeHeight);
      adv = metrics.stringWidth(label);
      g2d.drawString(label, leftMargin - adv - 12, y1 + hgt / 2);
    }

    // title

    // bold font
    oldFont = g.getFont();
    font = font = new Font("SanSerif", Font.BOLD, 12);
    g2d.setFont(font);

    label = "PCA Scree Plot";
    adv = metrics.stringWidth(label);
    g2d.drawString(label, getWidth() / 2 - adv / 2, topMargin - hgt - 5);

    g2d.setFont(oldFont);
  }
示例#18
0
 // also clip, transform, composite,
 // public boolean isOpaque(){return false;}//theOpaque!=null&&theOpaque;}
 // ---------------------------------------------------------
 private void doPaint(Graphics2D g, int s, Object o) {
   // process an operation from the buffer
   // System.out.println(s);
   Object o1 = null,
       o2 = null,
       o3 = null,
       o4 = null,
       o5 = null,
       o6 = null,
       o7 = null,
       o8 = null,
       o9 = null,
       o10 = null,
       o11 = null;
   if (o instanceof Object[]) {
     Object[] a = (Object[]) o;
     if (a.length > 0) o1 = a[0];
     if (a.length > 1) o2 = a[1];
     if (a.length > 2) o3 = a[2];
     if (a.length > 3) o4 = a[3];
     if (a.length > 4) o5 = a[4];
     if (a.length > 5) o6 = a[5];
     if (a.length > 6) o7 = a[6];
     if (a.length > 7) o8 = a[7];
     if (a.length > 8) o9 = a[8];
     if (a.length > 9) o10 = a[9];
     if (a.length > 10) o11 = a[10];
   }
   switch (s) {
     case clear:
       paintBackground(g, theBackground);
       break;
       // public void addRenderingHints(Map<?,?> hints)
       // {toBuffer("addRenderingHints",hints );}
     case addRenderingHints:
       g.addRenderingHints((Map<?, ?>) o);
       break;
     case clip1:
       g.clip((Shape) o);
       break;
     case draw1:
       g.draw((Shape) o);
       break;
     case draw3DRect:
       g.draw3DRect((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Boolean) o5);
       break;
     case drawGlyphVector:
       g.drawGlyphVector((GlyphVector) o1, (Float) o2, (Float) o3);
       break;
     case drawImage1:
       g.drawImage((BufferedImage) o1, (BufferedImageOp) o2, (Integer) o3, (Integer) o4);
       break;
     case drawImage2:
       g.drawImage((Image) o1, (AffineTransform) o2, (ImageObserver) o3);
       break;
     case drawRenderableImage:
       g.drawRenderableImage((RenderableImage) o1, (AffineTransform) o2);
       break;
     case drawRenderedImage:
       g.drawRenderedImage((RenderedImage) o1, (AffineTransform) o2);
       break;
     case drawString1:
       g.drawString((AttributedCharacterIterator) o1, (Float) o2, (Float) o3);
       break;
     case drawString2:
       g.drawString((AttributedCharacterIterator) o1, (Integer) o2, (Integer) o3);
       break;
     case drawString3:
       g.drawString((String) o1, (Float) o2, (Float) o3);
       break;
     case drawString4:
       g.drawString((String) o1, (Integer) o2, (Integer) o3);
       break;
     case fill:
       g.fill((Shape) o);
       break;
     case fill3DRect:
       g.fill3DRect((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Boolean) o5);
       break;
     case rotate1:
       g.rotate((Double) o);
       break;
     case rotate2:
       g.rotate((Double) o1, (Double) o2, (Double) o3);
       break;
     case scale1:
       g.scale((Double) o1, (Double) o2);
       break;
     case setBackground:
       g.setBackground(
           (Color) o); // paintBackground(g,(Color)o); /*super.setBackground((Color)o) ;*/
       break;
     case setComposite:
       g.setComposite((Composite) o);
       break;
     case setPaint:
       g.setPaint((Paint) o);
       break;
     case setRenderingHint:
       g.setRenderingHint((RenderingHints.Key) o1, o2);
       break;
     case setRenderingHints:
       g.setRenderingHints((Map<?, ?>) o);
       break;
     case setStroke:
       g.setStroke((Stroke) o);
       break;
     case setTransform:
       g.setTransform(makeTransform(o));
       break;
     case shear:
       g.shear((Double) o1, (Double) o2);
       break;
     case transform1:
       g.transform(makeTransform(o));
       break;
     case translate1:
       g.translate((Double) o1, (Double) o2);
       break;
     case translate2:
       g.translate((Integer) o1, (Integer) o2);
       break;
     case clearRect:
       g.clearRect((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4);
       break;
     case copyArea:
       g.copyArea(
           (Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6);
       break;
     case drawArc:
       g.drawArc(
           (Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6);
       break;
     case drawBytes:
       g.drawBytes((byte[]) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5);
       break;
     case drawChars:
       g.drawChars((char[]) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5);
       break;
     case drawImage4:
       g.drawImage((Image) o1, (Integer) o2, (Integer) o3, (Color) o4, (ImageObserver) o5);
       break;
     case drawImage5:
       g.drawImage((Image) o1, (Integer) o2, (Integer) o3, (ImageObserver) o4);
       break;
     case drawImage6:
       g.drawImage(
           (Image) o1,
           (Integer) o2,
           (Integer) o3,
           (Integer) o4,
           (Integer) o5,
           (Color) o6,
           (ImageObserver) o7);
       break;
     case drawImage7:
       g.drawImage(
           (Image) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (ImageObserver) o6);
       break;
     case drawImage8:
       g.drawImage(
           (Image) o1,
           (Integer) o2,
           (Integer) o3,
           (Integer) o4,
           (Integer) o5,
           (Integer) o6,
           (Integer) o7,
           (Integer) o8,
           (Integer) o9,
           (Color) o10,
           (ImageObserver) o11);
       break;
     case drawImage9:
       g.drawImage(
           (Image) o1,
           (Integer) o2,
           (Integer) o3,
           (Integer) o4,
           (Integer) o5,
           (Integer) o6,
           (Integer) o7,
           (Integer) o8,
           (Integer) o9,
           (ImageObserver) o10);
       break;
     case drawLine:
       g.drawLine((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4);
       break;
     case drawOval:
       g.drawOval((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4);
       break;
     case drawPolygon1:
       g.drawPolygon((int[]) o1, (int[]) o2, (Integer) o3);
       break;
     case drawPolygon2:
       g.drawPolygon((Polygon) o);
       break;
     case drawPolyline:
       g.drawPolyline((int[]) o1, (int[]) o2, (Integer) o3);
       break;
     case drawRect:
       g.drawRect((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4);
       break;
     case drawRoundRect:
       g.drawRoundRect(
           (Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6);
       break;
     case fillArc:
       g.fillArc(
           (Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6);
       break;
     case fillOval:
       g.fillOval((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4);
       break;
       // {toBuffer("fillPolygon",mkArg(xPoints,  yPoints, nPoints) );}
     case fillPolygon1:
       g.fillPolygon((int[]) o1, (int[]) o2, (Integer) o3);
       break;
     case fillPolygon2:
       g.fillPolygon((Polygon) o);
       break;
     case fillRect:
       g.fillRect((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4);
       break;
     case fillRoundRect:
       g.fillRoundRect(
           (Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6);
       break;
     case setClip1:
       g.setClip((Shape) o);
       break;
     case setColor:
       g.setColor((Color) o);
       break;
     case setFont:
       g.setFont((Font) o);
       break;
     case setPaintMode:
       g.setPaintMode();
       break;
     case setXORMode:
       g.setXORMode((Color) o);
       break;
     case opaque:
       super.setOpaque((Boolean) o);
       break;
     case drawOutline: // g.drawString((String)o1, (Integer)o2, (Integer)o3) ;break;
       {
         FontRenderContext frc = g.getFontRenderContext();
         TextLayout tl = new TextLayout((String) o1, g.getFont(), frc);
         Shape s1 = tl.getOutline(null);
         AffineTransform af = g.getTransform();
         g.translate((Integer) o2, (Integer) o3);
         g.draw(s1);
         g.setTransform(af);
       }
       ;
       break;
     default:
       System.out.println("Unknown image operation " + s);
   }
 }