Java GUI程序将2D数组值转换为图形

时间:2014-07-06 13:37:51

标签: java arrays map graphic

我是Java和Java的初学者我必须制作一个java程序,它从尺寸为347 * 697的二维数组中获取输入(数组的值范围从2200到4200)。我必须这样做,每增加100个范围,就会有一组特定的颜色(例如2200-2300色(214,217,223)等等)。我从未做过任何事情......&这是我发现有点类似的程序......任何人都可以告诉我如何转换它 我知道要求太多,但明天我必须给出一个ppt。 &安培;当每个选项都关闭时,我来到这里。拜托,伙计们,我需要帮助

import javax.swing.*;
import java.awt.*;
import java.util.Random;

public class Map extends JPanel {

public static final Color CITY = new Color(214,217,223);
public static final Color DESERT = new Color(255,204,102);
public static final Color DIRT_ROAD = new Color(153,102,0);
public static final Color FOREST = new Color(0,102,0);
public static final Color HILLS = new Color(51,153,0);
public static final Color LAKE = new Color(0,153,153);
public static final Color MOUNTAINS = new Color(102,102,255);
public static final Color OCEAN = new Color(0,0,153);
public static final Color PAVED_ROAD = new Color(51,51,0);
public static final Color PLAINS = new Color(102,153,0);

public static final Color[] TERRAIN = {
    CITY,
    DESERT,
    DIRT_ROAD,
    FOREST,
    HILLS,
    LAKE,
    MOUNTAINS,
    OCEAN,
    PAVED_ROAD,
    PLAINS
};

public static final int NUM_ROWS = 215;
public static final int NUM_COLS = 300;

public static final int PREFERRED_GRID_SIZE_PIXELS = 10;

// In reality you will probably want a class here to represent a map tile,
// which will include things like dimensions, color, properties in the
// game world.  Keeping simple just to illustrate.
private final Color[][] terrainGrid;

public Map(){
    this.terrainGrid = new Color[NUM_ROWS][NUM_COLS];
    Random r = new Random();
    // Randomize the terrain
    for (int i = 0; i < NUM_ROWS; i++) {
        for (int j = 0; j < NUM_COLS; j++) {
            int randomTerrainIndex = r.nextInt(TERRAIN.length);
            Color randomColor = TERRAIN[randomTerrainIndex];
            this.terrainGrid[i][j] = randomColor;
        }
    }
    int preferredWidth = NUM_COLS * PREFERRED_GRID_SIZE_PIXELS;
    int preferredHeight = NUM_ROWS * PREFERRED_GRID_SIZE_PIXELS;
    setPreferredSize(new Dimension(preferredWidth, preferredHeight));
}

@Override
public void paintComponent(Graphics g) {
    // Important to call super class method
    super.paintComponent(g);
    // Clear the board
    g.clearRect(0, 0, getWidth(), getHeight());
    // Draw the grid
    int rectWidth = getWidth() / NUM_COLS;
    int rectHeight = getHeight() / NUM_ROWS;

    for (int i = 0; i < NUM_ROWS; i++) {
        for (int j = 0; j < NUM_COLS; j++) {
            // Upper left corner of this terrain rect
            int x = i * rectWidth;
            int y = j * rectHeight;
            Color terrainColor = terrainGrid[i][j];
            g.setColor(terrainColor);
            g.fillRect(x, y, rectWidth, rectHeight);
        }
    }
}

public static void main(String[] args) {
    // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JFrame frame = new JFrame("Game");
            Map map = new Map();
            frame.add(map);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
        }
    });
}

}

1 个答案:

答案 0 :(得分:0)

使用所需图像的每个像素的rgb值创建byte[]byte[]长度为width * height * 3(每个像素为3个值)。请查看this,了解如何从BufferedImage创建byte[]

另一种方法是直接为每个像素创建一个BufferedImage和set rgb值。在这种情况下,您必须将所有3个rgb值转换为单个int值。