protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); g2d.setColor(Color.RED); g2d.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight()); g2d.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2); g2d.setFont(font); FontRenderContext frc = g2d.getFontRenderContext(); GlyphVector gv = font.createGlyphVector(frc, "Cat"); Rectangle2D box = gv.getVisualBounds(); int y = (int)(((getHeight() - box.getHeight()) / 2d) + (-box.getY())); g2d.drawString("Cat", x, y); x += box.getWidth(); y = (int)(((getHeight() - box.getHeight()) / 2d) + (-box.getY())); g2d.drawString("Dog", x, y); g2d.dispose();
JFrame f = new JFrame("LowerRightFrame"); f.add(new JPanel() { f.pack(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice defaultScreen = ge.getDefaultScreenDevice(); Rectangle rect = defaultScreen.getDefaultConfiguration().getBounds(); int x = (int) rect.getMaxX() - f.getWidth(); int y = (int) rect.getMaxY() - f.getHeight(); f.setLocation(x, y); f.setVisible(true);
/** * Returns the FontBBox value as integer array. * @return the font's bounding box */ public int[] getFontBBoxAsIntArray() { RectangularShape rect = getFontBBox(); return new int[] { (int)Math.floor(rect.getMinX()), (int)Math.floor(rect.getMinY()), (int)Math.ceil(rect.getMaxX()), (int)Math.ceil(rect.getMaxY())}; }
import java.awt.Font; import java.awt.font.FontRenderContext; import java.awt.geom.AffineTransform; ... String text = "Hello World"; AffineTransform affinetransform = new AffineTransform(); FontRenderContext frc = new FontRenderContext(affinetransform,true,true); Font font = new Font("Tahoma", Font.PLAIN, 12); int textwidth = (int)(font.getStringBounds(text, frc).getWidth()); int textheight = (int)(font.getStringBounds(text, frc).getHeight());
public static void main(String args[]) { FrameTestBase t = new FrameTestBase(); t.add(new JComponent() { public void paintComponent(Graphics g) { String str = "hello world!"; int y = 50; FontMetrics fm = g.getFontMetrics(); Rectangle2D rect = fm.getStringBounds(str, g); g.setColor(bgColor); g.fillRect(x, y - fm.getAscent(), (int) rect.getWidth(), (int) rect.getHeight()); g.setColor(textColor); t.setDefaultCloseOperation(EXIT_ON_CLOSE); t.setSize(400, 200); t.setVisible(true);
addMouseListener(mouseAdapter); addMouseMotionListener(mouseAdapter); Floor2 mainPanel = new Floor2(w, h, gridDiv); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Stroke initStroke = g2.getStroke(); g2.setStroke(DRAWING_LINE_STROKE); if (mouseState == MouseState.DRAGGING && drawingLine != null) { g2.setColor(LINE_DRAWING_COLOR); g2.draw(drawingLine); g2.setColor(LINE_COLOR); for (List<Ellipse2D> ellipses : ellipseList) { Point2D p2d1 = new Point2D.Double(ellipses.get(0).getCenterX(), ellipses.get(0).getCenterY()); Point2D p2d2 = new Point2D.Double(ellipses.get(1).getCenterX(), ellipses.get(1).getCenterY()); Line2D line = new Line2D.Double(p2d1, p2d2); g2.draw(line); g.drawImage(bufImage, 0, 0, this); g2.setStroke(initStroke); for (int i = 1; i < grid; i++) { int x = i * divisionSize; g2.drawLine(x, 0, x, getSize().height);
private Color linec = Color.GRAY; @Override public void paintIcon(Component c, Graphics g, int x, int y) { Graphics2D g2 = (Graphics2D)g.create(); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.translate(x, y); g2.setStroke(new BasicStroke(a)); g2.setPaint(linec); g2.draw(new RoundRectangle2D.Float(a,a,size-2*a-1,size-2*a-1,r,r)); g2.setStroke(new BasicStroke(b)); g2.setColor(UIManager.getColor("Panel.background")); g2.drawLine(1*f,0*f,1*f,4*f); g2.drawLine(2*f,0*f,2*f,4*f); g2.drawLine(3*f,0*f,3*f,4*f); Rectangle2D b = s.getBounds(); Point2D.Double p = new Point2D.Double( b.getX() + b.getWidth()/2d, b.getY() + b.getHeight()/2d); AffineTransform toCenterAT = AffineTransform.getTranslateInstance( size/2d - p.getX(), size/2d - p.getY()); label.setFont(new Font("Monospace", Font.PLAIN, 24)); JPanel p = new JPanel(); p.add(label); p.setBorder(BorderFactory.createEmptyBorder(8,8,8,8)); return p;
Font font = getFont(); Graphics2D g2d = img.createGraphics(); g2d.setColor(Color.WHITE); g2d.fillRect(0, 0, 1200, 650); int size = rnd.nextInt(37) + 11; Font drawFont = font.deriveFont((float) size); FontMetrics fm = g2d.getFontMetrics(drawFont); Rectangle2D bounds = fm.getStringBounds(word, g2d); System.out.println("Positing " + word); do { int xPos = rnd.nextInt(1200 - (int)bounds.getWidth()); int yPos = rnd.nextInt(650 - (int)bounds.getHeight()); bounds.setFrame(xPos, yPos, bounds.getWidth(), bounds.getHeight()); } while (collision(used, bounds)); g2d.drawString(word, (float)bounds.getX(), (float)bounds.getY() + fm.getAscent()); g2d.setColor(Color.RED); g2d.draw(bounds); boolean collides = false; for (Rectangle2D check : used) { if (bounds.intersects(check)) { collides = true; break;
float dx1, dy1, ang1, dx2, dy2, ang2, ang3; Graphics2D g = (Graphics2D) g2; AffineTransform oldt = g.getTransform(); double height = bounds.getHeight(); double width = bounds.getWidth(); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setStroke(new BasicStroke(2.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL)); g.translate((this.getWidth() - width) / 2, (this.getHeight() - height) / 2); g.translate(-bounds.getX(), -bounds.getY()); g.setColor(Color.BLACK); g.draw(shape); g.setColor(Color.RED); g.draw(new Arc2D.Double(x1 - r, y1 - r, 2 * r, 2 * r, 0, ang1, Arc2D.OPEN)); g.setColor(Color.GREEN); g.draw(new Arc2D.Double(x2 - r, y2 - r, 2 * r, 2 * r, (180 - ang2), ang2, Arc2D.OPEN)); JFrame window = new JFrame(); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setBounds(30, 30, 300, 300); window.getContentPane().add(new MyCanvas()); window.setVisible(true);
JFrame jf = new JFrame("test"); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setSize(800, 800); containerPanel.setLayout(new GridBagLayout()); containerPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(1, 0, 1))); containerPanel.add(zoomPanel); zoomPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); jf.add(new JScrollPane(containerPanel)); jf.setVisible(true); addMouseWheelListener(new MouseWheelListener() { @Override public void mouseWheelMoved(MouseWheelEvent e) { getParent().setLocation(getParent().getLocation().x-offX,getParent().getLocation().y-offY); public void paint(Graphics g) { super.paint(g); g.setColor(Color.red); int w = getWidth(); int h = getHeight(); for (Rectangle2D rect : rects) { r.setRect(rect.getX() * w, rect.getY() * h, rect.getWidth() * w, rect.getHeight() * h); ((Graphics2D)g).draw(r);
g2d.setFont( getFont() ); g2d.setPaint(Color.RED); FontMetrics fm = g2d.getFontMetrics(); Rectangle2D rect = fm.getStringBounds(text, g2d); rect.setRect(rect.getX() + 100, rect.getY() + 50, rect.getWidth(), rect.getHeight()); g2d.draw(rect); g2d.setPaint(Color.BLACK); FontRenderContext frc = g2d.getFontRenderContext(); TextLayout layout = new TextLayout(text, getFont(), frc); layout.draw(g2d, (float)loc.getX(), (float)loc.getY()); bounds.setRect(bounds.getX()+loc.getX(), bounds.getY()+loc.getY(), bounds.getWidth(), bounds.getHeight()); g2d.draw(bounds); JFrame frame = new JFrame("SSCCE"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add( text ); frame.setSize(400, 200); frame.setLocationByPlatform( true ); frame.setVisible( true );
Rectangle2D rect = new Rectangle2D.Double(x, y, width, height); Graphics2D g2d = (Graphics2D) g.create(); g2d.draw(rect); g2d.draw(line); g2d.dispose(); rectangle.getX(), rectangle.getY(), rectangle.getX() + rectangle.getWidth(), rectangle.getY())); rectangle.getX(), rectangle.getY() + rectangle.getHeight(), rectangle.getX() + rectangle.getWidth(), rectangle.getY() + rectangle.getHeight())); rectangle.getX(), rectangle.getY(), rectangle.getX(), rectangle.getY() + rectangle.getHeight())); rectangle.getX() + rectangle.getWidth(), rectangle.getY(), rectangle.getX() + rectangle.getWidth(), rectangle.getY() + rectangle.getHeight()));
g2.setColor(Color.RED); g2.fill(red); g2.draw(red); g2.setColor(Color.WHITE); g2.fill(white); g2.draw(white); contentPane = this.getContentPane(); contentPane.add(MainPane); MainPane.setLayout(null); MainPane.addMouseListener(this); MainPane.addMouseMotionListener(this); if (el.contains(x, y)) { selected = el; offSet = new Point2D.Double(x - el.getX(), y - el.getY()); break; bounds.setFrame(new Rectangle2D.Double(x, y, bounds.getWidth(), bounds.getHeight())); selected.setFrame(bounds); repaint();
BufferedImage.TYPE_INT_ARGB); Graphics2D g = textImage.createGraphics(); FontRenderContext frc = g.getFontRenderContext(); Font font = new Font(Font.SANS_SERIF, Font.BOLD, 250); GlyphVector gv = font.createGlyphVector(frc, "Cat"); Rectangle2D box = gv.getVisualBounds(); int xOff = 25+(int)-box.getX(); int yOff = 80+(int)-box.getY(); Shape shape = gv.getOutline(xOff,yOff); g.setClip(shape); g.drawImage(originalImage,0,0,null); g.setClip(null); g.setStroke(new BasicStroke(2f)); g.setColor(Color.BLACK); g.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
private static void createAndShowUI() { final TestPanel panel = new TestPanel(); panel.validate(); JFrame frame = new JFrame("TestBall"); frame.getContentPane().add(panel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(new Dimension(300, 300)); frame.setLocationRelativeTo(null); frame.setVisible(true); this.addMouseListener(new MouseAdapter() { if (ball.contains(e.getPoint())) { System.out.println("hit the ball"); g2d.setColor(Color.RED); g2d.fill(ball); Rectangle ballBounds = ball.getBounds(); ball.setFrame(ballBounds); ballBounds.x = rand.nextInt(getWidth() - BALL_SIZE + 1) + 1; ballBounds.y = rand.nextInt(getHeight() - BALL_SIZE + 1) + 1; ball.setFrame(ballBounds); repaint();
g.setFont(getFont().deriveFont(20F)); Rectangle2D bigBounds = g.getFontMetrics().getStringBounds(big, g); g.drawString(big, (this.getWidth() - (int)bigBounds.getWidth()) / 2, (this.getHeight() - (int)bigBounds.getHeight()) / 2); g.setFont(getFont().deriveFont(10F)); Rectangle2D littleBounds = g.getFontMetrics().getStringBounds(little, g); g.drawString(little, (this.getWidth() - (int)littleBounds.getWidth()) / 2, (this.getHeight() + (int)littleBounds.getHeight()) / 2); JButton button = new JButton(icon); JPopupMenu popup = new JPopupMenu(); popup.add(button); popup.setBorderPainted(false); popup.setLightWeightPopupEnabled(false); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true);
JFrame f = new JFrame("Test"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new JPanel() { private static final int N = 8; private final Ellipse2D ellipse = new Ellipse2D.Float(); super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); g2d.setStroke(new BasicStroke(N)); ellipse.setFrame(N, N, getWidth() - 2 * N, getHeight() - 2 * N); g2d.draw(ellipse); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true);
addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { shapesLock.lock(); try { Rectangle containerBounds = getBounds(); containerBounds.setLocation(0, 0); Iterator<Ellipse2D> it = shapes.iterator(); Ellipse2D shape = it.next(); Rectangle2D bounds = shape.getBounds2D(); double y = bounds.getY(); Double delta = deltas.get(shape); if (delta == null) { shape.setFrame(bounds.getX(), y, bounds.getWidth(), bounds.getHeight()); if (containerBounds.contains(shape.getBounds())) { delta = Math.min(delta + 0.25, 6d); deltas.put(shape, delta); shapesLock.unlock(); repaint(); Thread.sleep(40); } catch (InterruptedException ex) { g2d.dispose();
JFrame f = new JFrame(); f.add(new JComponent() { public void paintComponent(Graphics g) { g.setColor(Color.BLUE); g.fillOval(centerX-ovalWidth/2, centerY-ovalHeight/2, ovalWidth, ovalHeight); FontMetrics fm = g.getFontMetrics(); double textWidth = fm.getStringBounds(text, g).getWidth(); g.setColor(Color.WHITE); g.drawString(text, (int) (centerX - textWidth/2), f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(300, 300); f.setVisible(true);
super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; Rectangle2D r1 = getBounds(); int margin = 50; Rectangle2D r2 = new Rectangle2D.Double( r1.getMinX()+margin, r1.getMinY()+margin, r1.getWidth()-2*margin, r1.getHeight()-2*margin); g2d.setColor(Color.YELLOW); g2d.fill(r1); g2d.clip(p); g2d.setColor(Color.BLACK); for (double d = 0; d < r1.getWidth() + r1.getHeight(); d += spacing) g2d.draw(line); clipper.setPreferredSize(new Dimension(300, 200)); JFrame frame = new JFrame("HoleClipper"); frame.setContentPane(clipper); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true);