为什么我的 Paint surface 方法不起作用?
Why is my Paint surface method not working?
所以有一段时间重写 JComponent 的 paint 方法给我带来了麻烦,我永远无法弄清楚为什么。我正在进行一个项目,该项目将拍摄一张黑白图像并将其绘制在屏幕上,其中图片中的每个像素都是屏幕上的 50 x 50 框,显然有些绘画将在屏幕外完成,但这没关系,因为这将是一款带有移动屏幕的 2D 自上而下游戏。无论我尝试过什么,当它运行时它永远不会在屏幕上绘制任何东西并且它与我的 1:50 逻辑无关因为当我试图让它绘制一个简单的矩形时,它甚至没有绘制那。所以问题一定出在 paint 方法上,但我不知道出了什么问题。我知道这是很多代码,但有人可以让我知道哪里出了问题吗?
(我知道还有很多方法没用,忽略那些)
这是 Jframe class:
(在此class绘制黑白图必须指定路径)
`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 class(class 包含主要方法):
package Code;
public class StartHere {
public static void main(String[] args) {
//Creating the frame:
final Window Frame = new Window();
}
}
这是存储所有地图数据的地图class:
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];
}
}
最后,这是负责从图像文件加载地图的地图加载器 class:
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,而不是绘制。任何体面的图形教程都会告诉你这一点。
- 总是调用 super 的绘画方法,super.paintComponent(...) 如果您要覆盖它,请在您的覆盖范围内。
- 以这样的方式创建您的代码,以便每个 class 都可以独立测试和调试,使用小型模拟 classes 来帮助您做到这一点。
- 这样如果您 运行 遇到错误,您可以创建并 post 您的最小、完整和可验证的示例供我们测试,而不是要求我们检查整个大型程序.
- 请注意,您永远不会调用
DrawStartInterface(...)
- 请学习并遵循 Java 命名约定,因为这样做将有助于其他人更好地理解您的代码。字段和方法名称应全部以小写字母开头,而 class 和接口名称应以大写字母开头。
所以有一段时间重写 JComponent 的 paint 方法给我带来了麻烦,我永远无法弄清楚为什么。我正在进行一个项目,该项目将拍摄一张黑白图像并将其绘制在屏幕上,其中图片中的每个像素都是屏幕上的 50 x 50 框,显然有些绘画将在屏幕外完成,但这没关系,因为这将是一款带有移动屏幕的 2D 自上而下游戏。无论我尝试过什么,当它运行时它永远不会在屏幕上绘制任何东西并且它与我的 1:50 逻辑无关因为当我试图让它绘制一个简单的矩形时,它甚至没有绘制那。所以问题一定出在 paint 方法上,但我不知道出了什么问题。我知道这是很多代码,但有人可以让我知道哪里出了问题吗? (我知道还有很多方法没用,忽略那些)
这是 Jframe class:
(在此class绘制黑白图必须指定路径)
`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 class(class 包含主要方法):
package Code;
public class StartHere {
public static void main(String[] args) {
//Creating the frame:
final Window Frame = new Window();
}
}
这是存储所有地图数据的地图class:
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];
}
}
最后,这是负责从图像文件加载地图的地图加载器 class:
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,而不是绘制。任何体面的图形教程都会告诉你这一点。
- 总是调用 super 的绘画方法,super.paintComponent(...) 如果您要覆盖它,请在您的覆盖范围内。
- 以这样的方式创建您的代码,以便每个 class 都可以独立测试和调试,使用小型模拟 classes 来帮助您做到这一点。
- 这样如果您 运行 遇到错误,您可以创建并 post 您的最小、完整和可验证的示例供我们测试,而不是要求我们检查整个大型程序.
- 请注意,您永远不会调用
DrawStartInterface(...)
- 请学习并遵循 Java 命名约定,因为这样做将有助于其他人更好地理解您的代码。字段和方法名称应全部以小写字母开头,而 class 和接口名称应以大写字母开头。