【发布时间】:2014-07-26 09:25:40
【问题描述】:
你能帮我解决这个算法吗:
我在BufferedImage img 中有图像,我有Graphics2D g2 对象Player[][] matrixPlayer = new Player[800][800] 的双数组,现在,我想将此数组(matrixPlayer) 打印到我的BufferedImage img,我该怎么做?
如何计算图片的比例?
int h = img.getHeight();
int w = img.getWidth();
for(int i = 0; i < 800; i++){
for (int j = 0; j < 800; j++) {
if(matrixPlayer[i][j]!=null)
///DRAW RECT on matrixPlayer[i][j].get(x); get(y);
}
}
比你们所有人。
编辑:
有两个文件(http://www.sendspace.com/filegroup/wwFs6z5VfqiVVdgkjejEWQ)
玩家:
public class Player {
private int x;
private int y;
private final String name;
private final String nameVil;
private final String aliance;
public Player(String x, String y, String name, String nameVil, String aliance) {
this.x = Integer.parseInt(x);
this.y = Integer.parseInt(y);
this.name = name;
this.nameVil = nameVil;
this.aliance = aliance;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public String getName() {
return name;
}
public String getNameVil() {
return nameVil;
}
public String getAliance() {
return aliance;
}
}
主类
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.imageio.ImageIO;
public class TravianMapa {
private final String[] aliance = {"RELAX™", "RELAKS™"};
private final String URL_TO_IMG = "C:\\Users\\pc\\Desktop\\mapa2.jpg";
private final Player[][] matrixMap = new Player[800][800];
private void start() throws IOException {
BufferedImage img = null;
try {
img = ImageIO.read(new File(URL_TO_IMG));
} catch (IOException e) {
System.out.println("Error: " + e);
}
drawMap(img);
}
private void drawMap(BufferedImage img) throws IOException {
getSqlFileData();
int h = img.getHeight(); //výška
int w = img.getWidth(); // šířka
Graphics2D g2 = img.createGraphics();
g2.setColor(Color.BLACK);
int h2 = h / 800;
int w2 = w / 800;
//AXIS
g2.drawLine(w / 2, 0, w / 2, h);
g2.drawLine(0, h / 2, w, h / 2);
////////////////////////////////////////////////////////////////////////////////
for (int i = 0; i < 800; i++) {
for (int j = 0; j < 800; j++) {
if (matrixMap[i][j] != null) {
int rx = (i * w2) + matrixMap[i][j].getX();
int ry = (j * h2) + matrixMap[i][j].getY();
g2.fillRect(rx, ry, 5, 5);
g2.drawString(matrixMap[i][j].getName(), rx, ry);
}
}
}
////////////////////////////////////////////////////////////////////////////////
File outputfile = new File("saved.png");
ImageIO.write(img, "png", outputfile);
}
private void getSqlFileData() throws FileNotFoundException, IOException {
try (BufferedReader br = new BufferedReader(new FileReader("map.sql"))) {
String line = br.readLine();
while (line != null) {
parseLine(line);
line = br.readLine();
}
}
}
private void parseLine(String line) {
int subS = 30;
line = line.substring(subS, line.length() - 2);
String[] tmpArr = line.split(",");
if (wantPrintALiance(tmpArr[tmpArr.length - 2])) {
tmpArr[5] = tmpArr[5].substring(1, tmpArr[5].length() - 1);
tmpArr[7] = tmpArr[7].substring(1, tmpArr[7].length() - 1);
tmpArr[9] = tmpArr[9].substring(1, tmpArr[9].length() - 1);
Player p = new Player(tmpArr[1], tmpArr[2], tmpArr[7], tmpArr[5], tmpArr[9]);
addToMatrix(p);
}
}
private boolean wantPrintALiance(String alianceInput) {
alianceInput = alianceInput.substring(1, alianceInput.length() - 1);
for (int i = 0; i < aliance.length; i++) {
if (alianceInput.equals(aliance[i])) {
return true;
}
}
return false;
}
private void addToMatrix(Player p) {
int realX, realY;
realX = 400 + p.getX();
realY = 400 + p.getY();
matrixMap[realX][realY] = p;
matrixMap[realX][realY].setX(realX);
matrixMap[realX][realY].setY(realY);
}
public static void main(String[] args) throws IOException {
new TravianMapa().start();
}
}
【问题讨论】:
-
请分享最少且完整的可测试代码。
-
好的,对不起,我编辑了我的帖子,现在,有完整的源代码。如果要运行它,请粘贴 map.sql 和 mapa2.jpg。 重要的方法是绘制地图
-
@DavidPostill Complete != Minimal
-
我们是否假设 BufferedImage 大于 800x800 并且您正在尝试确定如何调整播放器(在 800x800 中)网格以适应缓冲图像?