public void draw(Graphics g) { animate(); g.setColor(Color.black); g.fillRect(0, 0, dim.width, dim.height); g.setColor(Color.white); numpaint++; DebugPrinter dbg = new DebugPrinter(g, 50, 60); dbg.print( "Spring-mass demo by yigal irani, drag balls or create new ones by clicking inside box"); dbg.print("frame", numpaint); dbg.print("fps", 1 / timer.time_diff); Point top_left = point_by_vec(new Vec(-1, 1)); g.draw3DRect(top_left.x, top_left.y, screen_dist(2), screen_dist(2), true); for (int i = 0; i < springs.size(); i++) { Spring spring = springs.get2(i); Point p1 = point_by_vec(balls.get2(spring.start).pos); Point p2 = point_by_vec(balls.get2(spring.end).pos); g.drawLine(p1.x, p1.y, p2.x, p2.y); } for (int i = 0; i < balls.size(); i++) { Ball ball = balls.get2(i); Point p = point_by_vec(ball.pos); int screen_radius = screen_dist(RADIUS); g.setColor(Color.blue); g.fillOval(p.x - screen_radius, p.y - screen_radius, screen_radius * 2, screen_radius * 2); g.setColor(Color.white); g.drawOval(p.x - screen_radius, p.y - screen_radius, screen_radius * 2, screen_radius * 2); g.drawString("" + i, p.x, p.y); } }
protected void paintComponent(Graphics g) { super.paintComponent(g); if (model != null) { newImgBuf(); // refresh the image buffer if necessary // compute the real-to-screen ratio, this variable differs // from model.real2Screen by zoomScale Dimension sz = getSize(); double real2Screen0 = model.getScaleFromSpan(realSpan, sz.width, sz.height); model.setMatrix(viewMatrix, real2Screen0 * zoomScale, sz.width / 2, sz.height / 2); imgG.setColor(Color.BLACK); imgG.fillRect(0, 0, sz.width, sz.height); model.paint(imgG); g.drawImage(img, 0, 0, this); } }
void drawPoint(Graphics g, DrawObject p) { if (p == null) { return; } if ((sequencingOn) && (p.sequenceNum != currentSequenceNumDisplay)) { return; } int x = (int) ((p.x - minX) / (maxX - minX) * (D.width - 2 * inset)); int y = (int) ((p.y - minY) / (maxY - minY) * (D.height - 2.0 * inset)); if (p.diameter > 1) { int r = p.diameter / 2; g.fillOval(inset + x - r, D.height - y - inset - r, 2 * r, 2 * r); } else { g.fillRect(inset + x, D.height - y - inset, 1, 1); } }
/** * Called by the paint method to draw the graph and its graph items. * * @param g the graphics context. */ public void paintComponent(Graphics g) { Dimension dim = getSize(); Insets insets = getInsets(); dataArea = new Rectangle( insets.left, insets.top, dim.width - insets.left - insets.right - 1, dim.height - insets.top - insets.bottom - 1); // background if (isOpaque()) { g.setColor(getBackground()); g.fillRect(0, 0, dim.width, dim.height); } g.setColor(getForeground()); // get axis tickmarks double xticks[] = xAxis.getTicks(); double yticks[] = yAxis.getTicks(); int yb = dataArea.y + dataArea.height; // draw grid if (showGrid) { g.setColor(gridColor != null ? gridColor : getBackground().darker()); // vertical x grid lines for (int i = 0; i < xticks.length; i += 2) { int x = dataArea.x + (int) Math.round(xticks[i]); g.drawLine(x, dataArea.y, x, dataArea.y + dataArea.height); } // horizontal y grid lines for (int i = 0; i < yticks.length; i += 2) { int y = yb - (int) Math.round(yticks[i]); g.drawLine(dataArea.x, y, dataArea.x + dataArea.width, y); } } for (int i = 0; i < graphItems.size(); i++) { ((GraphItem) graphItems.elementAt(i)).draw(this, g, dataArea, xAxis, yAxis); } if (sPt != null && ePt != null) { g.setColor(getForeground()); g.drawRect( Math.min(sPt.x, ePt.x), Math.min(sPt.y, ePt.y), Math.abs(ePt.x - sPt.x), Math.abs(ePt.y - sPt.y)); } }
void drawImage(Graphics g, DrawObject I) { if ((sequencingOn) && (I.sequenceNum != currentSequenceNumDisplay)) { return; } for (int i = 0; i < I.pixels.length; i++) { for (int j = 0; j < I.pixels[i].length; j++) { Color c = new Color(I.pixels[i][j][1], I.pixels[i][j][2], I.pixels[i][j][3]); g.setColor(c); // Note: i starts at the top row of the image. int x = j; // x along the x-axis is the column coord of pixels int y = I.pixels[i].length - i; int x1 = (int) ((x - minX) / (maxX - minX) * (D.width - 2 * inset)); int y1 = (int) ((y - minY) / (maxY - minY) * (D.height - 2.0 * inset)); int x2 = (int) ((x + 1 - minX) / (maxX - minX) * (D.width - 2 * inset)); int y2 = (int) ((y - 1 - minY) / (maxY - minY) * (D.height - 2.0 * inset)); g.fillRect(inset + x1, D.height - y1 - inset, x2 - x1, y1 - y2); } } }
public static void drawValueBar( int x, int y, String labelhead, double value, double percentage, int colorindex, Graphics g) { g.setColor(GetColor(colorindex)); g.drawRect(x, y, VALUE_BAR_WIDTH, VALUE_BAR_HEIGHT); int barwidth; if (percentage < 0.0) { // Unknown value barwidth = VALUE_BAR_WIDTH - 1; } else { barwidth = (int) ((VALUE_BAR_WIDTH - 1) * Math.min(1.0, percentage)); } // g.setColor(gradientColor(percentage)); g.fillRect(x + 1, y + 1, barwidth, VALUE_BAR_HEIGHT - 1); if (labelhead != null) { g.setFont(MainFrame.defaultFont); // g.setColor(MainFrame.labelColor); int off = 2; g.drawString(labelhead, x + VALUE_BAR_WIDTH + 2, y + VALUE_BAR_HEIGHT); // off += (labelhead.length()+1) * 4; // Just a guess String maxStr = new String("DateRate [/sec]:"); off += (int) ((maxStr.length() + 1) * 5.5); if (value < 0.0) { g.drawString("?", x + VALUE_BAR_WIDTH + off, y + VALUE_BAR_HEIGHT); } else { if (percentage < 0.0) { g.drawString(format(value), x + VALUE_BAR_WIDTH + off, y + VALUE_BAR_HEIGHT); } else { g.drawString( format(value) + " (" + format(percentage * 100) + "%)", x + VALUE_BAR_WIDTH + off, y + VALUE_BAR_HEIGHT); } } } }
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); }