コード例 #1
1
  /**
   * Draws as much as possible of a given string into a 2d square region in a graphical space.
   *
   * @param g component onto which to draw
   * @param f font to use in drawing the string
   * @param s string to be drawn
   * @param xPos
   * @param yPos
   * @param width
   * @param height
   */
  public static void drawStringMultiline(
      Graphics2D g, Font f, String s, double xPos, double yPos, double width, double height) {
    FontMetrics fm = g.getFontMetrics(f);
    int w = fm.stringWidth(s);
    int h = fm.getAscent();
    // g.setColor(Color.LIGHT_GRAY);
    g.setColor(Color.BLACK);
    g.setFont(f);

    Scanner lineSplitter = new Scanner(s);
    // draw as much as can fit in each item
    // read all content from scanner, storing in string lists (where each string == 1 line), each
    // string should be as long as possible without overflowing the space
    int maxRows = (int) height / h;
    List<String> textRows = new ArrayList<>();
    while (lineSplitter.hasNextLine() && textRows.size() < maxRows) {
      String line = lineSplitter.nextLine();
      // if line is blank, insert to maintain paragraph seps
      if (line.trim().equals("")) {
        textRows.add("");
      }
      // else, pass to inner loop
      StringBuilder currentBuilder = new StringBuilder();
      int currentStrWidth = 0;
      Scanner splitter = new Scanner(line);
      while (splitter.hasNext() && textRows.size() < maxRows) {
        String token = splitter.next() + " ";
        // TODO incorporate weight detection, formatting for token?
        currentStrWidth += fm.stringWidth(token);
        if (currentStrWidth >= width) {
          // if string length >= glyph width, build row
          textRows.add(currentBuilder.toString());
          currentBuilder = new StringBuilder();
          currentBuilder.append(token);
          currentStrWidth = fm.stringWidth(token);
        } else {
          // if not yet at end of row, append to builder
          currentBuilder.append(token);
        }
      }

      // if we've still space and still have things to write, add them here
      if (textRows.size() < maxRows) {
        textRows.add(currentBuilder.toString());
        currentBuilder = new StringBuilder();
        currentStrWidth = 0;
      }
    }

    // write each line to object
    for (int t = 0; t < textRows.size(); t++) {
      String line = textRows.get(t);
      if (fm.stringWidth(line) <= width) {
        // ensure that string doesn't overflow the box
        //                g.drawString(line, (float) (xPos-(width/2.)), (float) (yPos-(height/2.) +
        // h * (t+1)));
        g.drawString(line, (float) xPos, (float) (yPos + h * (t + 1)));
      }
    }
  }
コード例 #2
0
 public void backgroundImpl() {
   if (backgroundAlpha) {
     // Create a small array that can be used to set the pixels several times.
     // Using a single-pixel line of length 'width' is a tradeoff between
     // speed (setting each pixel individually is too slow) and memory
     // (an array for width*height would waste lots of memory if it stayed
     // resident, and would terrify the gc if it were re-created on each trip
     // to background().
     WritableRaster raster = ((BufferedImage) image).getRaster();
     if ((clearPixels == null) || (clearPixels.length < width)) {
       clearPixels = new int[width];
     }
     java.util.Arrays.fill(clearPixels, backgroundColor);
     for (int i = 0; i < height; i++) {
       raster.setDataElements(0, i, width, 1, clearPixels);
     }
   } else {
     // new Exception().printStackTrace(System.out);
     // in case people do transformations before background(),
     // need to handle this with a push/reset/pop
     pushMatrix();
     resetMatrix();
     g2.setColor(new Color(backgroundColor)); // , backgroundAlpha));
     g2.fillRect(0, 0, width, height);
     popMatrix();
   }
 }
コード例 #3
0
  @Override
  protected void paintBackground(Graphics g, JComponent component) {
    getGlassParameters();
    Graphics2D g2d = (Graphics2D) g.create();

    if (getOrientation().isHorizontal()) {
      paintBackground(
          g2d,
          0,
          0,
          component.getWidth(),
          component.getHeight(),
          getOrientation().isHorizontal(),
          component);
    } else {
      paintBackground(
          g2d,
          0,
          0,
          component.getHeight(),
          component.getWidth(),
          getOrientation().isHorizontal(),
          component);
    }

    g2d.dispose();
  }
コード例 #4
0
ファイル: DrawTool.java プロジェクト: tstamler/linAlg
  // From: http://forum.java.sun.com/thread.jspa?threadID=378460&tstart=135
  void drawArrow(
      Graphics2D g2d,
      int xCenter,
      int yCenter,
      int x,
      int y,
      float stroke,
      BasicStroke drawStroke) {
    double aDir = Math.atan2(xCenter - x, yCenter - y);
    // Line can be dashed.
    g2d.setStroke(drawStroke);
    g2d.drawLine(x, y, xCenter, yCenter);
    // make the arrow head solid even if dash pattern has been specified
    g2d.setStroke(lineStroke);
    Polygon tmpPoly = new Polygon();
    int i1 = 12 + (int) (stroke * 2);
    // make the arrow head the same size regardless of the length length
    int i2 = 6 + (int) stroke;
    tmpPoly.addPoint(x, y);
    tmpPoly.addPoint(x + xCor(i1, aDir + .5), y + yCor(i1, aDir + .5));
    tmpPoly.addPoint(x + xCor(i2, aDir), y + yCor(i2, aDir));
    tmpPoly.addPoint(x + xCor(i1, aDir - .5), y + yCor(i1, aDir - .5));
    tmpPoly.addPoint(x, y); // arrow tip
    g2d.drawPolygon(tmpPoly);

    // Remove this line to leave arrow head unpainted:
    g2d.fillPolygon(tmpPoly);
  }
コード例 #5
0
ファイル: StationModelCanvas.java プロジェクト: ethanrd/IDV
  /**
   * Override the pain method do draw the axis lines
   *
   * @param g The graphics to paint to
   */
  public void paint(Graphics g) {
    Rectangle b = getBounds();
    g.setColor(canvasBg);
    g.fillRect(0, 0, b.width, b.height);
    paintGrid(g);

    Point center = getCenter();
    if (g instanceof Graphics2D) {
      ((Graphics2D) g).translate(center.x, center.y);
    }
    super.paint(g);
    if (g instanceof Graphics2D) {
      ((Graphics2D) g).scale(1.0, 1.0);
    }
    g.setColor(Color.gray);
    g.drawLine(0, -10 * b.height, 0, 10 * b.height);
    g.drawLine(-10 * b.width, 0, 10 * b.width, 0);

    MetSymbol tmp = highlightedMetSymbol;
    if (tmp != null) {
      Rectangle tb = tmp.getBounds();
      g.setColor(Color.red);
      g.drawRect(tb.x - 2, tb.y - 2, tb.width + 4, tb.height + 4);
    }
  }
コード例 #6
0
 protected void drawImageMosaic(Graphics2D g2) {
   // Break the image up into tiles. Draw each
   //   tile with its own transparency, allowing
   //   the background to show through to varying
   //   degrees.
   int side = 36;
   int width = mImage.getWidth();
   int height = mImage.getHeight();
   for (int y = 0; y < height; y += side) {
     for (int x = 0; x < width; x += side) {
       // Calculate an appropriate transparency value.
       float xBias = (float) x / (float) width;
       float yBias = (float) y / (float) height;
       float alpha = 1.0f - Math.abs(xBias - yBias);
       g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
       // Draw the subimage.
       int w = Math.min(side, width - x);
       int h = Math.min(side, height - y);
       BufferedImage tile = mImage.getSubimage(x, y, w, h);
       g2.drawImage(tile, x, y, null);
     }
   }
   // Reset the composite.
   g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
 }
