protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); int w = getWidth(); int h = getHeight(); // Draw ordinate. g2.draw(new Line2D.Double(Points, Points, Points, h - Points)); // Draw abcissa. g2.draw(new Line2D.Double(Points, h - Points, w - Points, h - Points)); // Draw labels. Font font = g2.getFont(); FontRenderContext frc = g2.getFontRenderContext(); LineMetrics lm = font.getLineMetrics("0", frc); float sh = lm.getAscent() + lm.getDescent(); // Ordinate label. String s = "Strecke"; float sy = Points + ((h - 2 * Points) - s.length() * sh) / 2 + lm.getAscent(); for (int i = 0; i < s.length(); i++) { String letter = String.valueOf(s.charAt(i)); float sw = (float) font.getStringBounds(letter, frc).getWidth(); float sx = (Points - sw) / 2; g2.drawString(letter, sx, sy); sy += sh; } // Abcissa label. s = "Datum"; sy = h - Points + (Points - sh) / 2 + lm.getAscent(); float sw = (float) font.getStringBounds(s, frc).getWidth(); float sx = (w - sw) / 2; g2.drawString(s, sx, sy); // Draw lines. double xInc = (double) (w - 2 * Points) / (data.length - 1); double scale = (double) (h - 2 * Points) / getMax(); g2.setPaint(Color.green.darker()); for (int i = 0; i < data.length - 1; i++) { double x1 = Points + i * xInc; double y1 = h - Points - scale * data[i]; double x2 = Points + (i + 1) * xInc; double y2 = h - Points - scale * data[i + 1]; g2.draw(new Line2D.Double(x1, y1, x2, y2)); } // Mark data points. g2.setPaint(Color.red); for (int i = 0; i < data.length; i++) { double x = Points + i * xInc; double y = h - Points - scale * data[i]; g2.fill(new Ellipse2D.Double(x - 2, y - 2, 4, 4)); } }
/* * (non-Javadoc) * * @see org.math.plot.render.AbstractDrawer#drawStringRatio(java.lang.String, * double[], double, double, double) */ public void drawTextBase(String label, double... rC) { int[] sC = projection.screenProjectionBase(rC); // Corner offset adjustment : Text Offset is used Here FontRenderContext frc = comp2D.getFontRenderContext(); Font font1 = comp2D.getFont(); int x = sC[0]; int y = sC[1]; double w = font1.getStringBounds(label, frc).getWidth(); double h = font1.getSize2D(); x -= (int) (w * text_Eastoffset); y += (int) (h * text_Northoffset); int wc = (int) (w * FastMath.cos(text_angle) + h * FastMath.sin(text_angle)); int hc = (int) (h * FastMath.cos(text_angle) + w * FastMath.sin(text_angle)); if (!comp2D.hitClip(x, y, wc, hc)) { return; } if (text_angle != 0) { comp2D.rotate(text_angle, x + w / 2, y - h / 2); } String[] lines = label.split("\n"); for (int i = 0; i < lines.length; i++) { comp2D.drawString(lines[i], x, y); y += h; } // comp2D.drawString(label, x, y); if (text_angle != 0) { comp2D.rotate(-text_angle, x + w / 2, y - h / 2); } }
public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; String message = "Hello, World!"; Font f = new Font("Serif", Font.BOLD, 36); g2.setFont(f); FontRenderContext context = g2.getFontRenderContext(); Rectangle2D bounds = f.getStringBounds(message, context); double x = (getWidth() - bounds.getWidth()) / 2; double y = (getHeight() - bounds.getHeight()) / 2; double ascent = -bounds.getY(); double baseY = y + ascent; g2.drawString(message, (int) x, (int) baseY); g2.setPaint(Color.LIGHT_GRAY); g2.draw(new Line2D.Double(x, baseY, x + bounds.getWidth(), baseY)); Rectangle2D rect = new Rectangle2D.Double(x, y, bounds.getWidth(), bounds.getHeight()); g2.draw(rect); }
/** * Gets the page count of this section. * * @param g2 the graphics context * @param pf the page format * @return the number of pages needed */ public int getPageCount(Graphics2D g2, PageFormat pf) { if (message.equals("")) return 0; FontRenderContext context = g2.getFontRenderContext(); Font f = new Font("Serif", Font.PLAIN, 72); Rectangle2D bounds = f.getStringBounds(message, context); scale = pf.getImageableHeight() / bounds.getHeight(); double width = scale * bounds.getWidth(); int pages = (int) Math.ceil(width / pf.getImageableWidth()); return pages; }
public void paintComponent(Graphics g) { // необходиом чтобы текст коректно отрисовывался в окне super.paintComponent(g); // рисуем текст в окне Graphics2D g2 = (Graphics2D) g; AffineTransform t = g2.getTransform(); g.drawString("It is text", 5, 5); // создание шрифта Font f = new Font("SanasSerif", Font.ITALIC, 20); g2.setFont(f); g2.drawString("It is new text", 5, 33); String[] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); for (int i = 5; i < 20; i++) { g2.rotate(-0.05); g2.setColor( new Color( (int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255))); Font f1 = new Font(fontNames[i], Font.BOLD, 20); g2.setFont(f1); g2.drawString(fontNames[i], 5, 20 * i); } // текст в центре g2.setTransform(t); // возращение к кординатам, которые запонилив начале Font f2 = new Font("SanasSerif", Font.ITALIC, 20); g2.setFont(f2); String s = "It is center!"; FontRenderContext context = g2.getFontRenderContext(); Rectangle2D r = f2.getStringBounds(s, context); double x1 = (getWidth() - r.getWidth()) / 2; double y1 = (getHeight() - r.getHeight()) / 2; double ascent = -r.getY(); // узнаем высоту текста double y2 = y1 + ascent; Rectangle2D rect = new Rectangle2D.Double(x1, y1, r.getWidth(), r.getHeight()); g2.setColor(Color.YELLOW); g2.fill(rect); g2.setColor(Color.red); g2.drawString(s, (int) x1, (int) y2); g2.setColor(Color.blue); g2.draw(new Line2D.Double(x1, y2, x1 + r.getWidth(), y2)); g2.draw(rect); }
public void drawShadowedText(String label, float alpha, double... pC) { int[] sC = projection.screenProjection(pC); // Corner offset adjustment : Text Offset is used Here FontRenderContext frc = comp2D.getFontRenderContext(); Font font1 = comp2D.getFont(); int x = sC[0]; int y = sC[1]; double w = font1.getStringBounds(label, frc).getWidth(); double h = font1.getSize2D(); x -= (int) (w * text_Eastoffset); y += (int) (h * text_Northoffset); int wc = (int) (w * FastMath.cos(text_angle) + h * FastMath.sin(text_angle)); int hc = (int) (h * FastMath.cos(text_angle) + w * FastMath.sin(text_angle)); if (!comp2D.hitClip(x, y, wc, hc)) { return; } if (text_angle != 0) { comp2D.rotate(text_angle, x + w / 2, y - h / 2); } Composite cs = comp2D.getComposite(); Color c = comp2D.getColor(); String[] lines = label.split("\n"); for (int i = 0; i < lines.length; i++) { comp2D.setColor(Color.white); comp2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha)); comp2D.fillRect(x, y - (int) h, (int) w, (int) h); comp2D.setComposite(cs); comp2D.setColor(c); comp2D.drawString(lines[i], x, y); y += h; } if (text_angle != 0) { comp2D.rotate(-text_angle, x + w / 2, y - h / 2); } }
public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; String message = "Hello, World!"; Font f = new Font("Serif", Font.BOLD, 36); g2.setFont(f); // measure the size of the message FontRenderContext context = g2.getFontRenderContext(); Rectangle2D bounds = f.getStringBounds(message, context); // set (x,y) = top left corner of text double x = (getWidth() - bounds.getWidth()) / 2; double y = (getHeight() - bounds.getHeight()) / 2; // add ascent to y to reach the baseline double ascent = -bounds.getY(); double baseY = y + ascent; // draw the message g2.drawString(message, (int) x, (int) baseY); g2.setPaint(Color.LIGHT_GRAY); // draw the baseline g2.draw(new Line2D.Double(x, baseY, x + bounds.getWidth(), baseY)); // draw the enclosing rectangle Rectangle2D rect = new Rectangle2D.Double(x, y, bounds.getWidth(), bounds.getHeight()); g2.draw(rect); }
protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); int w = getWidth(); int h = getHeight(); double xScale = (w - 2 * PAD) / (xMax - xMin); double yScale = (h - 2 * PAD) / (yMax - yMin); if (firstTime) System.out.printf("xScale = %.1f yScale = %.1f%n", xScale, yScale); Point2D.Double origin = new Point2D.Double(); // Axes origin. Point2D.Double offset = new Point2D.Double(); // Locate data. if (xMax < 0) { origin.x = w - PAD; offset.x = origin.x - xScale * xMax; } else if (xMin < 0) { origin.x = PAD - xScale * xMin; offset.x = origin.x; } else { origin.x = PAD; offset.x = PAD - xScale * xMin; } if (yMax < 0) { origin.y = h - PAD; offset.y = origin.y - yScale * yMax; } else if (yMin < 0) { origin.y = PAD - yScale * yMin; offset.y = origin.y; } else { origin.y = PAD; offset.y = PAD - yScale * yMin; } if (firstTime) { System.out.printf("origin = [%6.1f, %6.1f]%n", origin.x, origin.y); System.out.printf("offset = [%6.1f, %6.1f]%n", offset.x, offset.y); } // Draw abcissa. g2.draw(new Line2D.Double(PAD, origin.y, w - PAD, origin.y)); // Draw ordinate. g2.draw(new Line2D.Double(origin.x, PAD, origin.x, h - PAD)); g2.setPaint(Color.red); // Mark origin. g2.fill(new Ellipse2D.Double(origin.x - 2, origin.y - 2, 4, 4)); // Plot data. g2.setPaint(Color.blue); for (int i = 0; i < x.length; i++) { double x1 = offset.x + xScale * x[i]; double y1 = offset.y + yScale * y[i]; if (firstTime) System.out.printf("i = %d x1 = %6.1f y1 = %.1f%n", i, x1, y1); g2.fill(new Ellipse2D.Double(x1 - 2, y1 - 2, 4, 4)); g2.drawString(String.valueOf(i), (float) x1 + 3, (float) y1 - 3); } // Draw extreme data values. g2.setPaint(Color.black); Font font = g2.getFont(); FontRenderContext frc = g2.getFontRenderContext(); LineMetrics lm = font.getLineMetrics("0", frc); String s = String.format("%.1f", xMin); float width = (float) font.getStringBounds(s, frc).getWidth(); double x = offset.x + xScale * xMin; g2.drawString(s, (float) x, (float) origin.y + lm.getAscent()); s = String.format("%.1f", xMax); width = (float) font.getStringBounds(s, frc).getWidth(); x = offset.x + xScale * xMax; g2.drawString(s, (float) x - width, (float) origin.y + lm.getAscent()); s = String.format("%.1f", yMin); width = (float) font.getStringBounds(s, frc).getWidth(); double y = offset.y + yScale * yMin; g2.drawString(s, (float) origin.x + 1, (float) y + lm.getAscent()); s = String.format("%.1f", yMax); width = (float) font.getStringBounds(s, frc).getWidth(); y = offset.y + yScale * yMax; g2.drawString(s, (float) origin.x + 1, (float) y); if (firstTime) System.out.println("------------------------------"); firstTime = false; }