- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
所以我试图根据一个包含整数的文件制作 map
1111111111
1000000001
1000000001
1000000001
1111111111
所以我得到的代码是显示 1 的草地 block (32x32) 和 0 的土 block 。
这是我用于加载和区分 2 的代码。
protected void loadMapFile(String string) throws Exception {
// TODO Load Map, Put into int array
File f = new File(string);
BufferedReader in = new BufferedReader(new FileReader(f));
String line = in.readLine();
cols = line.length();
rows = count(string) + 1;
System.out.println(rows);
lines = new String[rows];
map = new Image[cols][rows];
mapMask = new int[cols][rows];
while (line != null) {
lines[counter] = line;
counter++;
line = in.readLine();
}
for (int y = 0; y < rows; y++) {
for (int x = 0; x < cols; x++) {
mapMask[x][y] = Integer.parseInt(lines[y].substring(x, x + 1));
System.out.print(mapMask[x][y]);
setImageContent(x, y);
}
System.out.println("");
}
mapCreated = true;
}
private void setImageContent(int x, int y) {
switch (mapMask[x][y]) {
case 0:// dirt
map[x][y] = Images.DirtTile.getImage();
break;
case 1:// grass
map[x][y] = Images.GrassTile.getImage();
break;
}
}
在我的绘画方法中,我是这样画的:
public void paintComponent(Graphics g) {
if (mapCreated) {
for (int y = 0; y < endRows; y++) {
for (int x = 0; x < endCols; x++) {
g.drawImage(map[x][y], 0 + (x * 32), 0 + (y * 32),
32 + (x * 32), 32 + (y * 32), 0, 0, 32, 32, this);
}
}
}
}
我的问题是,你们会推荐这个吗?还有没有更简单的方法可以将 Tiles 渲染到屏幕上?
最佳答案
paintComponent(...)
中绘制单个 BufferedImage。这将使绘图更加高效。另一种方法是将图 block 放入 ImageIcons,将图标放入 JLabel 数组。例如:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import javax.swing.*;
@SuppressWarnings("serial")
public class GridExample extends JPanel {
public static final int[][] MAP = {
{1, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2},
{1, 1, 0, 0, 2, 2, 2, 2, 2, 2, 2},
{1, 1, 1, 0, 2, 2, 2, 2, 2, 2, 2},
{1, 1, 1, 0, 0, 2, 2, 2, 2, 2, 2},
{1, 1, 1, 1, 0, 2, 2, 2, 2, 2, 2},
{1, 1, 1, 0, 0, 0, 2, 2, 2, 2, 2},
{1, 1, 0, 0, 0, 2, 2, 2, 2, 2, 2},
{1, 1, 1, 0, 0, 0, 2, 2, 2, 2, 2},
{1, 1, 1, 1, 1, 0, 0, 0, 0, 2, 2},
{1, 1, 1, 1, 1, 0, 0, 0, 2, 2, 2},
{1, 1, 1, 1, 1, 1, 0, 0, 0, 2, 2}
};
public static final Color[] COLORS = {};
private JLabel[][] labelGrid = new JLabel[MAP.length][MAP[0].length];
public GridExample() {
setLayout(new GridLayout(MAP.length, MAP[0].length));
for (int r = 0; r < labelGrid.length; r++) {
for (int c = 0; c < labelGrid[r].length; c++) {
labelGrid[r][c] = new JLabel();
labelGrid[r][c].setIcon(Ground.getGround(MAP[r][c]).getIcon());
add(labelGrid[r][c]);
}
}
}
private static void createAndShowGui() {
GridExample mainPanel = new GridExample();
JFrame frame = new JFrame("GridExample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
enum Ground {
DIRT(0, new Color(205,133, 63)), GRASS(1, new Color(0, 107, 60)),
WATER(2, new Color(29, 172, 214));
private int value;
private Color color;
private Icon icon;
private Ground(int value, Color color) {
this.value = value;
this.color = color;
icon = createIcon(color);
}
private Icon createIcon(Color color) {
int width = 24; // how to use const in enum?
BufferedImage img = new BufferedImage(width, width, BufferedImage.TYPE_INT_ARGB);
Graphics g = img.getGraphics();
g.setColor(color);
g.fillRect(0, 0, width, width);
g.dispose();
return new ImageIcon(img);
}
public int getValue() {
return value;
}
public Color getColor() {
return color;
}
public Icon getIcon() {
return icon;
}
public static Ground getGround(int value) {
for (Ground ground : Ground.values()) {
if (ground.getValue() == value) {
return ground;
}
}
return null;
}
}
示例 2:可以通过鼠标按下来更改图标的 Grid:
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import javax.swing.*;
@SuppressWarnings("serial")
public class GridExample extends JPanel {
private Ground[][] groundMap = {
{ Ground.GRASS, Ground.DIRT, Ground.DIRT, Ground.WATER, Ground.WATER,
Ground.WATER, Ground.WATER, Ground.WATER, Ground.WATER,
Ground.WATER, Ground.WATER },
{ Ground.GRASS, Ground.GRASS, Ground.DIRT, Ground.DIRT, Ground.WATER,
Ground.WATER, Ground.WATER, Ground.WATER, Ground.WATER,
Ground.WATER, Ground.WATER },
{ Ground.GRASS, Ground.GRASS, Ground.GRASS, Ground.DIRT, Ground.WATER,
Ground.WATER, Ground.WATER, Ground.WATER, Ground.WATER,
Ground.WATER, Ground.WATER },
{ Ground.GRASS, Ground.GRASS, Ground.GRASS, Ground.DIRT, Ground.DIRT,
Ground.WATER, Ground.WATER, Ground.WATER, Ground.WATER,
Ground.WATER, Ground.WATER },
{ Ground.GRASS, Ground.GRASS, Ground.GRASS, Ground.GRASS, Ground.DIRT,
Ground.WATER, Ground.WATER, Ground.WATER, Ground.WATER,
Ground.WATER, Ground.WATER },
{ Ground.GRASS, Ground.GRASS, Ground.GRASS, Ground.DIRT, Ground.DIRT,
Ground.DIRT, Ground.WATER, Ground.WATER, Ground.WATER,
Ground.WATER, Ground.WATER },
{ Ground.GRASS, Ground.GRASS, Ground.DIRT, Ground.DIRT, Ground.DIRT,
Ground.WATER, Ground.WATER, Ground.WATER, Ground.WATER,
Ground.WATER, Ground.WATER },
{ Ground.GRASS, Ground.GRASS, Ground.GRASS, Ground.DIRT, Ground.DIRT,
Ground.DIRT, Ground.WATER, Ground.WATER, Ground.WATER,
Ground.WATER, Ground.WATER },
{ Ground.GRASS, Ground.GRASS, Ground.GRASS, Ground.GRASS,
Ground.GRASS, Ground.DIRT, Ground.DIRT, Ground.DIRT,
Ground.DIRT, Ground.WATER, Ground.WATER },
{ Ground.GRASS, Ground.GRASS, Ground.GRASS, Ground.GRASS,
Ground.GRASS, Ground.DIRT, Ground.DIRT, Ground.DIRT,
Ground.WATER, Ground.WATER, Ground.WATER },
{ Ground.GRASS, Ground.GRASS, Ground.GRASS, Ground.GRASS,
Ground.GRASS, Ground.GRASS, Ground.DIRT, Ground.DIRT,
Ground.DIRT, Ground.WATER, Ground.WATER }, };
private JLabel[][] labelGrid = new JLabel[groundMap.length][groundMap[0].length];
public GridExample() {
setLayout(new GridLayout(groundMap.length, groundMap[0].length));
for (int r = 0; r < labelGrid.length; r++) {
for (int c = 0; c < labelGrid[r].length; c++) {
labelGrid[r][c] = new JLabel();
labelGrid[r][c].setIcon(groundMap[r][c].getIcon());
add(labelGrid[r][c]);
}
}
addMouseListener(new MyMouseListener());
}
private class MyMouseListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent mEvt) {
Component comp = getComponentAt(mEvt.getPoint());
for (int row = 0; row < labelGrid.length; row++) {
for (int col = 0; col < labelGrid[row].length; col++) {
if (labelGrid[row][col] == comp) {
Ground ground = groundMap[row][col];
int mapCode = ground.getValue();
mapCode++;
mapCode %= Ground.values().length;
groundMap[row][col] = Ground.values()[mapCode];
labelGrid[row][col].setIcon(groundMap[row][col].getIcon());
}
}
}
}
}
private static void createAndShowGui() {
GridExample mainPanel = new GridExample();
JFrame frame = new JFrame("GridExample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
enum Ground {
DIRT(0, new Color(205, 133, 63)), GRASS(1, new Color(0, 107, 60)), WATER(2,
new Color(29, 172, 214));
private int value;
private Color color;
private Icon icon;
private Ground(int value, Color color) {
this.value = value;
this.color = color;
icon = createIcon();
}
private Icon createIcon() {
int width = 24;
BufferedImage img = new BufferedImage(width, width,
BufferedImage.TYPE_INT_ARGB);
Graphics g = img.getGraphics();
g.setColor(color);
g.fillRect(0, 0, width, width);
g.dispose();
return new ImageIcon(img);
}
public int getValue() {
return value;
}
public Color getColor() {
return color;
}
public Icon getIcon() {
return icon;
}
public static Ground getGround(int value) {
for (Ground ground : Ground.values()) {
if (ground.getValue() == value) {
return ground;
}
}
return null;
}
}
关于java - 基于带整数的文件在 Canvas 上平铺绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17916057/
我正在尝试编写一个函数来制作绘图并将其自动保存到文件中。 我努力用它来动态地做的技巧[plotname=varname & filename=varname &], 并使其与从循环中调用它兼容。 #
有人可以帮助我如何在绘图条形图中添加“下拉”菜单。 我在以下链接 ( https://plot.ly/python/v3/dropdowns/ ) 上找到了一些信息,但我正在努力调整代码,因此下拉选项
我不确切知道如何表达这一点,但我本质上希望根据其他数据之前的列值将数据分组为 Excel 图的系列。例如: size weight apple 3 35 orange 4
我正在为出版物创建图表并希望它们具有相同的字体大小。 当我用多图创建图形时,字体大小会减小,即使我没有更改tiff() 分辨率或pointsize 参数。我根据最终适合的地 block 数量增加了图形
我用 glm::perspective(80.0f, 4.0f/3.0f, 1.0f, 120.0f);并乘以 glm::mat4 view = glm::lookAt( glm::vec3(
我在 Shiny UI 中有一个情节。如果我更改任何输入参数并且通过 react 性图将会改变。但是让我们考虑以下情况:- Shiny UI 中的绘图可以说股票的日内价格变动。为此,您查询一些实时数据
我对 R 有点陌生。我在以下两个线程中跟踪并实现了结果: http://tolstoy.newcastle.edu.au/R/e17/help/12/03/7984.html http://lukem
我想在 WPF 控件中使用 GDI+ 绘图。 最佳答案 有多种方法可以做到这一点,最简单的方法是锁定您使用 GDI 操作的位图,获取像素缓冲区(从锁定中获取的 BitmapData 中的 Scan0
如何在以下取自其网站的绘图示例中隐藏颜色条? df % layout(title = '2014 Global GDPSource:CIA World Factbook',
我有两列数据,X 和 Y,每个条目在两个向量的小数点后都有 4 位数据。 当我使用 plot(x,y) 绘制简单图时,轴上显示的数据精确到小数点后两位。如何在两个轴上将其更改为小数点后 4 位精度?
我目前正在使用 Canvas 处理 JavaFX-Drawing-Application。在 GraphicsContext 的帮助下,我使用 beginPath() 和 lineTo() 方法绘制线
如果这个问题已经得到解答,但我无法找到我需要的东西,我提前道歉。我想从名为 data1.dat、data2.dat 的文件中绘制一些结果......我设法通过循环导入数据,但我无法使用循环绘制结果。仅
我的 pandas 数据框中有一个功能,可以(可能)具有多个级别。我可以使用以下方法获得独特的级别: reasons = df["Reason"].unique() 我可以通过执行以下操作在单个图表上
我在 Ubuntu 14 和 Windows 7(均为 64 位)中用 Python 绘制结果时遇到问题。作为一个简单的比较,我做了: from tvb.simulator.lab import *
以下代码相当简单 - 它用随机选择的像素填充设计表面 - 没什么特别的(暂时忽略第二种方法中的 XXXXXXX)。 private void PaintBackground() { Rando
我正在尝试制作一个绘制函数图形的 swing 应用程序(现在很简单,例如 x+2)但我在根据屏幕坐标制作我的点的数学坐标时遇到问题。我希望它在我的图表中简单地画一条从 P1(0,1) 到 P2(1,2
编辑 4:问题的新格式 背景:我有一个扩展 JFrame 的类 Window,在 JFrame 中我有一个 Canvas 。我向 Canvas 添加自定义对象。这个对象的唯一目的(为了争论)是在 Ca
我需要为即将到来的锦标赛标记阶梯,但我找不到任何方法来语义标记它。到目前为止我看到的唯一方法是 mark it up as a table ,我想不惜一切代价避免这种情况。 有什么想法吗? 最佳答案
我目前正在为一个小型 uC 项目编写 UI。在计算垂直线的位置时遇到一些问题。这个想法是将红线沿 x 轴移动到矩形的末端。 使用无限旋转编码器递增的值,范围为 0 到 800,增量为 1。矩形的左侧是
我正在尝试绘制光分布图。我想准确地执行此问题的第一步所要求的:Statistical analysis on Bell shaped (Gaussian) curve . 现在我有一组值。我希望数组元
我是一名优秀的程序员,十分优秀!