如何将图像放在特定的JPanel中? [英] How to put an image in a specific JPanel?
问题描述
我正在尝试最近的棋盘游戏,现在我正在制作 Checkers 棋盘游戏。但是,我可以弄清楚如何在 GridLayout
的单元格中显示芯片。每个单元格都有自己的 JPanel ,我通过 FOR循环 在2D数组中分配。
I am experimenting with board games lately and now I am making a Checkers board game. However I cannon figure out how to display a chip in a cell of the GridLayout
. Each cell has it's own JPanel that I assigned in a 2D Array by a FOR Loop.
我需要显示图像 p1Chip
这只是 .png
在特定的 JPanel 中,假设它的变量名称是 board [2] [3]
,而不会搞砸 GridLayout
。
I need to display the image p1Chip
which is just a .png
in a specific JPanel, lets say it's variable name name is board[2][3]
, without messing up the GridLayout
.
关于我如何做到这一点的示例代码会很棒,因为它会帮助我理解更好。
A sample code on how I can do this would be great as it will help me out understand better.
我搜索了互联网,但找不到我需要的东西,或者至少解释了怎么做的东西。
I have searched the internet but I can't find what I need, or at least something that explains how to do it.
以下是目前的代码:
package checkers;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.IOException;
import javax.swing.JTextField;
public class Main extends JFrame {
private JPanel contentPane;
Image p1Chip;
JPanel[][] board = new JPanel[8][8];
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Main() throws IOException {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 800);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
startGame();
}
//Start Game!
public void startGame() throws IOException{
drawBoard();
}
//******************************DRAWS BOARD******************************\\
//Draws the board
public void drawBoard() throws IOException{
System.out.println("Start Drawing Board!");
getContentPane().setLayout(new GridLayout(8,8));
int colorAssignRow = 0;
int colorAssignCol = 0;
for(int r = 0; r < 8; r++){
colorAssignRow++;
colorAssignCol = 0;
for(int c = 0; c < 8; c++){
colorAssignCol++;
board[r][c] = new JPanel();
if(colorAssignRow%2!=0){
if(colorAssignCol%2==0)board[r][c].setBackground(Color.RED);
else board[r][c].setBackground(Color.BLACK);
}
else if(colorAssignRow%2==0){
if(colorAssignCol%2==0)board[r][c].setBackground(Color.BLACK);
else board[r][c].setBackground(Color.RED);
}
getContentPane().add(board[r][c]);
}
}
System.out.println("Board Drawing Done!");
}
//******************************END OF DRAWING BOARD******************************\\
public void getAssets(){
System.out.println("Getting assets!");
p1Chip = new ImageIcon("P1ChipNormal.png").getImage();
}
}
以上代码工作正常只输出 JPanels 的Checkers板,每个板位于网格的不同单元格中。
The code above works fine as it just outputs the Checkers board of JPanels, each in a different cell of the grid.
更新:
添加此方法以显示芯片,但是当我运行此方法时,没有芯片出现。
UPDATE: This method is added to display the chips, however when I run this method, no chips show up.
public void drawChips(){
/*
* When: 0 and even
* 1 and odd
* 2 and even
*/
//Drawing Player One Chips\\
for(int r = 0; r < 8; r++){
for(int c = 0; c < 8; c++){
label[r][c] = new JLabel();
board[r][c] = new JPanel();
if(r==0 && c%2==0){
label[r][c].setIcon(p1Chip);
board[r][c].add(label[r][c]);
}
else if(r==1 && c%2!=0 && c!=0){
label[r][c].setIcon(p1Chip);
board[r][c].add(label[r][c]);
}
else if(r==2 && c%2==0){
label[r][c].setIcon(p1Chip);
board[r][c].add(label[r][c]);
}
}
}
}
推荐答案
要在JPanel单元格中显示芯片:
To show a chip in a JPanel cell:
- 将芯片图像放入ImageIcon
- 通过JLabel的
setIcon(chipIcon)
方法将ImageIcon放入JLabel - 添加JLabel通过
add(someLabel)
方法到JPanel - JPanel现在将显示图像。
- Put the chip image into an ImageIcon
- Put that ImageIcon into a JLabel via JLabel's
setIcon(chipIcon)
method - Add the JLabel to the JPanel via the
add(someLabel)
method -- and the JPanel now will display the image.
然后,如果你想点击并移动芯片,
Then if you want to click and move the chip,
- 给它MouseListener和MouseMotionListener(MouseAdapater)
- 单击时,从包含JPanel的JLabel中移除JLabel并将其提升到顶层窗口的玻璃窗格。
- 使用MouseAdapter移动它。
- 发布时,将JLabel放在鼠标悬停的JPanel上。
- give it MouseListener and MouseMotionListener (MouseAdapater)
- When clicked, remove the JLabel from its containing JPanel and elevate it to the top level window's glass pane.
- Move it with the MouseAdapter.
- When released, place the JLabel the JPanel that the mouse is over.
这篇关于如何将图像放在特定的JPanel中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!