本文整理汇总了Java中java.awt.geom.AffineTransform.getScaleInstance方法的典型用法代码示例。如果您正苦于以下问题:Java AffineTransform.getScaleInstance方法的具体用法?Java AffineTransform.getScaleInstance怎么用?Java AffineTransform.getScaleInstance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.awt.geom.AffineTransform
的用法示例。
在下文中一共展示了AffineTransform.getScaleInstance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createBufferedImage
import java.awt.geom.AffineTransform; //导入方法依赖的package包/类
/**
* Creates and returns a buffered image into which the chart has been drawn.
*
* @param imageWidth the image width.
* @param imageHeight the image height.
* @param drawWidth the width for drawing the chart (will be scaled to fit image).
* @param drawHeight the height for drawing the chart (will be scaled to fit image).
* @param info optional object for collection chart dimension and entity information.
*
* @return a buffered image.
*/
public BufferedImage createBufferedImage(int imageWidth, int imageHeight,
double drawWidth, double drawHeight,
ChartRenderingInfo info) {
BufferedImage image = new BufferedImage(
imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB
);
Graphics2D g2 = image.createGraphics();
double scaleX = imageWidth / drawWidth;
double scaleY = imageHeight / drawHeight;
AffineTransform st = AffineTransform.getScaleInstance(scaleX, scaleY);
g2.transform(st);
draw(g2, new Rectangle2D.Double(0, 0, drawWidth, drawHeight), null, info);
g2.dispose();
return image;
}
示例2: drawUnexpanded
import java.awt.geom.AffineTransform; //导入方法依赖的package包/类
/**
* Draw a {@link GamePiece} that is not the top piece in an unexpanded {@link Stack}
*
* Default implementation is a white square with a black border
*/
protected void drawUnexpanded(GamePiece p, Graphics g,
int x, int y, Component obs, double zoom) {
if (blankColor == null) {
p.draw(g, x, y, obs, zoom);
}
else {
Graphics2D g2d = (Graphics2D) g;
g.setColor(blankColor);
Shape s = p.getShape();
AffineTransform t = AffineTransform.getScaleInstance(zoom,zoom);
t.translate(x/zoom,y/zoom);
s = t.createTransformedShape(s);
g2d.fill(s);
g.setColor(Color.black);
g2d.draw(s);
}
}
示例3: run
import java.awt.geom.AffineTransform; //导入方法依赖的package包/类
@Override
public BufferedImage run(final @NonNull BufferedImage image) {
if (isFlippedHorizontally || isFlippedVertically) {
final double scaleX = isFlippedHorizontally ? -1 : 1;
final double scaleY = isFlippedVertically ? -1 : 1;
final double translateX = isFlippedHorizontally ? -image.getWidth() : 0;
final double translateY = isFlippedVertically ? -image.getHeight() : 0;
final AffineTransform tx = AffineTransform.getScaleInstance(scaleX, scaleY);
tx.translate(translateX, translateY);
final BufferedImageOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
return op.filter(image, null);
}
return image;
}
示例4: scaleRect
import java.awt.geom.AffineTransform; //导入方法依赖的package包/类
public static Shape scaleRect(final Rectangle2D shape, final int max) {
Vector2D newDimension = scaleWithRatio(shape.getWidth(), shape.getHeight(), max);
if (newDimension == null) {
return shape;
}
final AffineTransform transform = AffineTransform.getScaleInstance(newDimension.getX(), newDimension.getY());
return transform.createTransformedShape(shape);
}
示例5: main
import java.awt.geom.AffineTransform; //导入方法依赖的package包/类
public static void main(final String[] args) throws IOException {
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsConfiguration gc = ge.getDefaultScreenDevice()
.getDefaultConfiguration();
AffineTransform at;
for (final int size : SIZES) {
for (final int scale : SCALES) {
final int sw = size * scale;
at = AffineTransform.getScaleInstance(sw, sw);
for (Shape clip : SHAPES) {
clip = at.createTransformedShape(clip);
for (Shape to : SHAPES) {
to = at.createTransformedShape(to);
// Prepare test images
VolatileImage vi = getVolatileImage(gc, size);
BufferedImage bi = getBufferedImage(sw);
// Prepare gold images
BufferedImage goldvi = getCompatibleImage(gc, size);
BufferedImage goldbi = getBufferedImage(sw);
draw(clip, to, vi, bi, scale);
draw(clip, to, goldvi, goldbi, scale);
validate(bi, goldbi);
}
}
}
}
}
示例6: run
import java.awt.geom.AffineTransform; //导入方法依赖的package包/类
@Override
public void run() {
BufferedImage img = new BufferedImage(display.getWidth(), display.getHeight(), BufferedImage.TYPE_INT_RGB);
for (int y = 0; y < display.getHeight(); y++) {
for (int x = 0; x < display.getWidth(); x++) {
int idx = getIndex(x, y);
int nIdx = 3 * idx;
int r = ((int) (rgbValues[nIdx++] * 255)); // red component 0...255
int g = ((int) (rgbValues[nIdx++] * 255)); // green component 0...255
int b = ((int) (rgbValues[nIdx] * 255)); // blue component 0...255
int col = (r << 16) | (g << 8) | b;
img.setRGB(x, y, col);
}
}
AffineTransform tx = AffineTransform.getScaleInstance(1, -1); //alligns the image correctly
tx.translate(0, -img.getHeight(null));
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
img = op.filter(img, null);
SimpleDateFormat sdfDate = new SimpleDateFormat("_yyyy_MM_dd_HH_mm_ss");//dd/MM/yyyy
Date now = new Date();
String strDate = sdfDate.format(now);
try {
ImageIO.write(img, "png", new File(String.format("%s/ImageCreator%s.png", path, strDate)));
} catch (IOException ex) {
log.warning("can not save Image, Error: " + ex.toString());
}
}
示例7: scale
import java.awt.geom.AffineTransform; //导入方法依赖的package包/类
/**
* 缩放图片
*
* @param imgPath 源图片绝对路径
* @param disPath 目标图片绝对路径
* @param height 目标高度
* @param width 目标宽度
* @param whiteFill 是否补白
*/
public final static void scale(String imgPath, String disPath, int height, int width, boolean whiteFill) {
try {
double ratio = 0.0;
File file = new File(imgPath);
BufferedImage bufferedImage = ImageIO.read(file);
Image tempImg = bufferedImage.getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH);
// 计算比例
if ((bufferedImage.getHeight() > height) || (bufferedImage.getWidth() > width)) {
double ratioV = (new Integer(height).doubleValue() / bufferedImage.getHeight());
double ratioH = (new Integer(width).doubleValue() / bufferedImage.getWidth());
ratio = Math.max(ratioV, ratioH);
AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
tempImg = op.filter(bufferedImage, null);
}
if (whiteFill) {
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = img.createGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
if (width == tempImg.getWidth(null)) {
g.drawImage(tempImg, 0, (height - tempImg.getHeight(null)) / 2, tempImg.getWidth(null), tempImg.getHeight(null), Color.WHITE, null);
} else {
g.drawImage(tempImg, (width - tempImg.getWidth(null)) / 2, 0, tempImg.getWidth(null), tempImg.getHeight(null), Color.WHITE, null);
}
g.dispose();
tempImg = img;
}
ImageIO.write((BufferedImage)tempImg, "JPEG", new File(disPath));
} catch (IOException e) {
//
e.printStackTrace();
}
}
示例8: textOut
import java.awt.geom.AffineTransform; //导入方法依赖的package包/类
private void textOut(String str,
Font font, PhysicalFont font2D,
FontRenderContext frc,
float deviceSize, int rotation, float awScale,
double scaleFactorX, double scaleFactorY,
float userx, float usery,
float devx, float devy, float targetW) {
String family = font2D.getFamilyName(null);
int style = font.getStyle() | font2D.getStyle();
WPrinterJob wPrinterJob = (WPrinterJob)getPrinterJob();
boolean setFont = wPrinterJob.setFont(family, deviceSize, style,
rotation, awScale);
if (!setFont) {
super.drawString(str, userx, usery, font, frc, targetW);
return;
}
float[] glyphPos = null;
if (!okGDIMetrics(str, font, frc, scaleFactorX)) {
/* If there is a 1:1 char->glyph mapping then char positions
* are the same as glyph positions and we can tell GDI
* where to place the glyphs.
* On drawing we remove control chars so these need to be
* removed now so the string and positions are the same length.
* For other cases we need to pass glyph codes to GDI.
*/
str = wPrinterJob.removeControlChars(str);
char[] chars = str.toCharArray();
int len = chars.length;
GlyphVector gv = null;
if (!FontUtilities.isComplexText(chars, 0, len)) {
gv = font.createGlyphVector(frc, str);
}
if (gv == null) {
super.drawString(str, userx, usery, font, frc, targetW);
return;
}
glyphPos = gv.getGlyphPositions(0, len, null);
Point2D gvAdvPt = gv.getGlyphPosition(gv.getNumGlyphs());
/* GDI advances must not include device space rotation.
* See earlier comment in printGlyphVector() for details.
*/
AffineTransform advanceTransform =
AffineTransform.getScaleInstance(scaleFactorX, scaleFactorY);
float[] glyphAdvPos = new float[glyphPos.length];
advanceTransform.transform(glyphPos, 0, //source
glyphAdvPos, 0, //destination
glyphPos.length/2); //num points
glyphPos = glyphAdvPos;
}
wPrinterJob.textOut(str, devx, devy, glyphPos);
}
示例9: scaleShape
import java.awt.geom.AffineTransform; //导入方法依赖的package包/类
public static Shape scaleShape(final Shape shape, final double scale) {
final AffineTransform transform = AffineTransform.getScaleInstance(scale, scale);
return transform.createTransformedShape(shape);
}
示例10: paintComponent
import java.awt.geom.AffineTransform; //导入方法依赖的package包/类
/**
* Paints the component by drawing the chart to fill the entire component, but allowing for the
* insets (which will be non-zero if a border has been set for this component). To increase
* performance (at the expense of memory), an off-screen buffer image can be used.
*
* @param g
* the graphics device for drawing on.
*/
@Override
public void paintComponent(Graphics g) {
if (this.chart == null) {
return;
}
Graphics2D g2 = (Graphics2D) g.create();
// first determine the size of the chart rendering area...
Dimension size = getSize();
Insets insets = getInsets();
Rectangle2D available = new Rectangle2D.Double(insets.left, insets.top,
size.getWidth() - insets.left - insets.right, size.getHeight() - insets.top - insets.bottom);
// work out if scaling is required...
boolean scale = false;
double drawWidth = available.getWidth();
double drawHeight = available.getHeight();
this.scaleX = 1.0;
this.scaleY = 1.0;
if (drawWidth < this.minimumDrawWidth) {
this.scaleX = drawWidth / this.minimumDrawWidth;
drawWidth = this.minimumDrawWidth;
scale = true;
} else if (drawWidth > this.maximumDrawWidth) {
this.scaleX = drawWidth / this.maximumDrawWidth;
drawWidth = this.maximumDrawWidth;
scale = true;
}
if (drawHeight < this.minimumDrawHeight) {
this.scaleY = drawHeight / this.minimumDrawHeight;
drawHeight = this.minimumDrawHeight;
scale = true;
} else if (drawHeight > this.maximumDrawHeight) {
this.scaleY = drawHeight / this.maximumDrawHeight;
drawHeight = this.maximumDrawHeight;
scale = true;
}
Rectangle2D chartArea = new Rectangle2D.Double(0.0, 0.0, drawWidth, drawHeight);
// redrawing the chart every time...
AffineTransform saved = g2.getTransform();
g2.translate(insets.left, insets.top);
if (scale) {
AffineTransform st = AffineTransform.getScaleInstance(this.scaleX, this.scaleY);
g2.transform(st);
}
this.chart.draw(g2, chartArea, this.anchor, this.info);
g2.setTransform(saved);
Iterator<Overlay> iterator = this.overlays.iterator();
while (iterator.hasNext()) {
Overlay overlay = iterator.next();
overlay.paintOverlay(g2, this);
}
// redraw the zoom rectangle (if present) - if useBuffer is false,
// we use XOR so we can XOR the rectangle away again without redrawing
// the chart
drawSelectionRectangle(g2);
g2.dispose();
this.anchor = null;
this.verticalTraceLine = null;
this.horizontalTraceLine = null;
}
示例11: main
import java.awt.geom.AffineTransform; //导入方法依赖的package包/类
public static void main(final String[] args) throws IOException {
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsConfiguration gc = ge.getDefaultScreenDevice()
.getDefaultConfiguration();
AffineTransform at;
for (int size : SIZES) {
at = AffineTransform.getScaleInstance(size, size);
for (Shape clip : SHAPES) {
clip = at.createTransformedShape(clip);
for (Shape to : SHAPES) {
to = at.createTransformedShape(to);
// Prepare test images
BufferedImage snapshot;
BufferedImage bi = getBufferedImage(size);
VolatileImage vi = getVolatileImage(gc, size);
while (true) {
vi.validate(gc);
Graphics2D g2d = vi.createGraphics();
g2d.setColor(Color.GREEN);
g2d.fillRect(0, 0, size, size);
g2d.dispose();
if (vi.validate(gc) != VolatileImage.IMAGE_OK) {
continue;
}
draw(clip, to, bi, vi);
snapshot = vi.getSnapshot();
if (vi.contentsLost()) {
continue;
}
break;
}
// Prepare gold images
BufferedImage goldvi = getCompatibleImage(gc, size);
BufferedImage goldbi = getBufferedImage(size);
draw(clip, to, goldbi, goldvi);
validate(snapshot, goldvi);
vi.flush();
}
}
}
}
示例12: getDefaultTransform
import java.awt.geom.AffineTransform; //导入方法依赖的package包/类
@Override
public AffineTransform getDefaultTransform() {
return AffineTransform.getScaleInstance(scale, scale);
}
示例13: cut
import java.awt.geom.AffineTransform; //导入方法依赖的package包/类
public static BufferedImage cut(BufferedImage bi, int w, int h) throws IOException {
// 进行图片拉缩
int oldWidth = bi.getWidth();
int oldHeight = bi.getHeight();
double wr = 1.0;
double hr = 1.0;
// 横图(宽 > 高)
if (oldWidth > oldHeight) {
hr = h * 1.0 / oldHeight;
wr = hr;
// 拉伸后宽度不足
if (wr * oldWidth < w) {
wr = w * 1.0 / oldWidth;
hr = wr;
}
}
// 竖图
else {
wr = w * 1.0 / oldWidth;
hr = wr;
// 拉伸后高度不足
if (hr * oldHeight < h) {
hr = h * 1.0 / oldHeight;
wr = hr;
}
}
AffineTransform transform = AffineTransform.getScaleInstance(wr, hr);
AffineTransformOp ato = new AffineTransformOp(transform, null);
BufferedImage image = (BufferedImage) ato.filter(bi, null);
int newWidth = (int) (wr * oldWidth);
int newHeight = (int) (hr * oldHeight);
int x = 0, y = 0;
// 宽度大于预期宽度,裁剪左右
if (newWidth > w) {
x = -(newWidth - w) / 2;
} else if (newHeight > h) {// 裁剪上下
y = -(newHeight - h) / 2;
}
BufferedImage newImage = new BufferedImage(w, h, image.getType());
Graphics g = newImage.getGraphics();
g.drawImage(image, x, y, null);
return newImage;
}
示例14: main
import java.awt.geom.AffineTransform; //导入方法依赖的package包/类
public static void main(final String[] args) throws IOException {
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsConfiguration gc = ge.getDefaultScreenDevice()
.getDefaultConfiguration();
AffineTransform at;
for (int size : SIZES) {
at = AffineTransform.getScaleInstance(size, size);
for (Shape clip : SHAPES) {
clip = at.createTransformedShape(clip);
for (Shape to : SHAPES) {
to = at.createTransformedShape(to);
// Prepare test images
BufferedImage snapshot;
VolatileImage source = getVolatileImage(gc, size);
VolatileImage target = getVolatileImage(gc, size);
int attempt = 0;
while (true) {
if (++attempt > 10) {
throw new RuntimeException("Too many attempts: " + attempt);
}
// Prepare source images
source.validate(gc);
Graphics2D g2d = source.createGraphics();
g2d.setColor(Color.RED);
g2d.fillRect(0, 0, size, size);
g2d.dispose();
if (source.validate(gc) != VolatileImage.IMAGE_OK) {
continue;
}
// Prepare target images
target.validate(gc);
g2d = target.createGraphics();
g2d.setColor(Color.GREEN);
g2d.fillRect(0, 0, size, size);
g2d.dispose();
if (target.validate(gc) != VolatileImage.IMAGE_OK) {
continue;
}
draw(clip, to, source, target);
snapshot = target.getSnapshot();
if (source.contentsLost() || target.contentsLost()) {
continue;
}
break;
}
// Prepare gold images
BufferedImage goldS = getSourceGold(gc, size);
BufferedImage goldT = getTargetGold(gc, size);
draw(clip, to, goldS, goldT);
validate(snapshot, goldT);
source.flush();
target.flush();
}
}
}
}
示例15: main
import java.awt.geom.AffineTransform; //导入方法依赖的package包/类
public static void main(String args[]) {
String fontName = "Lucida Sans";
if (args.length > 0) {
fontName = args[0];
}
FontRenderContext frc = new FontRenderContext(null, false, false);
FontRenderContext frc2 = new FontRenderContext(AffineTransform.getScaleInstance(1.5, 1.5), false, false);
Font font0 = new Font(fontName, 0, 20);
HashMap map = new HashMap();
map.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
map.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
Font font = font0.deriveFont(map);
System.out.println("Using font: " + font);
double rot = -Math.PI/4;
AffineTransform scrtx = AffineTransform.getRotateInstance(rot);
scrtx.scale(1, 2);
Font[] fonts = {
font.deriveFont(1f),
font.deriveFont(20f),
font.deriveFont(40f),
font.deriveFont(80f),
font.deriveFont(AffineTransform.getRotateInstance(rot)),
font.deriveFont(AffineTransform.getScaleInstance(1, 2)),
font.deriveFont(AffineTransform.getScaleInstance(2, 4)),
font.deriveFont(scrtx),
};
LineMetrics[] metrics = new LineMetrics[fonts.length * 2];
for (int i = 0; i < metrics.length; ++i) {
Font f = fonts[i % fonts.length];
FontRenderContext frcx = i < fonts.length ? frc : frc2;
metrics[i] = f.getLineMetrics("X", frcx);
// dumpMetrics("Metrics for " + f.getSize2D() + " pt. font,\n tx: " +
// f.getTransform() + ",\n frctx: " + frcx.getTransform(), metrics[i]);
}
// test for linear scale
// this seems to work, might need to get fancy to deal with last-significant-bit issues?
double ds1 = metrics[2].getStrikethroughOffset() - metrics[1].getStrikethroughOffset();
double du1 = metrics[2].getUnderlineThickness() - metrics[1].getUnderlineThickness();
double ds2 = metrics[3].getStrikethroughOffset() - metrics[2].getStrikethroughOffset();
double du2 = metrics[3].getUnderlineThickness() - metrics[2].getUnderlineThickness();
if (ds2 != ds1 * 2 || du2 != du1 * 2) {
throw new IllegalStateException("non-linear scale: " + ds1 + " / " + ds2 + ", " +
du1 + " / " + du2);
}
JFrame jf = new JFrame("Fonts");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(new JScrollPane(new FontsPanel(fonts)));
jf.pack();
jf.setVisible(true);
}