当前位置: 首页 > 面试题库 >

在现有的JPanels中绘制JLayeredPane

杜苏燕
2023-03-14
问题内容

我正在开发国际象棋游戏。我想让板容器使用GridLayout来显示JPanels的8x8网格。(这将使诸如突出显示所选正方形和有效移动之类的功能变得更加容易。)然后,我想在此层上添加片段,以便将其拖放。我最初是通过在单独的正方形JPanels中绘制它们来显示这些块的,但后来发现尝试将它们拖放时,这是一个问题。从那以后,我一直在尝试使用JLayeredPane作为主要容器,但是遇到了几个问题。

一个是,一旦我为JLayeredPane指定了GridLayout,无论我使用哪个Integer来指定要添加JLabel或其他类型图像的图层,这些块都会被添加到网格中,从而使它们的位置固定并变形整个董事会。我已经读到,使用LayoutManagers会干扰JLayeredPane上的图层定位,因此这并不奇怪。(尽管JLayeredPane教程中的Oracle演示程序似乎可以做到这一点:http
:
//download.oracle.com/javase/tutorial/uiswing/examples/components/LayeredPaneDemo2Project/src/components/LayeredPaneDemo2.java)

但是,我还尝试将JPanels网格放入其自己的JPanel中,然后将其添加到JLayeredPane的低层,其想法是我可以将拖放图标添加到单独的,不透明的JPanel上。
JLayeredPane的图层。但是,当我执行此操作时,仅在JLayeredPane内部放置网格JPanel之后(即在添加拖放层之前),将不会显示网格。

我还尝试覆盖JLayeredPane的paintComponent(和paint)方法以绘制片段图像,但是它们被JPanels隐藏(我可以通过将JPanels设置为非不透明来看到它们确实存在),并且我可以告诉您没有选择在JLayeredPane上设置图形层的选项。我还尝试使用框架的glassPane绘制片段,但在那里也出现了不良行为。

任何帮助解释这种行为或我要去哪里的帮助,将不胜感激!


问题答案:

这是一个简单的示例,显示了如何(随机)将“棋子”从一个正方形拖放到另一个正方形:

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

public class ChessBoard extends JFrame implements MouseListener, MouseMotionListener
{
    JLayeredPane layeredPane;
    JPanel chessBoard;
    JLabel chessPiece;
    int xAdjustment;
    int yAdjustment;

    public ChessBoard()
    {
        Dimension boardSize = new Dimension(600, 600);

        //  Use a Layered Pane for this this application

        layeredPane = new JLayeredPane();
        layeredPane.setPreferredSize( boardSize );
        layeredPane.addMouseListener( this );
        layeredPane.addMouseMotionListener( this );
        getContentPane().add(layeredPane);

        //  Add a chess board to the Layered Pane

        chessBoard = new JPanel();
        chessBoard.setLayout( new GridLayout(8, 8) );
        chessBoard.setPreferredSize( boardSize );
        chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);
        layeredPane.add(chessBoard, JLayeredPane.DEFAULT_LAYER);

        //  Build the Chess Board squares

        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8; j++)
            {
                JPanel square = new JPanel( new BorderLayout() );
                square.setBackground( (i + j) % 2 == 0 ? Color.red : Color.white );
                chessBoard.add( square );
            }
        }

        // Add a few pieces to the board

        ImageIcon duke = new ImageIcon("dukewavered.gif"); // add an image here

        JLabel piece = new JLabel( duke );
        JPanel panel = (JPanel)chessBoard.getComponent( 0 );
        panel.add( piece );
        piece = new JLabel( duke );
        panel = (JPanel)chessBoard.getComponent( 15 );
        panel.add( piece );
    }

    /*
    **  Add the selected chess piece to the dragging layer so it can be moved
    */
    public void mousePressed(MouseEvent e)
    {
        chessPiece = null;
        Component c =  chessBoard.findComponentAt(e.getX(), e.getY());

        if (c instanceof JPanel) return;

        Point parentLocation = c.getParent().getLocation();
        xAdjustment = parentLocation.x - e.getX();
        yAdjustment = parentLocation.y - e.getY();
        chessPiece = (JLabel)c;
        chessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);

        layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
        layeredPane.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
    }

    /*
    **  Move the chess piece around
    */
    public void mouseDragged(MouseEvent me)
    {
        if (chessPiece == null) return;

        //  The drag location should be within the bounds of the chess board

        int x = me.getX() + xAdjustment;
        int xMax = layeredPane.getWidth() - chessPiece.getWidth();
        x = Math.min(x, xMax);
        x = Math.max(x, 0);

        int y = me.getY() + yAdjustment;
        int yMax = layeredPane.getHeight() - chessPiece.getHeight();
        y = Math.min(y, yMax);
        y = Math.max(y, 0);

        chessPiece.setLocation(x, y);
     }

    /*
    **  Drop the chess piece back onto the chess board
    */
    public void mouseReleased(MouseEvent e)
    {
        layeredPane.setCursor(null);

        if (chessPiece == null) return;

        //  Make sure the chess piece is no longer painted on the layered pane

        chessPiece.setVisible(false);
        layeredPane.remove(chessPiece);
        chessPiece.setVisible(true);

        //  The drop location should be within the bounds of the chess board

        int xMax = layeredPane.getWidth() - chessPiece.getWidth();
        int x = Math.min(e.getX(), xMax);
        x = Math.max(x, 0);

        int yMax = layeredPane.getHeight() - chessPiece.getHeight();
        int y = Math.min(e.getY(), yMax);
        y = Math.max(y, 0);

        Component c =  chessBoard.findComponentAt(x, y);

        if (c instanceof JLabel)
        {
            Container parent = c.getParent();
            parent.remove(0);
            parent.add( chessPiece );
            parent.validate();
        }
        else
        {
            Container parent = (Container)c;
            parent.add( chessPiece );
            parent.validate();
        }
    }

    public void mouseClicked(MouseEvent e) {}
    public void mouseMoved(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}

    public static void main(String[] args)
    {
        JFrame frame = new ChessBoard();
        frame.setDefaultCloseOperation( DISPOSE_ON_CLOSE );
        frame.setResizable( false );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
     }
}


 类似资料:
  • 问题内容: 就像标题说的那样,我正在尝试在现有的QGraphicsView中绘制。我使用QT Designer生成的窗口,我想在 QGraphicsView 内部绘制一个16x16正方形的矩阵。 这是我目前拥有的代码: 窗口: 我想在其中创建从窗口从QGraphicsView绘制方法的逻辑类: 我似乎无法找到有关如何在线进行操作的示例,我发现的示例对我来说是模糊的,似乎无法弄清楚。 问题答案: 我

  • 问题内容: 我正在制作一个需要在两个给定点之间绘制正弦波的应用程序。我有Google,也有Google,也没有找到我认为合适的东西。所以我的问题是,Android中是否有一种有效的方法来使用一些预定义的点来绘制类似波形的平滑波形?预先感谢您的任何帮助 问题答案: 您可以使用android.graphics.Path类在一组控制点之间构造一组二次或三次 Bézier样条曲线。

  • 我用JFreeChart来表示我的x和y数组。这些数组被绘制得很好,但是回归线被破坏了,永远也不会被绘制出来。除了和函数之外,所有函数都可以工作,例如打印值。不知怎的,这两个都不起作用。我不太介意,但我喜欢能够。我的阵列具有正确的数据,因此不确定问题出在哪里。我正在函数中将数组数据导入数据集。我的具有和阵列。它们有和数据类型。

  • 问题内容: 我似乎无法弄清楚如何正确旋转位图字体。我认为您修改了SpriteBatch的转换矩阵。但是,尝试旋转会使文本绕某个点旋转,而且我不知道如何相对于文本本身旋转文本。 问题答案: 您可以尝试以下代码:

  • 问题内容: 好的,我有这个代码 并且它将淡蓝色的背景绘制到屏幕上。我正在尝试创建一个渐变,该渐变从顶部的深蓝色到底部的浅蓝色。有没有简单的方法可以做到这一点?我是Libgdx和OpenGL的新手,所以我正尝试从书中学习,但我似乎找不到答案。我听说过要绘制一个大正方形并为顶点设置不同的颜色,但是我不确定该怎么做。 问题答案: 在libGDX中,ShapeRenderer对象包含一个方法,该方法为其位