2

我试图通过制作一个益智程序来提高我对 Java 的理解,尤其是 Java GUI。当前,用户选择一个图像,该图像被切割成指定数量的块。这些碎片是随机绘制到屏幕上的,但它们似乎被其他碎片的空白部分覆盖,并不是所有的都显示出来,但我可以打印出所有的坐标。我正在使用绝对定位,因为 LayoutManager 似乎不起作用。我短暂地尝试了 layeredPanes,但他们让我感到困惑,似乎并没有解决问题。我真的很感激一些帮助。
这是2个相关的类:

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 form

puzzle = 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;
  }//end randomNumber


  private PuzzlePiece[] createClip(BufferedImage passedImage)
  {

 int cw, ch;
 int w,h;
 w = image.getWidth(null);
 h = image.getHeight(null);
 cw = w/xPieces;
     ch = h/yPieces;

 int[] cells=new int[xPieces*yPieces];

 int dx, dy;

 BufferedImage clip = passedImage;

 //layeredPane.setPreferredSize(new Dimension(w,h));

    for (int x=0; x<xPieces; x++) 
      {
        int sx = x*cw;
        for (int y=0; y<yPieces; y++) 
            {
            int sy = y*ch;
            int cell = cells[x*xPieces+y];
            dx = (cell / xPieces) * cw;
            dy = (cell % yPieces) * ch;

            clip= passedImage.getSubimage(sx, sy, cw, ch);
    int myX = randomNumber(0,(int)w);
    int myY = randomNumber(0,(int)h);

    PuzzlePiece piece=new PuzzlePiece(clip,myX,myY);
    puzzle[x*xPieces+y]=piece;
    piece.setBounds(myX,myY,w,h);
    //layeredPane.setBounds(myX,myY,w,h);
    //layeredPane.add(piece,new Integer(x*xPieces+y));
    pane.add(piece);
    piece.repaint();

        }//end nested for
}//end for
return puzzle;
  }//end createClip

}//end class

如果间距有点乱,请见谅!

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;

public class PuzzlePiece extends JPanel
{
private Point imageCorner;      //the image's top-left corner location
private Point prevPt;               //mouse location for previous event
private Boolean insideImage =false;

private BufferedImage image;

public PuzzlePiece(BufferedImage clip, int x, int y)
{
 image = clip;
 imageCorner = new Point(x,y);
 //repaint();
}//end constructor

public void paintComponent(Graphics g)
{
     super.paintComponent(g);
 g.drawImage(image, (int)getImageCornerX(),(int)getImageCornerY(), this);
     System.out.println("paint "+getImageCornerX()+"   "+getImageCornerY());
    //repaint(); 
//g.dispose();
}//end paintComponent

public Point getImageCorner()
{
  return imageCorner;
}//end getImageCorner
public double getImageCornerY()
{
  return imageCorner.getY();
}//end getImageCornerY
public double getImageCornerX()
{
  return imageCorner.getX();
}//end getPoint


}//end class PuzzlePiece

任何帮助将不胜感激,我真的被卡住了!谢谢!!

4

3 回答 3

6

我对这个想法很感兴趣,所以我做了另一个例子,使用自定义布局管理器。

public class MyPuzzelBoard extends JPanel {

    public static final int GRID_X = 4;
    public static final int GRID_Y = 4;
    private BufferedImage image;

