下面列出了java.awt.Graphics2D#setRenderingHints ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void testGraphics2D() throws Exception {
ClientMapInfo mapInfo = new ClientMapInfo();
MapRasterizingInfo mapRasterizingInfo = new MapRasterizingInfo();
mapRasterizingInfo.setBounds(new Bbox(-80, -50, 100, 100));
mapInfo.setCrs("EPSG:4326");
mapRasterizingInfo.setScale(1);
mapRasterizingInfo.setTransparent(true);
mapInfo.getWidgetInfo().put(MapRasterizingInfo.WIDGET_KEY, mapRasterizingInfo);
ClientVectorLayerInfo clientBeansPointLayerInfo = new ClientVectorLayerInfo();
clientBeansPointLayerInfo.setServerLayerId(layerBeansPoint.getId());
VectorLayerRasterizingInfo layerRasterizingInfo = new VectorLayerRasterizingInfo();
layerRasterizingInfo.setStyle(layerBeansPointStyleInfo);
clientBeansPointLayerInfo.getWidgetInfo().put(VectorLayerRasterizingInfo.WIDGET_KEY, layerRasterizingInfo);
mapInfo.getLayers().add(clientBeansPointLayerInfo);
BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
RenderingHints renderingHints = new Hints();
renderingHints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Graphics2D graphics2d = image.createGraphics();
graphics2d.setRenderingHints(renderingHints);
imageService.writeMap(graphics2d, mapInfo);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
imageService.writeMap(baos, mapInfo);
BufferedImage image2 = ImageIO.read(new ByteArrayInputStream(baos.toByteArray()));
Assert.assertArrayEquals(image2.getRGB(0, 0, 100, 100, null, 0, 100),image.getRGB(0, 0, 100, 100, null, 0, 100));
}
/**
*
* @param g2d
*/
protected void paintInit(Graphics2D g2d) {
g2d.setClip(leftMargin, topMargin, (int) graphWidth, (int) graphHeight);
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);
g2d.setPaint(Color.BLACK);
g2d.setStroke(new BasicStroke(1.0f));
g2d.setFont(new Font(
"SansSerif",
Font.BOLD,
10));
}
@Override
protected AbstractButton buildButton() {
return new JButton() {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Gradient gradient = getGradient();
if (gradient != null) {
Rectangle r = new Rectangle(0, 0, getWidth(), getHeight());
r.grow(-3, -3);
Graphics2D g2 = (Graphics2D) g;
RenderingHints rh = g2.getRenderingHints();
g2.setRenderingHints(edu.xtec.jclic.Constants.DEFAULT_RENDERING_HINTS);
gradient.paint(g2, r);
g2.setRenderingHints(rh);
}
}
};
}
/**
* Configure a Graphics2D with this context.
* <p>
* This adds to (but does not replace) the incoming Graphics2D's existing
* transform and clipping. All other attributes (rendering hints, composite,
* etc) are replaced.
*/
public void install(Graphics2D g) {
g.setRenderingHints(renderingHints);
g.setColor(getColor());
g.setPaint(getPaint());
g.setStroke(stroke);
g.setFont(font);
if (backgroundColor != null)
g.setBackground(backgroundColor);
if (clip != null) {
g.clip(clip);
}
AffineTransform tx = g.getTransform();
tx.concatenate(getTransform());
g.setTransform(tx);
if (xorColor != null) {
g.setXORMode(xorColor);
} else {
g.setPaintMode();
g.setComposite(getComposite());
}
}
@Override
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Rectangle rBounds = new Rectangle(0, 0, getWidth(), getHeight());
if (!isOpaque() || actBgGradient == null || actBgGradient.hasTransparency())
super.paintComponent(g);
RenderingHints rh = g2.getRenderingHints();
if (isOpaque() && actBgGradient != null) {
g2.setRenderingHints(edu.xtec.jclic.Constants.DEFAULT_RENDERING_HINTS);
actBgGradient.paint(g2, rBounds);
}
if (getActivityEditor() != null)
getActivityEditor().drawPreview(g2, rBounds, marginInt.getValue());
g2.setRenderingHints(rh);
}
/**
* Draws the annotation icon on operators.
*/
private void draw(final Operator operator, final Graphics2D g2, final ProcessRendererModel rendererModel,
final boolean printing) {
// Draw annotation icons only if hidden
if (visualizer.isActive()) {
return;
}
WorkflowAnnotations annotations = rendererModel.getOperatorAnnotations(operator);
if (annotations == null || annotations.isEmpty()) {
return;
}
Rectangle2D frame = rendererModel.getOperatorRect(operator);
int iconSize = 16;
int xOffset = (iconSize + 2) * 2;
int iconX = (int) (frame.getX() + frame.getWidth() - xOffset);
int iconY = (int) (frame.getY() + frame.getHeight() - iconSize - 1);
ImageIcon icon = ProcessDrawUtils.getIcon(operator, rendererModel.getZoomFactor() <= 1d ? IMAGE_ANNOTATION : IMAGE_ANNOTATION_ZOOMED);
RenderingHints originalRenderingHints = g2.getRenderingHints();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(icon.getImage(), iconX, iconY, iconSize, iconSize, null);
g2.setRenderingHints(originalRenderingHints);
}
/**
*
* @param g2d
*/
public void paint ( Graphics2D g2d ) {
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 );
g2d.setColor( Color.black );
DrawBounds( g2d );
// paint valuesliders
for (Component vms : getComponents()) {
if ( vms instanceof JPanel ) {
vms.repaint();
}
}
}
/**
*
* @param g2d
*/
protected void paintInit(Graphics2D g2d) {
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);
g2d.setPaint(Color.BLACK);
g2d.setStroke(new BasicStroke(1.0f));
g2d.setFont(ReduxConstants.sansSerif_12_Bold);
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D g2d = (Graphics2D)g.create();
RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHints(rh);
g2d.setColor(color);
g2d.translate(x - shape.getBounds().x, y - shape.getBounds().y);
g2d.fill(shape);
g2d.dispose();
}
/**
*
* @param g2d
*/
public void paint(Graphics2D g2d) {
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);
// bottom border
g2d.setColor(Color.black);
g2d.drawLine(0, getBounds().height - 1, getWidth() - 1, getBounds().height - 1);
}
/**
* If a background color was provided: this paints that background. This
* method is not capable of painting the scaled image, though.
*
*/
public void paint(Graphics2D g, int x, int y, int width, int height) {
if (color != null) {
g.setColor(color);
g.setRenderingHints(Thumbnail.qualityHints);
if (curvature == 0) {
g.fillRect(x, y, width, height);
} else {
RoundRectangle2D r = new RoundRectangle2D.Float(x, y,
width, height, curvature, curvature);
g.fill(r);
}
}
}
/**
* Draws the image and the grid.
*
* @param panel
* @param g
*/
public void draw(DrawingPanel panel, Graphics g) {
if (!visible) {
return;
}
if(dirtyImage){
image = Toolkit.getDefaultToolkit().createImage(imageSource);
}
if (image == null) {
panel.setMessage(DisplayRes.getString("Null Image")); //$NON-NLS-1$
return;
}
Graphics2D g2 = (Graphics2D) g;
AffineTransform gat = g2.getTransform(); // save graphics transform
RenderingHints hints = g2.getRenderingHints();
if (!OSPRuntime.isMac()) { // Rendering hint bug in Mac Snow Leopard
g2.setRenderingHint(RenderingHints.KEY_DITHERING,
RenderingHints.VALUE_DITHER_DISABLE);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_OFF);
}
double sx = (xmax - xmin) * panel.xPixPerUnit / ncol;
double sy = (ymax - ymin) * panel.yPixPerUnit / nrow;
// translate origin to pixel location of (xmin,ymax)
g2.transform(AffineTransform.getTranslateInstance(panel.leftGutter
+ panel.xPixPerUnit * (xmin - panel.xmin), panel.topGutter
+ panel.yPixPerUnit * (panel.ymax - ymax)));
g2.transform(AffineTransform.getScaleInstance(sx, sy)); // scales image to world units
g2.drawImage(image, 0, 0, panel);
g2.setTransform(gat); // restore graphics conext
g2.setRenderingHints(hints); // restore the hints
}
/**
* SCIPIO: Simple copy of a source image to a destination buffered image using
* {@link java.awt.Graphics#drawImage}.
* <p>
* WARN/FIXME: Graphics2D.drawImage appears to ignore RenderingHints.KEY_DITHERING and always applies dithering!
* <p>
* Added 2017-07-14.
* <p>
* @return the destImage
*/
public static BufferedImage copyToBufferedImageAwt(Image srcImage, BufferedImage destImage, RenderingHints renderingHints) {
Graphics2D g = destImage.createGraphics();
try {
if (renderingHints != null) g.setRenderingHints(renderingHints);
g.drawImage(srcImage, 0, 0, null);
} finally { // SCIPIO: added finally
g.dispose();
}
return destImage;
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHints(new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON));
g2.setColor(type.getColor());
g2.fillOval(x, y, SIZE, SIZE);
g2.dispose();
}
ProjectMetagonEditGrammarIconImage(ProjectMetagon pm,int span){
super(span,span,BufferedImage.TYPE_INT_RGB);
//init image
Path2D path=pm.getImagePath();
Graphics2D g=createGraphics();
g.setRenderingHints(UI.RENDERING_HINTS);
//fill background
g.setColor(UI.ELEMENTMENU_ICONBACKGROUND);
g.fillRect(0,0,span,span);
//glean metrics and transform
Rectangle2D pbounds=path.getBounds2D();
double pw=pbounds.getWidth(),ph=pbounds.getHeight(),scale;
int maxpolygonimagespan=span-(UI.ELEMENTMENU_ICONGEOMETRYINSET*2);
scale=(pw>ph)?maxpolygonimagespan/pw:maxpolygonimagespan/ph;
AffineTransform t=new AffineTransform();
t.scale(scale,-scale);//note y flip
double
xoffset=-pbounds.getMinX()+(((span-(pw*scale))/2)/scale),
yoffset=-pbounds.getMaxY()-(((span-(ph*scale))/2)/scale);
t.translate(xoffset,yoffset);
g.transform(t);
//fill metagon
//use color to distinguish protojig counts
int pjcount=pm.getJigCount();
if(pjcount<UI.GRAMMAR_EDITOR_METAGON_ICONS_FILLCOLOR.length)
g.setColor(UI.GRAMMAR_EDITOR_METAGON_ICONS_FILLCOLOR[pjcount]);
else
g.setColor(UI.GRAMMAR_EDITOR_METAGON_ICONS_FILLCOLOR[UI.GRAMMAR_EDITOR_METAGON_ICONS_FILLCOLOR.length-1]);
g.fill(path);
//stroke it
g.setColor(UI.ELEMENTMENU_ICON_STROKE);
g.setStroke(new BasicStroke(
(float)(UI.ELEMENTMENU_ICONPATHSTROKETHICKNESS/scale),
BasicStroke.CAP_SQUARE,
BasicStroke.JOIN_ROUND,
0,null,0));
g.draw(path);}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
RenderingHints rh = g2.getRenderingHints();
g2.setRenderingHints(edu.xtec.jclic.Constants.DEFAULT_RENDERING_HINTS);
gradient.paint(g2, new Rectangle(0, 0, getWidth(), getHeight()));
g2.setRenderingHints(rh);
}
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
int width = 150;
int height = 50;
String test = FortuneInfo.getInfo();
char data[][] = {
{ 'a', 'n', 'd', 'r', 'o', 'i', 'd' },
{ 'c', 'l', 'o', 'u', 'd' },
{ 'g', 'o', 'o', 'g', 'l', 'e' },
{ 'd', 'o', 'c', 'k', 'e', 'r' },
{ 'g', 'o', 'l', 'a', 'n', 'g' },
{ 'p', 'y', 't', 'h', 'o', 'n' },
{ 'c', 'o', 'm', 'p', 'u', 't', 'e' },
{ 'm', 'a', 'p', 's', 'a', 'p', 'i' }
};
BufferedImage bufferedImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bufferedImage.createGraphics();
Font font = new Font("Georgia", Font.BOLD, 18);
g2d.setFont(font);
RenderingHints rh = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
rh.put(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHints(rh);
GradientPaint gp = new GradientPaint(0, 0,
Color.red, 0, height/2, Color.black, true);
g2d.setPaint(gp);
g2d.fillRect(0, 0, width, height);
g2d.setColor(new Color(255, 153, 0));
Random r = new Random();
int index = Math.abs(r.nextInt()) % 5;
String captcha = String.copyValueOf(data[index]);
request.getSession().setAttribute("captcha", captcha );
int x = 0;
int y = 0;
for (int i=0; i<data[index].length; i++) {
x += 10 + (Math.abs(r.nextInt()) % 15);
y = 20 + Math.abs(r.nextInt()) % 20;
g2d.drawChars(data[index], i, 1, x, y);
}
g2d.dispose();
response.setContentType("image/png");
OutputStream os = response.getOutputStream();
ImageIO.write(bufferedImage, "png", os);
os.close();
}
/**
*
* @param g2d
*/
public void paint(Graphics2D g2d) {
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);
g2d.setColor(Color.BLACK);
g2d.setFont(textFont);
g2d.drawString(text, 1, 11);//12);
g2d.setColor(Color.gray);
// g2d.drawLine(0, 0, getWidth(), 0);
}
private void applyRenderingHints(Graphics2D g) {
if (renderingHints != null) g.setRenderingHints(renderingHints);
}
@Deprecated
public static void createThumbsForDoc(TrpDoc doc, boolean overwrite) throws Exception {
checkIfLocalDoc(doc);
File thmbsDir = new File(doc.getMd().getLocalFolder().getAbsolutePath() + File.separator + LocalDocConst.THUMBS_FILE_SUB_FOLDER);
FileUtils.forceMkdir(thmbsDir);
int newHeight = LocalDocConst.THUMB_SIZE_HEIGHT;
for (TrpPage p : doc.getPages()) {
File imgFile = FileUtils.toFile(p.getUrl());
if (imgFile == null)
throw new IOException("Cannot retrieve image url from: "+p.getUrl());
File thumbsFile = FileUtils.toFile(p.getThumbUrl());
if (thumbsFile == null)
throw new IOException("Cannot retrieve thumbs url from: "+p.getThumbUrl());
if (thumbsFile.exists() && !overwrite) // skip if already there and overwrite not specified
continue;
logger.debug("creating thumb file: "+thumbsFile);
long st = System.currentTimeMillis();
if (true) {
}
if (false) {
BufferedImage originalImage = ImgUtils.readImage(imgFile);
if (originalImage==null)
throw new IOException("Cannot load image "+imgFile.getAbsolutePath());
double sf = (double)newHeight / (double)originalImage.getHeight();
int newWidth = (int)(sf * originalImage.getWidth());
BufferedImage thmbImg = new BufferedImage(newWidth, newHeight, originalImage.getType());
Graphics2D g = thmbImg.createGraphics();
RenderingHints rh = new RenderingHints(
RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g.setRenderingHints(rh);
g.drawImage(originalImage, 0, 0, newWidth, newHeight, null);
g.dispose();
// logger.debug("thmbImg: "+originalImage+ " size: "+thmbImg.getWidth()+"x"+thmbImg.getHeight());
if (!ImageIO.write(thmbImg, FilenameUtils.getExtension(thumbsFile.getName()), thumbsFile))
throw new Exception("Could not write thumb file - no appropriate writer found!");
}
logger.debug("created thumb file: "+thumbsFile.getAbsolutePath()+" time = "+(System.currentTimeMillis()-st));
}
}