- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以有一段时间重写 JComponent 的 paint 方法给我带来了麻烦,我永远无法弄清楚为什么。我正在进行一个项目,该项目将拍摄一张黑白图像并将其绘制在屏幕上,其中图片中的每个像素都是屏幕上的 50 x 50 框,显然有些绘画将在屏幕外完成,但这没关系,因为这将是一款带有移动屏幕的 2D 自上而下游戏。无论我尝试过什么,当它运行时它永远不会在屏幕上绘制任何东西并且它与我的 1:50 逻辑无关,因为当我试图让它绘制一个简单的矩形时,它甚至没有绘制那个。所以问题一定出在 paint 方法上,但我不知道出了什么问题。我知道这是很多代码,但有人可以让我知道哪里出了问题吗?(我知道还有很多方法没用,先忽略那些)
这是 Jframe 类:
(必须在该类中指定绘制黑白图的路径)
`package Code;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
//class that stores all of the painting methods and the GUI methods:
public class Window extends JFrame{
double WINDOWWIDTH, WINDOWHEIGHT;
JPanel TitlePanel;
JButton PlayGame, Quit;
JLabel Title;
Map DigitizedMap;
PaintSurface PS;
int x = 0;
int y = 0;
int TilesAcross;
int TilesDown;
public Window(){
WINDOWWIDTH = 1200;
WINDOWHEIGHT = (Math.floor(WINDOWWIDTH / 50)*0.66) * 50;
TilesAcross = (int) (WINDOWWIDTH / 50);
TilesDown = (int) (WINDOWHEIGHT / 50);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize((int) WINDOWWIDTH, (int) WINDOWHEIGHT);
this.setTitle("Stealth Client || Version 1.0");
//all of the code for the user interface will go here:
//DrawStartInterface(this);
//creating the digitized verion of the map for the paint method to use:
MapLoader ML = new MapLoader("C:\\Users\\Greg\\Desktop\\TestMap.png");
DigitizedMap = ML.ConvertMap();
PS = new PaintSurface();
TitlePanel = new JPanel();
TitlePanel.add(PS, BorderLayout.CENTER);
this.add(TitlePanel);
this.setVisible(true);
this.setResizable(false);
}
private void DrawStartInterface(JFrame f){
//all of the starting UI:
TitlePanel = new JPanel(new GridBagLayout());
TitlePanel.setBackground(Color.DARK_GRAY);
Title = new JLabel("STEALTH");
Title.setForeground(Color.ORANGE);
Title.setFont(new Font("Calabri", Font.BOLD, 48));
addItem(TitlePanel, Title, 0, 0, 1, 1, 1);
PlayGame = new JButton("Play");
PlayGame.setBackground(Color.BLACK);
PlayGame.setForeground(Color.ORANGE);
PlayGame.setFont(new Font("Calabri", Font.BOLD, 36));
PlayGame.addActionListener(new ActionEvent());
addItem(TitlePanel, PlayGame, 0, 1, 1, 1, 1);
Quit = new JButton("Quit");
Quit.setBackground(Color.BLACK);
Quit.setForeground(Color.ORANGE);
Quit.setFont(new Font("Calabri", Font.BOLD, 36));
Quit.addActionListener(new ActionEvent());
addItem(TitlePanel, Quit, 0, 2, 1, 1, 1);
f.add(TitlePanel);
}
private class ActionEvent implements ActionListener{
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
//which action has been heard:
if(e.getSource() == PlayGame){
//plays the game:
StartGame();
} else if(e.getSource() == Quit){
//Quits the game:
Terminate();
}
}
}
public void Terminate(){
//exits the game:
System.exit(0);
}
//Starts the game:
private void StartGame(){
TitlePanel.add(PS);
}
//The paint Surface class that will be stored in the panel and paint the game:
class PaintSurface extends JComponent{
@Override
public void paint (Graphics g){
//basic graphics shizel wizel:
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.drawRect(100, 100, 100, 100);
render(g2);
System.out.println("Painted");
}
private void render(Graphics2D g2){
//renders only the squares within a certain distance of the center of the screen:
int PlayerTileX = (int) Math.ceil((x + (WINDOWWIDTH / 2))/50); // This is the x tile that the player is in
int PlayerTileY = (int) Math.ceil((y + (WINDOWHEIGHT / 2))/50); // this is the y tile that the player is in
//now we are going through only the tiles around the player and rendering them:
int XOffset = x - (int) (PlayerTileX - (Math.ceil(TilesAcross / 2)));
int YOffset = y - (int) (PlayerTileY - (Math.ceil(TilesDown / 2)));
for (int i = (int) (PlayerTileX - (Math.ceil(TilesAcross / 2))); i < (int) (PlayerTileX + (Math.ceil(TilesAcross / 2))); i++){
for (int n = (int) (PlayerTileY - (Math.ceil(TilesDown / 2))); n < (int) (PlayerTileY + (Math.ceil(TilesDown / 2))); n++){
//this is where only the coorect boxes will be rendered because of the limiting for loops:
//The if statement for determining what type of thing it is:
if (DigitizedMap.getTile(i, n) == 1){
//Rendering the walls:
g2.drawRect((int)(((i * 50) % WINDOWWIDTH) - XOffset), (int)(((i * 50) % WINDOWHEIGHT) - YOffset), 50, 50);
}
}
}
}
}
//used to add things to panels:
public void addItem(JPanel p, JComponent c, int x, int y, int width, int height,
int align /* Defines the spot on the coordinate */) {
GridBagConstraints gc = new GridBagConstraints();
gc.gridx = x;
gc.gridy = y;
gc.gridwidth = width;
gc.gridheight = height;
gc.insets = new Insets(10, 10, 10, 10);
switch (align) {
case 1:
gc.anchor = GridBagConstraints.NORTH;
break;
case 2:
gc.anchor = GridBagConstraints.EAST;
break;
case 3:
gc.anchor = GridBagConstraints.SOUTH;
break;
case 4:
gc.anchor = GridBagConstraints.WEST;
break;
case 5:
gc.anchor = GridBagConstraints.CENTER;
break;
}
p.add(c, gc);
}
}
`
这是 Starthere 类(包含主要方法的类):
package Code;
public class StartHere {
public static void main(String[] args) {
//Creating the frame:
final Window Frame = new Window();
}
}
这是存储所有 map 数据的 map 类:
package Code;
public class Map {
//the Array for all of the codes:
double Tiles[][];
int width;
int height;
//setters and getters for the width and height:
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
//the constructor for the double map:
public Map(long Width, long Height){
Tiles = new double[(int) Width][(int) Height];
}
//this is where the double array is going to be set:
public void setTile(int x, int y, double type){
Tiles[x][y] = type;
}
//this gets the given tile code:
public double getTile(int x, int y){
return Tiles[x][y];
}
}
最后,这是负责从图像文件加载 map 的 map 加载器类:
package Code;
import java.awt.Color;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class MapLoader {
BufferedImage MapImage;
public MapLoader(String MapPath){
//loading the map image from the specified map path:
try {
MapImage = ImageIO.read(new File(MapPath));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Map ConvertMap(){
Map m = new Map(MapImage.getWidth(), MapImage.getHeight());
//now to read the individual pixels of the image and determine the code for the map object:
for(int i = 0; i < MapImage.getWidth(); i++){
for(int n = 0; n < MapImage.getHeight(); n++){
if (MapImage.getRGB(i, n) == Color.BLACK.getRGB()){
//Black = Wall = 1
m.setTile(i, n, 1);
System.out.print("1");
} else {
//else it is nothing so White = Space = 0
m.setTile(i, n, 0);
System.out.print("0");
}
}
System.out.println("");
}
return m;
}
}
非常感谢任何帮助我的人,这已经困扰我好几天了,我没有想法。
这里还有一个我一直在使用的示例文件: Example File
最佳答案
您的绘画方法有效,但没有人能看到 PaintSurface 对象。你有:
PS = new PaintSurface();
TitlePanel = new JPanel();
TitlePanel.add(PS, BorderLayout.CENTER);
您将 PS 对象添加到 BorderLayout.CENTER 位置,但 TitlePanel 不使用 BorderLayout —— 它使用默认的 FlowLayout。现在 PaintSurface 的首选大小为 0,0,并且由于 TitlePanel 使用 FlowLayout,PS 将非常小。
解决方案:将TitlePanel的布局设置为BorderLayout。
PS = new PaintSurface();
TitlePanel = new JPanel(new BorderLayout());
TitlePanel.add(PS, BorderLayout.CENTER);
这将使 PaintSurface 实例填充 TitlePanel。
此外,根据我的评论:
DrawStartInterface(...)
关于java - 为什么我的 Paint surface 方法不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33113021/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!