    public MyPuzzelBoard(BufferedImage image) {
        setLayout(new VirtualLayoutManager());
        setImage(image);

        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    removeAll();
                    generatePuzzel();
                } else {
                    Component comp = getComponentAt(e.getPoint());
                    if (comp != null && comp != MyPuzzelBoard.this) {
                        setComponentZOrder(comp, 0);
                        invalidate();
                        revalidate();
                        repaint();
                    }
                }
            }
        });
    }

    public void setImage(BufferedImage value) {
        if (value != image) {
            image = value;
            removeAll();
            generatePuzzel();
        }
    }

    public BufferedImage getImage() {
        return image;
    }

    protected float generateRandomNumber() {
        return (float) Math.random();
    }

    protected void generatePuzzel() {
        BufferedImage image = getImage();

        if (image != null) {
            int imageWidth = image.getWidth();
            int imageHeight = image.getHeight();

            int clipWidth = imageWidth / GRID_X;
            int clipHeight = imageHeight / GRID_Y;
            for (int x = 0; x < GRID_X; x++) {
                for (int y = 0; y < GRID_Y; y++) {

                    float xPos = generateRandomNumber();
                    float yPos = generateRandomNumber();
                    Rectangle bounds = new Rectangle((x * clipWidth), (y * clipHeight), clipWidth, clipHeight);
                    MyPiece piece = new MyPiece(image, bounds);
                    add(piece, new VirtualPoint(xPos, yPos));

                }
            }
        }

        invalidate();
        revalidate();
        repaint();
    }

    public class VirtualPoint {

        private float x;
        private float y;

        public VirtualPoint(float x, float y) {
            this.x = x;
            this.y = y;
        }

        public float getX() {
            return x;
        }

        public float getY() {
            return y;
        }

        public void setX(float x) {
            this.x = x;
        }

        public void setY(float y) {
            this.y = y;
        }
    }

    public class VirtualLayoutManager implements LayoutManager2 {

        private Map<Component, VirtualPoint> mapConstraints;

        public VirtualLayoutManager() {
            mapConstraints = new WeakHashMap<>(25);
        }

        @Override
        public void addLayoutComponent(Component comp, Object constraints) {
            if (constraints instanceof VirtualPoint) {
                mapConstraints.put(comp, (VirtualPoint) constraints);
            }
        }

        @Override
        public Dimension maximumLayoutSize(Container target) {
            return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
        }

        @Override
        public float getLayoutAlignmentX(Container target) {
            return 0.5f;
        }

        @Override
        public float getLayoutAlignmentY(Container target) {
            return 0.5f;
        }

        @Override
        public void invalidateLayout(Container target) {
        }

        @Override
        public void addLayoutComponent(String name, Component comp) {
        }

        @Override
        public void removeLayoutComponent(Component comp) {
            mapConstraints.remove(comp);
        }

        @Override
        public Dimension preferredLayoutSize(Container parent) {
            return new Dimension(400, 400);
        }

        @Override
        public Dimension minimumLayoutSize(Container parent) {
            return preferredLayoutSize(parent);
        }

        @Override
        public void layoutContainer(Container parent) {
            int width = parent.getWidth();
            int height = parent.getHeight();

            for (Component comp : parent.getComponents()) {

                VirtualPoint p = mapConstraints.get(comp);
                if (p != null) {

                    int x = Math.round(width * p.getX());
                    int y = Math.round(height * p.getY());

                    Dimension size = comp.getPreferredSize();

                    x = Math.min(x, width - size.width);
                    y = Math.min(y, height - size.height);

                    comp.setBounds(x, y, size.width, size.height);

                }
            }
        }
    }
}

基本上,这使用“虚拟”坐标系,而不是提供以像素为单位的绝对 x/y 位置,而是将它们作为父容器的百分比提供。现在,老实说,转换回绝对定位并不需要太多,这样,您还可以获得布局缩放。

该示例还演示了 Z 重新排序(以防万一)和双击简单重新随机化拼图

哦,我也把这块透明的(opaque = false

随机布局

哦,我应该提到一件事,在浏览这个示例时,我发现可以将片段放置在屏幕之外(完全和部分)。

您可能需要检查您的定位代码,以确保布局时的图像不会被移出屏幕;)

于 2012-08-06T04:33:38.430 回答
3

我想提出很多建议,但首先...

您选择随机位置的方式已关闭...

int myX = randomNumber(0,(int)w);
int myY = randomNumber(0,(int)h);

这允许生成重复位置(和覆盖单元格)

更新(使用布局管理器)

好的,所以这是范式的轻微转变。我没有制作剪辑并将其传递给作品,而是让作品选择如何渲染作品。相反,我将它负责的 Rectangle 传递给它。

这意味着,你可以简单地使用类似的东西setCell(Rectangle)来改变一块(除非你非常喜欢拖放;))

Board由于 Java 7 下的一些有趣行为,我最终使用了面板,但这是另一个问题;)

package puzzel;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.*;

public class PuzzlePieceDriver extends JFrame {


    public PuzzlePieceDriver(ImageIcon myPuzzleImage) {

        setTitle("Hot Puzz");

        setDefaultCloseOperation(EXIT_ON_CLOSE);

        setLayout(new BorderLayout());
        add(new Board(myPuzzleImage));

        pack();

        setVisible(true);


    }//end constructor

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {

                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                ImageIcon image = new ImageIcon(PuzzlePieceDriver.class.getResource("/issue459.jpg"));

                PuzzlePieceDriver driver = new PuzzlePieceDriver(image);
                driver.setLocationRelativeTo(null);
                driver.setVisible(true);

            }
        });

    }
}//end class

