下面列出了java.awt.Graphics2D#copyArea ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void main(final String[] args) {
final BufferedImage bi = new BufferedImage(100, 300,
BufferedImage.TYPE_INT_RGB);
final Graphics2D g = bi.createGraphics();
g.scale(2, 2);
g.setColor(Color.RED);
g.fillRect(0, 0, 100, 300);
g.setColor(Color.GREEN);
g.fillRect(0, 100, 100, 100);
g.copyArea(0, 100, 100, 100, 0, -100);
g.dispose();
for (int x = 0; x < 100; ++x) {
for (int y = 0; y < 100; ++y) {
final int actual = bi.getRGB(x, y);
final int exp = Color.GREEN.getRGB();
if (actual != exp) {
System.err.println("Expected:" + Integer.toHexString(exp));
System.err.println("Actual:" + Integer.toHexString(actual));
throw new RuntimeException("Test " + "failed");
}
}
}
}
public static void main(final String[] args) {
final BufferedImage bi = new BufferedImage(100, 300,
BufferedImage.TYPE_INT_RGB);
final Graphics2D g = bi.createGraphics();
g.scale(2, 2);
g.setColor(Color.RED);
g.fillRect(0, 0, 100, 300);
g.setColor(Color.GREEN);
g.fillRect(0, 100, 100, 100);
g.copyArea(0, 100, 100, 100, 0, -100);
g.dispose();
for (int x = 0; x < 100; ++x) {
for (int y = 0; y < 100; ++y) {
final int actual = bi.getRGB(x, y);
final int exp = Color.GREEN.getRGB();
if (actual != exp) {
System.err.println("Expected:" + Integer.toHexString(exp));
System.err.println("Actual:" + Integer.toHexString(actual));
throw new RuntimeException("Test " + "failed");
}
}
}
}
public static void main(final String[] args) {
final BufferedImage bi = new BufferedImage(100, 300,
BufferedImage.TYPE_INT_RGB);
final Graphics2D g = bi.createGraphics();
g.scale(2, 2);
g.setColor(Color.RED);
g.fillRect(0, 0, 100, 300);
g.setColor(Color.GREEN);
g.fillRect(0, 100, 100, 100);
g.copyArea(0, 100, 100, 100, 0, -100);
g.dispose();
for (int x = 0; x < 100; ++x) {
for (int y = 0; y < 100; ++y) {
final int actual = bi.getRGB(x, y);
final int exp = Color.GREEN.getRGB();
if (actual != exp) {
System.err.println("Expected:" + Integer.toHexString(exp));
System.err.println("Actual:" + Integer.toHexString(actual));
throw new RuntimeException("Test " + "failed");
}
}
}
}
public static void main(final String[] args) {
final BufferedImage bi = new BufferedImage(100, 300,
BufferedImage.TYPE_INT_RGB);
final Graphics2D g = bi.createGraphics();
g.scale(2, 2);
g.setColor(Color.RED);
g.fillRect(0, 0, 100, 300);
g.setColor(Color.GREEN);
g.fillRect(0, 100, 100, 100);
g.copyArea(0, 100, 100, 100, 0, -100);
g.dispose();
for (int x = 0; x < 100; ++x) {
for (int y = 0; y < 100; ++y) {
final int actual = bi.getRGB(x, y);
final int exp = Color.GREEN.getRGB();
if (actual != exp) {
System.err.println("Expected:" + Integer.toHexString(exp));
System.err.println("Actual:" + Integer.toHexString(actual));
throw new RuntimeException("Test " + "failed");
}
}
}
}
public static void main(final String[] args) {
final BufferedImage bi = new BufferedImage(100, 300,
BufferedImage.TYPE_INT_RGB);
final Graphics2D g = bi.createGraphics();
g.scale(2, 2);
g.setColor(Color.RED);
g.fillRect(0, 0, 100, 300);
g.setColor(Color.GREEN);
g.fillRect(0, 100, 100, 100);
g.copyArea(0, 100, 100, 100, 0, -100);
g.dispose();
for (int x = 0; x < 100; ++x) {
for (int y = 0; y < 100; ++y) {
final int actual = bi.getRGB(x, y);
final int exp = Color.GREEN.getRGB();
if (actual != exp) {
System.err.println("Expected:" + Integer.toHexString(exp));
System.err.println("Actual:" + Integer.toHexString(actual));
throw new RuntimeException("Test " + "failed");
}
}
}
}
public static void main(final String[] args) {
final BufferedImage bi = new BufferedImage(100, 300,
BufferedImage.TYPE_INT_RGB);
final Graphics2D g = bi.createGraphics();
g.scale(2, 2);
g.setColor(Color.RED);
g.fillRect(0, 0, 100, 300);
g.setColor(Color.GREEN);
g.fillRect(0, 100, 100, 100);
g.copyArea(0, 100, 100, 100, 0, -100);
g.dispose();
for (int x = 0; x < 100; ++x) {
for (int y = 0; y < 100; ++y) {
final int actual = bi.getRGB(x, y);
final int exp = Color.GREEN.getRGB();
if (actual != exp) {
System.err.println("Expected:" + Integer.toHexString(exp));
System.err.println("Actual:" + Integer.toHexString(actual));
throw new RuntimeException("Test " + "failed");
}
}
}
}
public static void main(final String[] args) {
final BufferedImage bi = new BufferedImage(100, 300,
BufferedImage.TYPE_INT_RGB);
final Graphics2D g = bi.createGraphics();
g.scale(2, 2);
g.setColor(Color.RED);
g.fillRect(0, 0, 100, 300);
g.setColor(Color.GREEN);
g.fillRect(0, 100, 100, 100);
g.copyArea(0, 100, 100, 100, 0, -100);
g.dispose();
for (int x = 0; x < 100; ++x) {
for (int y = 0; y < 100; ++y) {
final int actual = bi.getRGB(x, y);
final int exp = Color.GREEN.getRGB();
if (actual != exp) {
System.err.println("Expected:" + Integer.toHexString(exp));
System.err.println("Actual:" + Integer.toHexString(actual));
throw new RuntimeException("Test " + "failed");
}
}
}
}
public static void main(final String[] args) {
final BufferedImage bi = new BufferedImage(100, 300,
BufferedImage.TYPE_INT_RGB);
final Graphics2D g = bi.createGraphics();
g.scale(2, 2);
g.setColor(Color.RED);
g.fillRect(0, 0, 100, 300);
g.setColor(Color.GREEN);
g.fillRect(0, 100, 100, 100);
g.copyArea(0, 100, 100, 100, 0, -100);
g.dispose();
for (int x = 0; x < 100; ++x) {
for (int y = 0; y < 100; ++y) {
final int actual = bi.getRGB(x, y);
final int exp = Color.GREEN.getRGB();
if (actual != exp) {
System.err.println("Expected:" + Integer.toHexString(exp));
System.err.println("Actual:" + Integer.toHexString(actual));
throw new RuntimeException("Test " + "failed");
}
}
}
}
public static void main(final String[] args) {
final BufferedImage bi = new BufferedImage(100, 300,
BufferedImage.TYPE_INT_RGB);
final Graphics2D g = bi.createGraphics();
g.scale(2, 2);
g.setColor(Color.RED);
g.fillRect(0, 0, 100, 300);
g.setColor(Color.GREEN);
g.fillRect(0, 100, 100, 100);
g.copyArea(0, 100, 100, 100, 0, -100);
g.dispose();
for (int x = 0; x < 100; ++x) {
for (int y = 0; y < 100; ++y) {
final int actual = bi.getRGB(x, y);
final int exp = Color.GREEN.getRGB();
if (actual != exp) {
System.err.println("Expected:" + Integer.toHexString(exp));
System.err.println("Actual:" + Integer.toHexString(actual));
throw new RuntimeException("Test " + "failed");
}
}
}
}
private void renderNoteField(Village v, HashMap<Village, List<Note>> pNoteMap, int row, int col, int pFieldWidth, int pFieldHeight, int pCopyPosition, Graphics2D g2d) {
if (v != null && v.isVisibleOnMap()) {
List<Note> notes = pNoteMap.get(v);//NoteManager.getSingleton().getNotesForVillage(v);
if (notes == null || notes.isEmpty()) {
return;
}
int half = notes.size() / 2;
int dx = -half * 2;
int dy = -half * 2;
//render notes
for (Note n : notes) {
int noteX = (int) Math.floor((double) col * pFieldWidth + pFieldWidth / 2.0 - 10);
int noteY = (int) Math.floor((double) row * pFieldHeight + pFieldHeight / 2.0 - 23);
int noteIcon = n.getMapMarker();
if (noteIcon != -1) {
g2d.copyArea(noteIcon * 32, pCopyPosition + 68, 32, 32, noteX + dx - noteIcon * 32, noteY + dy - pCopyPosition - 68);
dx += 2;
dy += 2;
}
}
}
}
public static void main(final String[] args) {
final BufferedImage bi = new BufferedImage(100, 300,
BufferedImage.TYPE_INT_RGB);
final Graphics2D g = bi.createGraphics();
g.scale(2, 2);
g.setColor(Color.RED);
g.fillRect(0, 0, 100, 300);
g.setColor(Color.GREEN);
g.fillRect(0, 100, 100, 100);
g.copyArea(0, 100, 100, 100, 0, -100);
g.dispose();
for (int x = 0; x < 100; ++x) {
for (int y = 0; y < 100; ++y) {
final int actual = bi.getRGB(x, y);
final int exp = Color.GREEN.getRGB();
if (actual != exp) {
System.err.println("Expected:" + Integer.toHexString(exp));
System.err.println("Actual:" + Integer.toHexString(actual));
throw new RuntimeException("Test " + "failed");
}
}
}
}
public static void main(final String[] args) {
final BufferedImage bi = new BufferedImage(100, 300,
BufferedImage.TYPE_INT_RGB);
final Graphics2D g = bi.createGraphics();
g.scale(2, 2);
g.setColor(Color.RED);
g.fillRect(0, 0, 100, 300);
g.setColor(Color.GREEN);
g.fillRect(0, 100, 100, 100);
g.copyArea(0, 100, 100, 100, 0, -100);
g.dispose();
for (int x = 0; x < 100; ++x) {
for (int y = 0; y < 100; ++y) {
final int actual = bi.getRGB(x, y);
final int exp = Color.GREEN.getRGB();
if (actual != exp) {
System.err.println("Expected:" + Integer.toHexString(exp));
System.err.println("Actual:" + Integer.toHexString(actual));
throw new RuntimeException("Test " + "failed");
}
}
}
}
public void testCopyArea() throws Exception {
RenderTest t2 = new RenderTest() {
@Override
public void paint(Graphics2D g) {
g.setPaint(Color.red);
g.fill(new Triangle(0, 0, 100, 100).shape);
g.copyArea(25, 25, 50, 50, 30, 30);
}
};
t2.test();
}
private void renderMarkerField(Village v, int row, int col, int pFieldWidth, int pFieldHeight, double zoom, boolean useDecoration, Graphics2D g2d) {
int tribeId = -666;
BufferedImage sprite = null;
Rectangle copyRect = null;
boolean showBarbarian = GlobalOptions.getProperties().getBoolean("show.barbarian");
Village currentUserVillage = DSWorkbenchMainFrame.getSingleton().getCurrentUserVillage();
if (v != null && (!v.getTribe().equals(Barbarians.getSingleton()) || showBarbarian)) {
v.setVisibleOnMap(true);
tribeId = v.getTribeID();
copyRect = renderedMarkerBounds.get(tribeId);
if (copyRect == null) {
sprite = getMarker(v);
}
} else {
if (v != null) {
v.setVisibleOnMap(false);
}
return;
}
//render sprite or copy area if sprite is null
if (sprite != null) {
//render sprite
AffineTransform t = AffineTransform.getTranslateInstance(col * pFieldWidth, row * pFieldHeight);
t.scale(1.0 / zoom, 1.0 / zoom);
g2d.drawRenderedImage(sprite, t);
if (currentUserVillage == null || !v.equals(currentUserVillage)) {
renderedMarkerBounds.put(tribeId, new Rectangle(col * pFieldWidth, row * pFieldHeight, pFieldWidth, pFieldHeight));
}
sprite.flush();
} else if (copyRect != null) {
g2d.copyArea(copyRect.x, copyRect.y, copyRect.width, copyRect.height, col * pFieldWidth - copyRect.x, row * pFieldHeight - copyRect.y);
}
}
private static BufferedImage fromSkin(ServerListPlusCore core, URL url, boolean helm) throws IOException {
BufferedImage skin = fromURL(core, url);
if (helm && !isSolidColor(skin, HELM_X, HELM_Y, HEAD_SIZE, HEAD_SIZE)) {
Graphics2D g = skin.createGraphics();
g.copyArea(HELM_X, HELM_Y, HEAD_SIZE, HEAD_SIZE, HEAD_X - HELM_X, HEAD_Y - HELM_Y);
g.dispose();
}
return skin.getSubimage(HEAD_X, HEAD_Y, HEAD_SIZE, HEAD_SIZE);
}
@Override
public void paint(final BufferedImage image, final Graphics2D graphics) {
graphics.copyArea(srcX, srcY, width, height, x - srcX, y - srcY);
}
@Override
protected void paintOperation(Graphics2D g) {
Rectangle r = getSourceRect();
g.copyArea(r.x, r.y, r.width, r.height, getDX(), getDY());
}
private void renderVillageField(Village v,
int row,
int col,
int globalRow,
int globalCol,
int pFieldWidth,
int pFieldHeight,
double zoom,
boolean useDecoration,
boolean showBarbarian,
boolean markedOnly,
Graphics2D g2d) {
Rectangle copyRect = null;
int textureId = -1;
BufferedImage sprite = null;
// Marker villageMarker = MarkerManager.getSingleton().getMarker(v);
if (v != null
&& !(v.getTribe().equals(Barbarians.getSingleton()) && !showBarbarian) // && !(villageMarker == null && markedOnly && !v.getTribe().getName().equals(GlobalOptions.getSelectedProfile().getTribeName()))
) {
//village field that has to be rendered
v.setVisibleOnMap(true);
if (GlobalOptions.getSkin().isMinimapSkin()) {
textureId = Skin.ID_V1;
} else {
textureId = v.getGraphicsType();
}
copyRect = renderedSpriteBounds.get(textureId);
if (copyRect == null) {
sprite = GlobalOptions.getSkin().getCachedImage(textureId, zoom);
}
} else {
if (v != null) {
v.setVisibleOnMap(false);
}
if (useDecoration) {
textureId = WorldDecorationHolder.getTextureId(globalCol, globalRow) + 100;
} else {
textureId = Skin.ID_DEFAULT_UNDERGROUND;
}
copyRect = renderedSpriteBounds.get(textureId);
if (copyRect == null && useDecoration) {
sprite = WorldDecorationHolder.getCachedImage(globalCol, globalRow, zoom);
} else if (copyRect == null && !useDecoration) {
sprite = GlobalOptions.getSkin().getCachedImage(textureId, zoom);
}
}
//render sprite or copy area if sprite is null
int posX = col * pFieldWidth;
int posY = row * pFieldHeight;
if (sprite != null) {
//render sprite
if (bMarkOnTop) {
g2d.setColor(Color.BLACK);
g2d.fillRect(posX, posY, pFieldWidth, pFieldHeight);
}
g2d.drawImage(sprite, posX, posY, null);
renderedSpriteBounds.put(textureId, new Rectangle(posX, posY, pFieldWidth, pFieldHeight));
} else if (copyRect != null) {
//copy from copy rect
g2d.copyArea(copyRect.x, copyRect.y, copyRect.width, copyRect.height, posX - copyRect.x, posY - copyRect.y);
}
}
@Override
public void paint(BufferedImage image, Graphics2D graphics) {
graphics.copyArea(srcX, srcY, width, height, x - srcX, y - srcY);
}
public void paint(Graphics2D g) {
g.copyArea(getX(), getY(), getWidth(), getHeight(), getDX(), getDY());
}