コード例 #7
0
  public BufferedImage createCrystalCase(Image cover) {
    BufferedImage crystal =
        new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = crystal.createGraphics();
    g2.setRenderingHint(
        RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

    int width = cover.getWidth(null);
    int height = cover.getHeight(null);

    float scale;

    if (width > height) {
      scale = (float) IMAGE_WIDTH / (float) width;
    } else {
      scale = (float) IMAGE_HEIGHT / (float) height;
    }

    int scaledWidth = (int) ((float) width * scale);
    int scaledHeight = (int) ((float) height * scale);

    int x = (IMAGE_WIDTH - scaledWidth) / 2;
    int y = (IMAGE_HEIGHT - scaledHeight) / 2;

    g2.drawImage(cover, x, y, scaledWidth, scaledHeight, null);

    g2.dispose();

    return crystal;
  }
コード例 #8
0
 private static BufferedImage makeBufferedImage(Icon icon, int w, int h) {
   BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
   Graphics2D g = image.createGraphics();
   icon.paintIcon(null, g, (w - icon.getIconWidth()) / 2, (h - icon.getIconWidth()) / 2);
   g.dispose();
   return image;
 }
コード例 #9
0
ファイル: StdDraw.java プロジェクト: ihordey/algorithms-4th
 /**
  * Write the given text string in the current font, centered on (x, y) and rotated by the
  * specified number of degrees
  *
  * @param x the center x-coordinate of the text
  * @param y the center y-coordinate of the text
  * @param s the text
  * @param degrees is the number of degrees to rotate counterclockwise
  */
 public static void text(double x, double y, String s, double degrees) {
   double xs = scaleX(x);
   double ys = scaleY(y);
   offscreen.rotate(Math.toRadians(-degrees), xs, ys);
   text(x, y, s);
   offscreen.rotate(Math.toRadians(+degrees), xs, ys);
 }
コード例 #10
0
ファイル: JCanvas.java プロジェクト: xaleth09/Connect4-AI
 /** [Internal] */
 private void paintBackground(Graphics2D g2, Color theBackground) {
   Color color1 = g2.getColor();
   if (theBackground == null) theBackground = Color.white;
   g2.setColor(theBackground);
   g2.fillRect(0, 0, 30000, 30000);
   g2.setColor(color1);
 }
コード例 #11
0
ファイル: JCanvas.java プロジェクト: xaleth09/Connect4-AI
  /** paint the canvas into a image file of given width and height */
  public void writeToImage(String s, int w, int h) {
    String ext;
    File f;
    try {
      ext = s.substring(s.lastIndexOf(".") + 1);
      f = new File(s);
    } catch (Exception e) {
      System.out.println(e);
      return;
    }
    if (!ext.equals("jpg") && !ext.equals("png")) {
      System.out.println("Cannot write to file: Illegal extension " + ext);
      return;
    }
    boolean opq = true;
    if (theOpaque != null) opq = theOpaque;

    BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = image.createGraphics();
    g2.setBackground(Color.white);
    g2.setPaint(Color.black);
    g2.setStroke(new BasicStroke(1));
    g2.setRenderingHint(
        RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    doBuffer(g2, true, new Rectangle(0, 0, w, h));
    try {
      ImageIO.write(image, ext, f);
    } catch (Exception e) {
      System.out.println(e);
    }
  }
コード例 #12
0
ファイル: BlackHole.java プロジェクト: cev62/JavaFun
  public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;

    g2d.draw(
        new Ellipse2D.Double(
            centerx - (centery - 500),
            centery - (centery - 500),
            2 * (centery - 500),
            2 * (centery - 500)));

    try {
      g2d.draw(
          new Ellipse2D.Double(
              player.x - player.size / 2, player.y - player.size / 2, player.size, player.size));
    } catch (NullPointerException e) {
    }

    k = 1;
    while (k <= ballCount) {

      g2d.draw(
          new Ellipse2D.Double(
              ball[k].x - ball[k].size / 2,
              ball[k].y - ball[k].size / 2,
              ball[k].size,
              ball[k].size));
      k++;
    }
  }
コード例 #13
0
ファイル: PathSegment.java プロジェクト: NVSL/laserTurtle
 /**
  * Method to paint this path segment
  *
  * @param g the graphics context
  */
 public void paintComponent(Graphics g) {
   Graphics2D g2 = (Graphics2D) g;
   BasicStroke penStroke = new BasicStroke(this.width);
   g2.setStroke(penStroke);
   g2.setColor(this.color);
   g2.draw(this.line);
 }
コード例 #14
0
ファイル: Jclec.java プロジェクト: RubelAhmed57/KEEL
  /**
   * Draw method
   *
   * @param g2 Graphics element
   * @param select Selected node
   */
  public void draw(Graphics2D g2, boolean select) {
    Point pinit = new Point(centre.x - 25, centre.y - 25);
    Point pfin = new Point(centre.x + 25, centre.y + 25);
    figure =
        new RoundRectangle2D.Float(
            pinit.x, pinit.y, Math.abs(pfin.x - pinit.x), Math.abs(pfin.y - pinit.y), 20, 20);

    g2.setColor(Color.black);
    if (select) {
      Stroke s = g2.getStroke();
      g2.setStroke(
          new BasicStroke(
              5, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[] {1, 1}, 0));
      g2.draw(figure);
      g2.setStroke(s);
    } else {
      g2.draw(figure);
    }
    g2.drawImage(image, centre.x - 25, centre.y - 25, 50, 50, pd);

    g2.setFont(new Font("Courier", Font.BOLD + Font.ITALIC, 12));
    FontMetrics metrics = g2.getFontMetrics();
    int width = metrics.stringWidth(dsc.getName());
    int height = metrics.getHeight();
    g2.drawString(dsc.getName(), centre.x - width / 2, centre.y + 40);
  }
コード例 #15
0
ファイル: DrawTool.java プロジェクト: tstamler/linAlg
 void drawLine(Graphics g, DrawObject L) {
   if (L == null) {
     return;
   }
   if ((sequencingOn) && (L.sequenceNum != currentSequenceNumDisplay)) {
     return;
   }
   Graphics2D g2 = (Graphics2D) g;
   g2.setStroke(L.drawStroke);
   int x1 = (int) ((L.x - minX) / (maxX - minX) * (D.width - 2 * inset));
   int y1 = (int) ((L.y - minY) / (maxY - minY) * (D.height - 2.0 * inset));
   int x2 = (int) ((L.x2 - minX) / (maxX - minX) * (D.width - 2 * inset));
   int y2 = (int) ((L.y2 - minY) / (maxY - minY) * (D.height - 2.0 * inset));
   if (L.isArrow) {
     drawArrow(
         (Graphics2D) g,
         inset + x1,
         D.height - y1 - inset,
         inset + x2,
         D.height - y2 - inset,
         1.0f,
         L.drawStroke);
   } else {
     g.drawLine(inset + x1, D.height - y1 - inset, inset + x2, D.height - y2 - inset);
   }
 }
コード例 #16
0
 public void paint(Graphics g) {
   Graphics2D g_2d = (Graphics2D) g;
   Ellipse2D ellipse = new Ellipse2D.Double(0, 2, 80, 80);
   Rectangle2D rect = new Rectangle2D.Double(40, 2, 80, 80);
   Area a1 = new Area(ellipse);
   Area a2 = new Area(rect);
   a1.intersect(a2); // "Óë"
   g_2d.fill(a1);
   ellipse.setFrame(130, 2, 80, 80);
   rect.setFrame(170, 2, 80, 80);
   a1 = new Area(ellipse);
   a2 = new Area(rect);
   a1.add(a2); // "»ò"
   g_2d.draw(a1);
   ellipse.setFrame(0, 90, 80, 80);
   rect.setFrame(40, 90, 80, 80);
   a1 = new Area(ellipse);
   a2 = new Area(rect);
   a1.subtract(a2); // "²î"
   g_2d.draw(a1);
   ellipse.setFrame(130, 90, 80, 80);
   rect.setFrame(170, 90, 80, 80);
   a1 = new Area(ellipse);
   a2 = new Area(rect);
   a1.exclusiveOr(a2); // "Òì»ò"
   g_2d.fill(a1);
 }
コード例 #17
0
ファイル: Ball.java プロジェクト: benlakey/breakout
  /**
   * Erases the ball.
   *
   * @param ballGraphic The graphics object to use.
   */
  public void erase(Graphics2D ballGraphic) {
    double upperLeftX = _position.x - (_pixelWidth / 2);
    double upperLeftY = _position.y - (_pixelHeight / 2);

    ballGraphic.setPaint(Color.WHITE);
    ballGraphic.fillOval((int) upperLeftX, (int) upperLeftY, _pixelWidth, _pixelHeight);
  }
コード例 #18
0
  protected void paintComponent(Graphics g2) {
    super.paintComponent(g2);

    Graphics2D g = (Graphics2D) g2;
    test.draw(g2, 0, 0);
    // Shape triangle = new Polygon(new int[] {15, 20, 10}, new int[] {0, 10, 10}, 3);

    Shape square = new Rectangle2D.Double(0, 0, 30, 30);

    g.setColor(Color.white);

    g.translate(20, 20);
    // g.rotate(theta, 10, 10);
    // g.fill(star);

    // g.setColor(new Color(0, 86, 141));
    // g.fill(square);
    if (star2 != null) g.fill(star2);

    // Graphics2D g3 = (Graphics2D) g;
    // 3.fill(star);

    // g.setStroke(new BasicStroke(1));
    // g.translate(x, y);
    // g.draw(line);
    // g.draw(line2);

  }
コード例 #19
0
 /** Draws the edge structure of the tree */
 public void drawEdges(Graphics2D gg) {
   gg.setPaint(Color.black);
   Enumeration nodeList = argument.getBreadthFirstTraversal().elements();
   // For each vertex...
   while (nodeList.hasMoreElements()) {
     // Get its edge list...
     TreeVertex vertex = (TreeVertex) nodeList.nextElement();
     Enumeration edges = vertex.getEdgeList().elements();
     // For each edge in the list...
     while (edges.hasMoreElements()) {
       TreeEdge edge = (TreeEdge) edges.nextElement();
       // If we have several vertices on layer 0, only draw
       // edges for layers below that
       if (!(argument.isMultiRoots() && vertex.getLayer() == 0)) {
         // If the edge has been selected with the mouse,
         // use a thick line
         if (edge.isSelected()) {
           gg.setStroke(selectStroke);
         }
         gg.draw(edge.getShape(this));
         // If we used a thick line, reset the stroke to normal
         // line for next edge.
         if (edge.isSelected()) {
           gg.setStroke(solidStroke);
         }
         TreeVertex edgeSource = edge.getDestVertex();
       }
     }
   }
 }
コード例 #20
0
ファイル: GeoBall.java プロジェクト: nbitesGuest/nbites
 public void draw(Graphics2D g2, boolean shouldFlip) {
   Font font = new Font("Sans_Serif", Font.PLAIN, 30);
   g2.setColor(Color.orange);
   g2.fillOval((int) x - 10, (int) (FieldConstants.FIELD_HEIGHT - y - 10), 20, 20);
   font = new Font("Sans_Serif", Font.PLAIN, 18);
   g2.setFont(font);
   g2.drawString(Double.toString(confidence), x, FieldConstants.FIELD_HEIGHT - y + 60);
 }
コード例 #21
0
 public void paintComponent(Graphics g) {
   Graphics2D g2 = (Graphics2D) g;
   Rectangle2D rect = new Rectangle2D.Double(0, 0, getWidth() - 1, getHeight() - 1);
   g2.setPaint(Color.YELLOW);
   g2.fill(rect);
   g2.setPaint(Color.BLUE);
   g2.draw(rect);
 }
コード例 #22
0
 public Image getImage() {
   BufferedImage image =
       new BufferedImage(getIconWidth(), getIconHeight(), BufferedImage.TYPE_INT_ARGB);
   Graphics2D g = image.createGraphics();
   paintIcon(null, g, 0, 0);
   g.dispose();
   return image;
 }
コード例 #23
0
 public void smooth() {
   smooth = true;
   g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
   g2.setRenderingHint(
       RenderingHints.KEY_INTERPOLATION,
       //                        RenderingHints.VALUE_INTERPOLATION_BILINEAR);
       RenderingHints.VALUE_INTERPOLATION_BICUBIC);
 }
コード例 #24
0
ファイル: Arrows.java プロジェクト: rtitec/swixml2
 private void drawArrow(Graphics2D g2, double theta, double x0, double y0) {
   double x = x0 - barb * Math.cos(theta + phi);
   double y = y0 - barb * Math.sin(theta + phi);
   g2.draw(new Line2D.Double(x0, y0, x, y));
   x = x0 - barb * Math.cos(theta - phi);
   y = y0 - barb * Math.sin(theta - phi);
   g2.draw(new Line2D.Double(x0, y0, x, y));
 }
コード例 #25
0
ファイル: Banner.java プロジェクト: bibhutibhusan89/Jafun
  public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
    Graphics2D g2 = (Graphics2D) g;
    if (page > getPageCount(g2, pf)) return Printable.NO_SUCH_PAGE;
    g2.translate(pf.getImageableX(), pf.getImageableY());

    drawPage(g2, pf, page);
    return Printable.PAGE_EXISTS;
  }