一块面板...该面板覆盖了preferredand minimumsize 方法...虽然它适用于本示例,但使用setPreferredSizeand可能会更好setMiniumumSize;)

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package puzzel;

import javax.swing.*;
import java.awt.*;
import java.awt.image.*;

public class PuzzlePiece extends JPanel {

    private BufferedImage masterImage;
    private Rectangle pieceBounds;
    private BufferedImage clip;

    public PuzzlePiece(BufferedImage image, Rectangle bounds) {

        masterImage = image;
        pieceBounds = bounds;

        // Make sure the rectangle fits the image
        int width = Math.min(pieceBounds.x + pieceBounds.width, image.getWidth() - pieceBounds.x);
        int height = Math.min(pieceBounds.y + pieceBounds.height, image.getHeight() - pieceBounds.y);

        clip = image.getSubimage(pieceBounds.x, pieceBounds.y, width, height);

    }//end constructor

    @Override
    public Dimension getPreferredSize() {

        return pieceBounds.getSize();

    }

    @Override
    public Dimension getMinimumSize() {

        return getPreferredSize();

    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        int x = 0;
        int y = 0;

        g.drawImage(clip, x, y, this);

        g.setColor(Color.RED);
        g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);

    }//end paintComponent

}//end class PuzzlePiece

板子面板...主要是因为我在使用 Java 7 时遇到的一些有趣问题...实现了一个MouseListener,当您运行程序时,单击板子,这很有趣;)

package puzzel;

import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

/**
 *
 * @author shane
 */
public class Board extends JPanel {

    public static final int X_PIECES = 4;
    public static final int Y_PIECES = 4;
    private PuzzlePiece[] puzzle = new PuzzlePiece[X_PIECES * Y_PIECES];

    private static BufferedImage image;

    public Board(ImageIcon myPuzzleImage) {

        image = iconToImage(myPuzzleImage); //pass image into bufferedImage form

        puzzle = createClip();

        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                removeAll();

                invalidate();

                createClip();

//                doLayout();

                invalidate();
                revalidate();
                repaint();

            }
        });

    }

    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;
    }//end randomNumber

    private PuzzlePiece[] createClip() {

        int cw, ch;
        int w, h;
        w = image.getWidth(null);
        h = image.getHeight(null);
        cw = w / X_PIECES;
        ch = h / Y_PIECES;

        // Generate a list of cell bounds
        List<Rectangle> lstBounds = new ArrayList<>(25);

        for (int y = 0; y < h; y += ch) {

            for (int x = 0; x < w; x += cw) {

                lstBounds.add(new Rectangle(x, y, cw, ch));

            }

        }

        BufferedImage clip = image;

        setLayout(new GridBagLayout());

        for (int x = 0; x < X_PIECES; x++) {
            for (int y = 0; y < Y_PIECES; y++) {

                // Get a random index
                int index = randomNumber(0, lstBounds.size() - 1);

                // Remove the bounds so we don't duplicate any positions
                Rectangle bounds = lstBounds.remove(index);

                PuzzlePiece piece = new PuzzlePiece(clip, bounds);
                puzzle[x * X_PIECES + y] = piece;

                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridx = x;
                gbc.gridy = y;
                gbc.fill = GridBagConstraints.BOTH;

                add(piece, gbc);
                piece.invalidate();
                piece.repaint();

            }//end nested for
        }//end for

        invalidate();
        repaint();

        return puzzle;
    }//end createClip

}

现在我知道你最终会问如何移动一块,有一个叫做getConstraintsGridBagLayout的奇妙方法,它允许你检索用于布局相关组件的约束。然后您可以修改and值并使用setConstraints来更新它(不要忘记调用and ;))gridxgridyinvalidaterepaint

我建议阅读如何使用 GridBagLayout以获取更多信息;)

最终,你会得到类似的东西:

谜题 1谜题 2

于 2012-08-05T23:06:06.357 回答
3

尝试setBorder(new LineBorder(Color.RED))在拼图构造函数中使用以查看拼图的边界在哪里。如果它们在您期望的位置,那么您的定位很可能是错误的。也可以让你的拼图扩展JComponent,或者setOpaque(false)如果你正在扩展,请使用JPanel.

于 2012-08-05T20:23:38.180 回答