- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否有一种更简单的方法来编写我的程序,以便我可以将基于图 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 ...
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/
我有一个对象,其属性的计算成本很高,因此它们仅在第一次访问时计算然后缓存。 private List notes; public List Notes { get
我正在选择datetime列中具有特定日期的记录,但感觉有点草率,我想知道是否有更好的方法。我的查询如下所示: SELECT * FROM myTable WHERE event_d
我正在尝试改进编码,最近遇到了自定义异常(exception)和“比许可更容易请求宽恕”(EAFP)的概念,但在我看来,自定义异常(exception)仍然遵循该概念。 例如,在下面的代码中,A看起来
//lets get the auto name set $accepted = 0; $sql = mysql_query("SELECT * FROM ".TBL_FACTIONS." WHERE
我正在尝试编写一个程序来处理各种对象的检测。这些对象具有原点、宽度、高度和速度。有没有一种方法可以设置数据结构/算法,以便每个对象都不会与其他所有对象进行检查? 我试图避免的问题的一些示例代码: fo
前一段时间我有一个类似的问题,但对整个 IoC/DI 主题以及我想要实现的目标的了解要少得多,所以这里又来了.... 我正在构建一个供公司内部通用的库。公共(public) API 中最常用的部分已经
是否有一种更简单的方法来编写我的程序,以便我可以将基于图 block 的 map 绘制到面板(某种形式)上,这样每次调整窗口大小时(关闭可调整大小) map 都不会重新绘制?我意识到这对于调试和测试我
我有大约 10 个使用相同事件的文本框 - 然后我只是将 sender 转换为文本框并对文本进行任何更改。我很好奇,有没有更简单/更简洁的方法将这些事件分配给代码隐藏方法?现在,我只是通过将每个事件的
我正在尝试在 Java 中实现一个退出堆栈,它目前正在给我带来机会!哈哈 我已经走到这一步了,据我所知,我的逻辑是合理的,但它无法编译。我不断收到 java.lang.ArrayIndexOutOfB
最近我接手了一个用 C#/XAML 编写的 Windows 应用商店应用程序项目,其要求之一是, 将项目迁移到 Javascript/HTML5,并通过从我们的网站动态加载 javascript,我们
我需要有关方法、算法或工具的建议来满足我的需求; SQL 中的动态过滤。 我使用 PHP 作为后端。我在前端创建结果过滤,如下所示: 单击“确定”后,我使用以下方式发送 JSON 数据: JSON.s
在 Chrome 检查器中,是否有比以下更好的编辑实时 CSS 的方法: 必须在“元素”和“来源”选项卡之间来回切换 将检查器样式表中的内容复制/粘贴到您的真实样式表中 在文件和文件夹窗口中找到您真正
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
我有一个 PhoneGap/Cordova 项目,这意味着我必须结合使用 JQuery 和 CSS 转换来实现动画(JQuery 动画在 iOS 上很笨重)。 我编写了一些实现一系列下拉菜单的代码,但
是否有更简单或更新的方法能够,例如,点击一个按钮并点击该按钮,重新路由到一个新路径? ....除了在组件上使用上下文方法之外? 谢谢 最佳答案 有几种方式: 1. withRouter - 来自 re
目前,我可以使用“在路径中查找”功能并键入 Artifact 的名称(或在 pom.xml 中搜索)来检查项目是否具有该依赖项,但这不适用于传递依赖项。 我可以在 IntelliJ(或命令行)中生成依
我有一个用 C# 编写的应用程序,该应用程序当前向 LDAP 进行身份验证。我们希望扩展功能以支持 IBM 的 Tivoli Access Manager,它由一个策略服务器和一个 LDAP 服务器(
假设我想根据同一字段中的值选择数据子集。现在我必须做这样的事情 TestLocationsResults = FILTER SalesData by (StoreId =='17' or StoreI
为什么“请求宽恕比获得许可更容易”(EAFP)被认为是 Python 中的良好实践?作为一名编程新手,我的印象是与使用其他检查相比,使用许多 try...except 例程会导致代码臃肿且可读性差。
这是一个与this fascinating question about detecting divide by zero exceptions at compile time相关的问题. 从 Eri
我是一名优秀的程序员,十分优秀!