我目前正在制作一款益智游戏,我的设计是将拼图(3 * 3棋盘)放在首位,我的九块棋子连续作为我的原始状态,我想成为能够将我的pieceComponents拖到电路板上,所以我将它们放在同一个JPanel中。我被建议使用GridBagConstraints来实现这一点,但是,对于我的拼图板,这是一个顶部的巨大空间,只有我添加到屏幕上的最后一个PieceComponent出现,我相信它是阻止我的GridBagConstraints用我的鼠标拖动PieceComponents(我已经拖动了),
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
public class Display extends JComponent {
private PuzzleDisplay puzzle;
private JFrame frame;
private static List<PieceComponent> pieces = new ArrayList<PieceComponent>();
private JPanel puzzlePanel;
private GridBagConstraints gbc = new GridBagConstraints();
/**
* Launch the application.
*/
public static void main(String[] args) {
Display d = new Display();
d.GUI();
}
/**
* Create the application.
*/
public Display() {
puzzle = new PuzzleDisplay();
}
private void PuzzlePanel() {
puzzlePanel = new JPanel();
puzzlePanel.setLayout(new GridBagLayout());
gbc.gridx=0;
gbc.gridy =0;
puzzlePanel.add(puzzle, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
puzzlePanel.add(puzzle.getPieces().get(0), gbc);
gbc.gridx = 1;
gbc.gridy = 1;
puzzlePanel.add(puzzle.getPieces().get(0), gbc);
gbc.gridx = 2;
gbc.gridy = 1;
puzzlePanel.add(puzzle.getPieces().get(0), gbc);
gbc.gridx = 3;
gbc.gridy = 1;
puzzlePanel.add(puzzle.getPieces().get(0), gbc);
puzzlePanel.setVisible(true);
}
public void GUI() {
frame = new JFrame("Puzzle");
frame.setVisible(true);
frame.setSize(850, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
PuzzlePanel();
frame.add(puzzlePanel, BorderLayout.CENTER);
}
}
这是我的PieceComponent类,我让它可以拖动。
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Component;
public class PieceComponent extends JComponent {
private Piece piece;
private BufferedImage myImage;
private volatile int screenX = 0;
private volatile int screenY = 0;
private volatile int myX = 0;
private volatile int myY = 0;
public PieceComponent(Side top, Side right, Side bottom, Side left, String path) {
piece = new Piece(top, right, bottom, left);
try {
myImage = ImageIO.read((new FileInputStream(path)));
} catch (IOException exp) {
exp.printStackTrace();
}
addMouseListener(new MouseListener() {
@Override
public void mousePressed(MouseEvent e) {
screenX = e.getXOnScreen();
screenY = e.getYOnScreen();
myX = getX();
myY = getY();
}
@Override
public void mouseClicked(MouseEvent e) {
piece.rotateClockwise();
repaint();
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
});
addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(MouseEvent e) {
int deltaX = e.getXOnScreen() - screenX;
int deltaY = e.getYOnScreen() - screenY;
setLocation(myX + deltaX, myY + deltaY);
}
@Override
public void mouseMoved(MouseEvent e) {
}
});
}
//returns the chosen side
public Side getSide(Direction direction) {
return piece.getSide(direction);
}
public Piece getPiece() {
return piece;
}
public int getHeight() {
return myImage.getHeight();
}
public int getWidth() {
return myImage.getWidth();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (myImage != null) {
AffineTransform trans = new AffineTransform();
trans.translate(getWidth() / 2, getHeight() / 2);
trans.rotate(piece.getOrientation() * Math.PI / 2);
trans.translate(-myImage.getWidth() / 2, -myImage.getHeight() / 2);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(myImage, trans, null);
}
}
public void rotateClockwise() {
piece.rotateClockwise();
}
这是我的PuzzleDisplay
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
public class PuzzleDisplay extends JComponent{
private Puzzle puzzle;
private JFrame frame;
private List<PieceComponent> pieceComponents = new ArrayList<PieceComponent>();
private List<Piece> pieces = new ArrayList<Piece>();
/**
* Create the application.
*/
public PuzzleDisplay() {
pieceComponents.add(new PieceComponent(Side.SPADE_OUT, Side.HEART_OUT, Side.DIAMOND_IN, Side.SPADE_IN, "res/piece_1.png"));
pieceComponents.add(new PieceComponent(Side.CLUB_OUT, Side.DIAMOND_OUT, Side.CLUB_IN, Side.HEART_IN, "res/piece_2.png"));
pieceComponents.add(new PieceComponent(Side.HEART_OUT, Side.CLUB_OUT, Side.CLUB_IN, Side.SPADE_IN, "res/piece_3.png"));
pieceComponents.add(new PieceComponent(Side.CLUB_OUT, Side.DIAMOND_OUT, Side.SPADE_IN, Side.SPADE_IN, "res/piece_4.png"));
pieceComponents.add(new PieceComponent(Side.CLUB_OUT, Side.CLUB_OUT, Side.HEART_IN, Side.SPADE_IN, "res/piece_5.png"));
pieceComponents.add(new PieceComponent(Side.HEART_OUT, Side.DIAMOND_OUT, Side.DIAMOND_IN, Side.HEART_IN, "res/piece_6.png"));
pieceComponents.add(new PieceComponent(Side.CLUB_OUT, Side.DIAMOND_OUT, Side.HEART_IN, Side.DIAMOND_IN, "res/piece_7.png"));
pieceComponents.add(new PieceComponent(Side.SPADE_OUT, Side.HEART_OUT, Side.CLUB_IN, Side.HEART_IN, "res/piece_8.png"));
pieceComponents.add(new PieceComponent(Side.DIAMOND_OUT, Side.SPADE_OUT, Side.SPADE_IN, Side.DIAMOND_IN, "res/piece_9.png"));
for(int i=0; i<pieceComponents.size(); i++) {
pieces.add(pieceComponents.get(i).getPiece());
}
puzzle = new Puzzle(3, pieces);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
int imageSize = pieceComponents.get(0).getHeight();
int boardBuffer = (getWidth()-3*imageSize)/2;
for(int i=0; i<puzzle.getCols(); i++) {
for(int j=0; j<puzzle.getRows(); j++) {
g2d.drawRect(i*imageSize, j*imageSize, imageSize, imageSize);
}
}
}
//returns the list of all pieces
public List<PieceComponent> getPieces(){
return pieceComponents;
}
//Fills the puzzle with the correct solution
public void solve() {
puzzle.solve();
}
//Determines whether the puzzle has been completed
public boolean isSolved() {
return puzzle.isSolved();
}
//Determines whether a piece will fit at the specified
//location
public boolean doesFit(PieceComponent pc, int row, int col) {
return puzzle.doesFit(pc.getPiece(), row, col);
}
//returns the Piece at the specified location
public Piece getPiece(int row, int col) {
return puzzle.getPiece(row, col);
}
//replaces a Piece at the given location and returns the old Piece
public Piece setPiece(Piece piece, int row, int col) {
return puzzle.setPiece(piece, row, col);
}
//removes and returns the Piece at the given location
public Piece removePiece(int row, int col) {
return removePiece(row, col);
}
//Clears the board and puts all pieces back into the piece list
public void reset() {
puzzle.reset();
}
//returns a List of any pieces not already on the board
public List<Piece> getUnused(){
return puzzle.getUnused();
}
//returns the number of rows
public int getRows() {
return puzzle.getRows();
}
//returns the number of columns
public int getCols() {
return puzzle.getCols();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(pieceComponents.get(0).getHeight()*3, pieceComponents.get(0).getHeight()*3);
}
/**
* Launch the application.
*/
public static void main(String[] args) {
PuzzleDisplay d = new PuzzleDisplay();
JFrame frame = new JFrame("Puzzle");
frame.setVisible(true);
frame.setSize(1250, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(d);
}
}