コード例 #26
0
ファイル: EllipseVertex.java プロジェクト: andrewiggins/Graph
  public void paintVertex(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    g2.setColor(this.getFillColor());
    g2.fill(this);
    g2.setColor(this.border);
    g2.draw(this);
  }
コード例 #27
0
ファイル: RasterTest.java プロジェクト: FauxFaux/jdk9-jdk
    public int print(Graphics g, PageFormat pgFmt, int pgIndex) {
      if (pgIndex > 0) return Printable.NO_SUCH_PAGE;

      Graphics2D g2d = (Graphics2D) g;
      g2d.translate(pgFmt.getImageableX(), pgFmt.getImageableY());
      doPaint(g2d);
      return Printable.PAGE_EXISTS;
    }
コード例 #28
0
 private void shadeExt(Graphics2D g2, int r, int g, int b, int a) {
   g2.setPaint(new Color(r, g, b, a));
   g2.fillRect(0, 0, iw, rect.y); /* _N_ */
   g2.fillRect(
       rect.x + rect.width + 1, rect.y, iw - rect.x - rect.width - 1, rect.height + 1); /* E */
   g2.fillRect(0, rect.y, rect.x, rect.height + 1); /* W */
   g2.fillRect(0, rect.y + rect.height + 1, iw, ih - rect.y - rect.height - 1); /* _S_ */
 }
コード例 #29
0
 ////////////////////////////////////////////////////////////////////////////
 // Paint
 //
 public synchronized void paint(Graphics g) {
   Graphics2D g2 = (Graphics2D) g;
   // Graphics2D buffGraphics2 = (Graphics2D) buffGraphics;
   //    g2.drawImage(bImage, 3, 3, getWidth() - 3, getHeight() - 3,
   //        0, 0, iWidth, iHeight, null);
   g2.drawImage(bImage, 0, 0, iWidth, iHeight, null);
   g2.drawString(String.valueOf(System.currentTimeMillis()), 10, 10);
 }
コード例 #30
0
  private void applyAlphaMask(
      BufferedImage buffer, BufferedImage alphaMask, int itemWidth, int itemHeight) {

    Graphics2D g2 = buffer.createGraphics();
    g2.setComposite(AlphaComposite.DstOut);
    g2.drawImage(alphaMask, null, 0, itemHeight);
    g2.dispose();
  }