我正在通过制作一个拼图程序来提高对Java的理解,尤其是Java GUI。当前,用户选择图像,该图像被切成指定的数量。这些棋子随机绘制到屏幕上,但似乎被其他棋子的空白部分覆盖,并且并非全部都出现,但是我可以打印出所有坐标。我正在使用绝对定位,因为LayoutManager似乎不起作用。我曾短暂尝试过layeredPanes,但他们让我感到困惑,似乎并不能解决问题。我真的很感谢您的帮助。这是两个相关的类:import javax.swing.*;import java.awt.*;import java.awt.image.*;import java.awt.event.*;public class PuzzlePieceDriver extends JFrame{ private static Dimension SCREENSIZE = Toolkit.getDefaultToolkit().getScreenSize(); private static final int HEIGHT = SCREENSIZE.height; private static final int WIDTH = SCREENSIZE.width; public static int MY_WIDTH; public static int MY_HEIGHT; private static BufferedImage image; private int xPieces = PuzzleMagicDriver.getXPieces(); private int yPieces = PuzzleMagicDriver.getYPieces(); private PuzzlePiece[] puzzle = new PuzzlePiece[xPieces*yPieces]; public Container pane = this.getContentPane(); private JLayeredPane layeredPane = new JLayeredPane(); public PuzzlePieceDriver(ImageIcon myPuzzleImage) { MY_WIDTH = myPuzzleImage.getIconWidth()+(int)myPuzzleImage.getIconHeight()/2; MY_HEIGHT = myPuzzleImage.getIconHeight()+(int)myPuzzleImage.getIconHeight()/2; setTitle("Hot Puzz");setSize(MY_WIDTH,MY_HEIGHT);setLocationByPlatform(true);pane.setLayout(null);image = iconToImage(myPuzzleImage); //pass image into bufferedImage formpuzzle = createClip(image);//pane.add(layeredPane);setVisible(true); }//end constructor public static BufferedImage iconToImage(ImageIcon icon) { Image img = icon.getImage(); int w = img.getWidth(null); int h = img.getHeight(null); BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics g = image.createGraphics(); // Paint the image onto the buffered image g.drawImage(img, 0, 0, null); g.dispose(); return image; }//end BufferedImage protected int randomNumber(int min, int max) { int temp = min + (int)(Math.random() * ((max - min) + 1)); return temp; }
添加回答
举报
0/150
提交
取消