简体   繁体   English

将矩阵绘制为图像-Java

[英]Draw matrix to image - java

can you somebody help me with this algorithm: 有人能帮我这个算法吗:

I have image in BufferedImage img , i have Graphics2D g2 a double array of object Player[][] matrixPlayer = new Player[800][800] , now, i want print this array( matrixPlayer ) to my BufferedImage img , how i can do this? 我在BufferedImage img有图像,我在Graphics2D g2有一个对象Player[][] matrixPlayer = new Player[800][800]的双数组,现在,我想将此数组( matrixPlayer )打印到我的BufferedImage img ,我如何做这个? How to calculate the ratio of the image? 如何计算图像的比例?

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);

    }
}

Than you all. 比你们所有人

EDIT: 编辑:

There are two files( http://www.sendspace.com/filegroup/wwFs6z5VfqiVVdgkjejEWQ ) 有两个文件( http://www.sendspace.com/filegroup/wwFs6z5VfqiVVdgkjejEWQ

PLAYER: 玩家:

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;
    }

}

Main CLASS 主类

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();
    }

}

Calculate the ratio between your buffered image and your player positions. 计算缓冲图像和播放器位置之间的比率。

final double widthRatio = img.getWidth() / 800.0;
final double heighRatio = img.getHeight() / 800.0;

Then multiple that ratio by your player position to determine where to draw the player. 然后将该比例乘以您的玩家位置来确定在何处吸引玩家。

final int drawX = (int) Math.round(player.getX() * widthRatio);
final int drawY = (int) Math.round(player.getY() * heightRatio);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM