private int padding = 25; private int labelPadding = 25; private Color lineColor = new Color(44, 102, 230, 180); private Color pointColor = new Color(100, 100, 100, 180); private Color gridColor = new Color(200, 200, 200, 200); private static final Stroke GRAPH_STROKE = new BasicStroke(2f); private int pointWidth = 4; private int numberYDivisions = 10; super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(Color.WHITE); g2.fillRect(padding + labelPadding, padding, getWidth() - (2 * padding) - labelPadding, getHeight() - 2 * padding - labelPadding); g2.setColor(Color.BLACK); FontMetrics metrics = g2.getFontMetrics(); int labelWidth = metrics.stringWidth(yLabel); g2.drawString(yLabel, x0 - labelWidth - 5, y0 + (metrics.getHeight() / 2) - 3); g2.drawLine(x0, y0, x1, y1); g2.drawLine(x0, y0, x1, y1); invalidate(); this.repaint();
JFrame frame = new JFrame ( "Bevel Arrows" ); g.setRenderingHint ( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ); g.setStroke ( new BasicStroke ( 20.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL ) ); g.translate ( 0, 60 ); arrow.draw ( g ); g.setStroke ( new BasicStroke ( 20.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER ) ); g.setStroke ( new BasicStroke ( 20.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND ) ); switch ( stroke.getLineJoin() ) { case BasicStroke.JOIN_BEVEL: veeX = endX - stroke.getLineWidth() * 0.25f; break; default: case BasicStroke.JOIN_MITER: veeX = endX - stroke.getLineWidth() * 0.5f / arrowRatio; break; case BasicStroke.JOIN_ROUND: veeX = endX - stroke.getLineWidth() * 0.5f; break; g.draw ( new Line2D.Float ( 50.0f, 0.0f, veeX - stroke.getLineWidth() * 0.25f, 0.0f ) ); float veeX = endX - stroke.getLineWidth() * 0.5f / arrowRatio;
protected void applyStroke(SimpleShape<?,?> shape) { if (_stroke instanceof BasicStroke){ BasicStroke bs = (BasicStroke)_stroke; shape.setStrokeStyle((double)bs.getLineWidth()); float[] dash = bs.getDashArray(); if (dash != null) { //TODO: implement more dashing styles shape.setStrokeStyle(StrokeStyle.LineDash.DASH); } } }
public Shape createStrokedShape (Shape shape) { GeneralPath result = new GeneralPath(); shape = new BasicStroke(getWidth(), BasicStroke.CAP_SQUARE, getJoin()).createStrokedShape(shape); PathIterator it = new FlatteningPathIterator(shape.getPathIterator(null), FLATNESS); float points[] = new float[6];
_graphics.setColor(fillColor); _graphics.fill(shape); BasicStroke stroke = new BasicStroke((float) la.getLineThickness()); double[] linePattern = la.getLinePattern(); if (linePattern != null) { dash[i] = (float) la.getLinePattern()[i]; stroke = new BasicStroke(stroke.getLineWidth(), BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10f, dash, 0f); _graphics.setStroke(stroke); if (la.getLineColor() != null || fillColor != null) { lineColor = la.getLineColor();
BasicStroke basicStroke = new BasicStroke(2.0f); public CButton(String txt) { super(txt); Graphics2D g2d = (Graphics2D) g.create(); g2d.setColor(new Color(0xFFAA00)); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setStroke(basicStroke); g2d.drawRoundRect(3, 3, getWidth()-4, getHeight()-4, archH, archH); setLayout(new GridBagLayout()); add(usrNameLabel, labCnst); add(usrNameFeild, txtCnst); Rectangle2D txRect = new Rectangle2D.Double(0, 0, textureImg.getWidth(), textureImg.getHeight()); TexturePaint txPaint = new TexturePaint(textureImg, txRect); g2d.setPaint(txPaint); JFrame frame = new JFrame("Demo: LogIn Dialogue"); frame.setSize(new Dimension(500, 300)); MainContainer container = new MainContainer();
private static final Color POINTS_COLOR = Color.red; private static final Color CURRENT_POINTS_COLOR = Color.blue; private static final Stroke STROKE = new BasicStroke(4f); private List<List<Point>> pointsList = new ArrayList<List<Point>>(); private List<Point> currentPointList = null; int x2 = p2.x; int y2 = p2.y; g.drawLine(x1, y1, x2, y2); p1 = p2; g.setColor(CURRENT_POINTS_COLOR); if (currentPointList != null && currentPointList.size() > 1) { Point p1 = currentPointList.get(0); JFrame frame = new JFrame("DrawStuff"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new DrawStuff()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true);
private static final Color BACKGROUND = new Color(0, 0, 0, 0); private static final Color INITIAL_COLOUR = Color.BLACK; private static final int BORDER_WIDTH = 5; private static final Color LINE_DRAWING_COLOR = new Color(200, 200, 255); private static final Color LINE_COLOR = Color.blue; private static final Stroke DRAWING_LINE_STROKE = new BasicStroke((float)BORDER_WIDTH); public static final int ELLIPSE_DIAMETER = 10; private MouseState mouseState = MouseState.IDLE; 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()); g.drawImage(bufImage, 0, 0, this); g2.setStroke(initStroke);
private void renderPoly(Graphics2D graphics, Color color, Polygon polygon) { if (polygon != null) { graphics.setColor(color); graphics.setStroke(new BasicStroke(2)); graphics.draw(polygon); graphics.setColor(new Color(color.getRed(), color.getGreen(), color.getBlue(), 20)); graphics.fill(polygon); } } }
private static final int BORDER_GAP = 30; private static final Color GRAPH_COLOR = Color.green; private static final Color GRAPH_POINT_COLOR = new Color(150, 50, 50, 180); private static final Stroke GRAPH_STROKE = new BasicStroke(3f); private static final int GRAPH_POINT_WIDTH = 12; private static final int Y_HATCH_CNT = 10; super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.drawLine(BORDER_GAP, getHeight() - BORDER_GAP, BORDER_GAP, BORDER_GAP); g2.drawLine(BORDER_GAP, getHeight() - BORDER_GAP, getWidth() - BORDER_GAP, getHeight() - BORDER_GAP); int y0 = getHeight() - (((i + 1) * (getHeight() - BORDER_GAP * 2)) / Y_HATCH_CNT + BORDER_GAP); int y1 = y0; g2.drawLine(x0, y0, x1, y1); Stroke oldStroke = g2.getStroke(); g2.setColor(GRAPH_COLOR); g2.setStroke(GRAPH_STROKE); for (int i = 0; i < graphPoints.size() - 1; i++) { int x1 = graphPoints.get(i).x;
private static final int PREF_W = 400; private static final int PREF_H = PREF_W; private static final Stroke BASIC_STROKE = new BasicStroke(6f); BufferedImage backgroundImage; BufferedImage overlayImage; int w = PREF_W - 2 * x; int h = w; g2.drawOval(x, y, w, h); g2.dispose(); return img; BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = img.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setStroke(BASIC_STROKE); JFrame frame = new JFrame("TestAlphaComposite"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new TestAlphaComposite()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true);
antialiasSetting = RenderingHints.VALUE_ANTIALIAS_ON; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antialiasSetting); g2.setColor(Color.white); g2.fillRect(0, 0, image.getWidth()+10, image.getHeight()+10); g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND)); translate.setToTranslation(offset, offset); shadow.transform(translate); g2.setColor(new Color(150,150,150)); g2.fill(shadow); new BasicStroke( strokeWeight, new BasicStroke( strokeWeight, BasicStroke.CAP_BUTT, new BasicStroke( 1, BasicStroke.CAP_ROUND,
private static final int BI_WIDTH = 600; private static final int BI_HEIGHT = BI_WIDTH; private static final Color LABEL_DRAW_COLOR = new Color(180, 180, 255); private static final Stroke LABEL_DRAW_STROKE = new BasicStroke(1); private static final Stroke BIMAGE_DRAW_STROKE = new BasicStroke(4); private static final int COLOR_DIV = 5; private BufferedImage bImage = new BufferedImage(BI_WIDTH, BI_HEIGHT, g2d.setBackground(Color.white); g2d.clearRect(0, 0, BI_WIDTH, BI_HEIGHT); g2d.dispose(); Color c = new Color((r * 255) / COLOR_DIV, (g * 255) / COLOR_DIV, (b * 255) / COLOR_DIV); colorList.add(c); btnPanel.add(saveImageBtn); btnPanel.add(clearImageBtn); setLayout(new BorderLayout()); g2d.setStroke(LABEL_DRAW_STROKE); if (pointList.size() < 2) { return;
int height = getHeight(); int shadowGap = this.shadowGap; Color shadowColorA = new Color(shadowColor.getRed(), shadowColor.getGreen(), shadowColor.getBlue(), shadowAlpha); Graphics2D graphics = (Graphics2D) g; graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); graphics.setColor(shadowColorA); graphics.fillRoundRect( graphics.fillRoundRect(0, 0, width - shadowGap, height - shadowGap, arcs.width, arcs.height); graphics.setColor(getForeground()); graphics.setStroke(new BasicStroke(strokeSize)); graphics.drawRoundRect(0, 0, width - shadowGap, height - shadowGap, arcs.width, arcs.height); graphics.setStroke(new BasicStroke());
JFrame frame = new JFrame(); frame.setLayout(new GridLayout(N, N, N, N)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); for (int i = 0; i < N * N; i++) { frame.add(new RotatePanel()); frame.pack(); frame.setVisible(true); super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.translate(this.getWidth() / 2, this.getHeight() / 2); g2d.rotate(theta); g2d.translate(-image.getWidth(this) / 2, -image.getHeight(this) / 2); g2d.drawImage(image, 0, 0, null); RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setPaint(Color.getHSBColor(r.nextFloat(), 1, 1)); g2d.setStroke(new BasicStroke(size / 8)); g2d.drawLine(0, size / 2, size, size / 2); g2d.drawLine(size / 2, 0, size / 2, size); g2d.dispose(); return bi;
private void drawImage( SwingUniversalImage img, int locationX, int locationY, int imageSize ) { if ( isDrawingPixelatedImages() && img.isBitmap() ) { BufferedImage bi = new BufferedImage( imageSize, imageSize, BufferedImage.TYPE_INT_ARGB ); Graphics2D g2 = (Graphics2D) bi.getGraphics(); g2.setColor( Color.WHITE ); g2.fillRect( 0, 0, imageSize, imageSize ); g2.drawImage( img.getAsBitmapForSize( imageSize, imageSize ), 0, 0, observer ); g2.dispose(); for ( int x = 0; x < bi.getWidth( observer ); x++ ) { for ( int y = 0; y < bi.getHeight( observer ); y++ ) { int rgb = bi.getRGB( x, y ); gc.setColor( new Color( rgb ) ); gc.setStroke( new BasicStroke( 1.0f ) ); gc.drawLine( locationX + xOffset + x, locationY + yOffset + y, locationX + xOffset + x, locationY + yOffset + y ); } } } else { gc.setBackground( Color.white ); gc.clearRect( locationX, locationY, imageSize, imageSize ); img.drawToGraphics( gc, locationX, locationY, imageSize, imageSize ); } }
gr.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); gr.setColor(Color.WHITE); gr.fillRect(0, 0, image.getWidth(), image.getHeight()); gr.setColor(Color.LIGHT_GRAY); gr.setStroke(new BasicStroke(1, 0, 0, 1, new float[] {5, 5}, 0)); gr.draw(new Line2D.Double(0, 300, 600, 300)); gr.draw(new Line2D.Double(300, 0, 300, 600)); at.scale(10, -10); gr.setStroke(new BasicStroke(1)); gr.setColor(Color.LIGHT_GRAY); gr.draw(new LiteShape(boundsPoly, at, false)); gr.setStroke(new BasicStroke(0.5f)); gr.setColor(Color.BLUE); gr.draw(new LiteShape(original, at, false)); if (clipped != null) { gr.setStroke(new BasicStroke(2)); if (clipped instanceof Polygon || clipped instanceof MultiPolygon) { gr.setColor(Color.LIGHT_GRAY); gr.dispose(); JFrame frame = new JFrame(title); frame.setContentPane(new JLabel(new ImageIcon(image))); frame.pack(); frame.setVisible(true);
protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g.create(); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.translate(100, 150); g2.rotate(0.4); g2.setPaint(Color.red); g2.fill(s); g2.setPaint(Color.black); g2.setStroke(new BasicStroke(3, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 1, new float[]{1,0.4f,1.5f}, 0)); g2.draw(s); JFrame f = new JFrame("Test"); Component c = new Test(); f.getContentPane().add(c); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.pack(); f.setVisible(true);
new Color(203, 203, 197), new Color(192, 142, 60) }; static final int WHITE = 0, BLACK = 1; imageShapeArea.subtract(shapeArea); ArrayList<Shape> regions = separateShapeIntoRegions(imageShapeArea); g.setStroke(new BasicStroke(1)); g.setColor(pieceColors[side]); Color baseColor = pieceColors[side]; if (gradient) { Color c1 = baseColor.brighter(); Color c2 = baseColor; GradientPaint gp = new GradientPaint( g.setColor(baseColor); g.setColor(outlineColor); l.setBackground(bg); l.setOpaque(true); c.add(l); l.setBackground(bg); l.setOpaque(true); c.add(l);
public static void printTree(Cluster a,String file) { int depth = getDepth(a); width = wunit*(depth+1); height = hunit*(depth+1); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g = image .createGraphics(); g.setColor(new Color(0,0,0)); g.setStroke(new BasicStroke(1)); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Font font = new Font("宋体", Font.BOLD, 20); g.setFont(font); drawTree(a, g, width/2, 0 , 1); //释放对象 g.dispose(); // 保存文件 try { ImageIO.write(image, "png", new File(file)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }