gpt4 book ai didi

java - Graphics2D 和 Jpanel 查询 : Easier Way?

转载 作者:行者123 更新时间:2023-11-29 10:13:36 27 4
gpt4 key购买 nike

是否有一种更简单的方法来编写我的程序,以便我可以将基于图 block 的 map 绘制到面板(某种形式)上,这样每次调整窗口大小时(关闭可调整大小) map 都不会重新绘制?我意识到这对于调试和测试我的 mapDrawing 函数非常有用,但是,我也不认为我做得很理想,甚至根本不是一种聪明的方式。

我的代码如下..如果你出于某种原因需要我的子类,我也可以编辑它们。

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


public class AhnkorMyst extends JPanel { // main game class

static final int screenWidth = 760;
static final int screenHeight = 760;

public void paintComponent(Graphics g) {
super.paintComponent(g); // paint background
setBackground(Color.BLACK);

Graphics2D g2d = (Graphics2D) g;
Map newMap = new Map(g2d, screenWidth, screenHeight);
newMap.generateBaseMap();
newMap.populateSurroundings();
newMap.quadSmoothingIteration ();

int i, j;
for (j = 0; j < (newMap.mapHeight / 20); j++) {
for (i = 0; i < (newMap.mapWidth / 20); i++) {
newMap.mainMap[i][j].paint();
}
}

}

public static void main (String[] args) {
AhnkorMyst game = new AhnkorMyst();
JFrame frame = new JFrame("Ahnkor Myst");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(game);
frame.setSize(screenWidth + 10, screenHeight + 30);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setResizable(false);

}
}

edit** 我的Map是用generateBaseMap()函数随机生成的。

最佳答案

这是“非常”基本的概念示例。基本上,这会在每次 JPanel 失效时重新构建 BufferedImage,它表示 map 的基本“ View ”。

你应该注意,我在每次构建 map 时简单地随机化 map ,大概你会使用某种定义 map 本身的虚拟结构,并将使用它来构建 map ...

enter image description here

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestTiles {

public static void main(String[] args) {
new TestTiles();
}

public TestTiles() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TileMap());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TileMap extends JPanel {

private int tileColumns = 8;
private int tileRows = 8;

private BufferedImage tileSheet;
private BufferedImage tileMap;

public TileMap() {

try {
tileSheet = ImageIO.read(getClass().getResource("/TileSet.png"));
} catch (IOException ex) {
ex.printStackTrace();
}

}

@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}

@Override
public void invalidate() {
tileMap = null;
super.invalidate();
}



protected void buildMap() {

tileMap = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = tileMap.createGraphics();
int tileWidth = tileSheet.getWidth() / tileColumns;
int tileHeight = tileSheet.getHeight() / tileRows;
Random random = new Random();
for (int x = 0; x < getWidth(); x += tileWidth) {
for (int y = 0; y < getHeight(); y += tileHeight) {
int xCell = random.nextInt(tileColumns - 1) * tileWidth;
int yCell = random.nextInt(tileRows - 1) * tileHeight;
BufferedImage tile = tileSheet.getSubimage(xCell, yCell, tileWidth, tileHeight);
g2d.drawImage(tile, x, y, this);
}
}
g2d.dispose();

}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (tileSheet != null) {
Graphics2D g2d = (Graphics2D) g.create();
if (tileMap == null) {
buildMap();
}
g2d.drawImage(tileMap, 0, 0, this);
g2d.dispose();
}
}
}

}

您可以进一步利用这个概念,将整个世界预先生成到一个 BufferedImage 中,然后使用 getSubImage 获取您想要显示的较小部分。这开始形成滚动的基本概念,因为您可以在世界中保持虚拟位置并计算需要显示 map 的哪一部分来表示它...

关于java - Graphics2D 和 Jpanel 查询 : Easier Way?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25027534/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com