为什么我的 java 国际象棋项目中的棋子照片不显示?
why wont my piece photo in my java chess project show up?
有人可以解释为什么在图像图标功能中,当我 运行 程序时,我的棋子照片不会显示在棋盘上。我将函数添加到主函数,但它似乎不会显示图像图标从第 36-39 行开始。
package chess;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Toolkit;
public class chess extends JPanel
{
final static int WINDOW_WIDTH=600; // Width of window
final static int WINDOW_HEIGHT=600; // Height of window
public void paint(Graphics g)
{
// Create Chess board
g.fillRect(100, 100, 400, 400); // White squares
for(int i = 100; i <= 400; i+=100){
for(int j = 100; j <= 400; j+=100){
g.clearRect(i, j, 50, 50); // Black squares
}
}
for(int i = 150; i <= 450; i+=100){
for(int j = 150; j <= 450; j+=100){
g.clearRect(i, j, 50, 50);
//Print out image on board
ImageIcon image;
image=new ImageIcon("brook.gif");
g.drawImage(image.getImage(),0*66,7*44,null);
}
}
}
public static void main(String[] args)
{
JFrame window = new JFrame("Chess"); // Create Frame and give it the method named "window"
window.setSize(WINDOW_WIDTH,WINDOW_HEIGHT); // sets the size of the window
window.getContentPane().add(new chess()); // adds the chess board to the window
window.setLocationRelativeTo(null); // Sets the window in the middle of the screen by setting it null
window.setBackground(Color.BLUE); // Window background color set to gray
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // When the close option is clicked the program will stop running
window.setVisible(true); // Makes the window visible
}
}
首先,您的项目层次结构应该看起来像这样
那么你需要改变
image=new ImageIcon("brook.gif");
关注
image=new ImageIcon(getClass().getResource("/brook.gif"));
作为最后一个 请确保您输入的 gif
名称是 。
永远记住它是区分大小写
例如
对于上面的Hierarchy,我不得不这样写
image=new ImageIcon(getClass().getResource("/sphere.png"));
没有
image=new ImageIcon(getClass().getResource("/Sphere.png")); // or
image=new ImageIcon(getClass().getResource("/SPHERE.png"));
//The above 2 lines won't work.
有人可以解释为什么在图像图标功能中,当我 运行 程序时,我的棋子照片不会显示在棋盘上。我将函数添加到主函数,但它似乎不会显示图像图标从第 36-39 行开始。
package chess;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Toolkit;
public class chess extends JPanel
{
final static int WINDOW_WIDTH=600; // Width of window
final static int WINDOW_HEIGHT=600; // Height of window
public void paint(Graphics g)
{
// Create Chess board
g.fillRect(100, 100, 400, 400); // White squares
for(int i = 100; i <= 400; i+=100){
for(int j = 100; j <= 400; j+=100){
g.clearRect(i, j, 50, 50); // Black squares
}
}
for(int i = 150; i <= 450; i+=100){
for(int j = 150; j <= 450; j+=100){
g.clearRect(i, j, 50, 50);
//Print out image on board
ImageIcon image;
image=new ImageIcon("brook.gif");
g.drawImage(image.getImage(),0*66,7*44,null);
}
}
}
public static void main(String[] args)
{
JFrame window = new JFrame("Chess"); // Create Frame and give it the method named "window"
window.setSize(WINDOW_WIDTH,WINDOW_HEIGHT); // sets the size of the window
window.getContentPane().add(new chess()); // adds the chess board to the window
window.setLocationRelativeTo(null); // Sets the window in the middle of the screen by setting it null
window.setBackground(Color.BLUE); // Window background color set to gray
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // When the close option is clicked the program will stop running
window.setVisible(true); // Makes the window visible
}
}
首先,您的项目层次结构应该看起来像这样
那么你需要改变
image=new ImageIcon("brook.gif");
关注
image=new ImageIcon(getClass().getResource("/brook.gif"));
作为最后一个 请确保您输入的 gif
名称是 。
永远记住它是区分大小写
例如
对于上面的Hierarchy,我不得不这样写
image=new ImageIcon(getClass().getResource("/sphere.png"));
没有
image=new ImageIcon(getClass().getResource("/Sphere.png")); // or
image=new ImageIcon(getClass().getResource("/SPHERE.png"));
//The above 2 lines won't work.