gpt4 book ai didi

java - 为什么我的 Paint surface 方法不起作用?

转载 作者:行者123 更新时间:2023-11-29 04:57:48 25 4
gpt4 key购买 nike

所以有一段时间重写 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。


此外,根据我的评论:

  • 始终覆盖 paintComponent,而不是 paint。任何体面的图形教程都会告诉你这一点。
  • 如果您要覆盖它,请始终在您的覆盖范围内调用 super 的绘画方法 super.paintComponent(...)。
  • 以这样的方式创建您的代码,以便每个类都可以独立测试和调试,使用小型模拟类来帮助您做到这一点。
  • 这样,如果您遇到错误,您可以创建并发布您的最小、完整和可验证的示例,供我们测试,而不是要求我们检查整个大型程序。
  • 请注意,您永远不会调用 DrawStartInterface(...)
  • 请学习并遵循 Java 命名约定,因为这样做将有助于其他人更好地理解您的代码。字段和方法名称应全部以小写字母开头,而类和接口(interface)名称应以大写字母开头。

关于java - 为什么我的 Paint surface 方法不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33113021/

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