【问题标题】:Mouse movement inside grid鼠标在网格内移动
【发布时间】:2013-01-07 00:17:19
【问题描述】:

我创建了一个网格,以及我的班级 gridBalls 的双数组。

gridBalls 有一个 int 类型,你传递给它一个数字来确定 在网格中绘制什么,例如 0 个空白、1 个球等。

如果我想说的话,我的mousedragged 中的代码会是什么样子 单击我的网格上的插槽 [0][0](假装它是一个球),拖动我的鼠标 向右并画一条水平线(在本例中键入 3),或者如果我 把我的鼠标拖到底部,它画了一条垂直线(类型2)

public class connectiontest extends JApplet implements Runnable, MouseListener, MouseMotionListener
{
    Thread t;

    int GRIDSIZE = 6;
    int gridLevel = 1;

    JPanel menuPanel = new JPanel (null);
    JPanel gamePanel = new JPanel (new GridLayout (GRIDSIZE, GRIDSIZE));
    gridBalls[] [] panelGrid = new gridBalls [GRIDSIZE] [GRIDSIZE];

    public void init ()
    {
        getContentPane ().setBackground (Color.black);
        getContentPane ().setLayout (null);
        setSize (600, 600);

        gamePanel.setBounds (80, 120, 450, 450);
        gamePanel.setEnabled (true);

        menuPanel.setBounds (10, 10, 580, 100);
        menuPanel.setBackground (Color.black);
        menuPanel.setOpaque (true);
        menuPanel.setBorder (BorderFactory.createLineBorder (Color.white, 1));

        getContentPane ().add (gamePanel);
        getContentPane ().add (menuPanel);

        t = new Thread (this);
        drawGrid ();
        levels ();
        t.start ();
    }


    public void run ()
    {
        while (t != null)
        {
            try
            {
                t.sleep (20);
            }
            catch (InterruptedException e)
            {
                break;
            }
        }
    }


    private void drawGrid ()
    {
        for (int row = 0 ; row < GRIDSIZE ; row++)
        {
            for (int col = 0 ; col < GRIDSIZE ; col++)
            {
                panelGrid [row] [col] = new gridBalls ();
                panelGrid [row] [col].setOpaque (true);
                panelGrid [row] [col].setBackground (Color.black);
                panelGrid [row] [col].clr = Color.black;
                panelGrid [row] [col].setBorder (BorderFactory.createLineBorder (Color.blue, 1));
                gamePanel.add (panelGrid [row] [col]);
                panelGrid [row] [col].addMouseListener (this);
                panelGrid [row] [col].addMouseMotionListener (this);
            }
        }
    }


    public void mouseClicked (MouseEvent e)
    {

    }


    public void mouseEntered (MouseEvent e)
    {

    }


    public void mouseExited (MouseEvent e)
    {

    }


    public void mousePressed (MouseEvent e)
    {

    }


    public void mouseMoved (MouseEvent e)
    {

    }


    public void mouseDragged (MouseEvent e)
    {
        for (int i = 0 ; i < GRIDSIZE ; i++)
        {
            for (int j = 0 ; j < GRIDSIZE ; j++)
            {
                if (e.getSource () == panelGrid [i] [j])
                {
                    repaint ();
                }
            }
        }
    }


    public void mouseReleased (MouseEvent e)
    {

    }


    public void levels ()
    {
        if (gridLevel == 1)
        {
            panelGrid [0] [0].clr = Color.green;
            panelGrid [0] [0].type = 1;
            panelGrid [4] [0].clr = Color.green;
            panelGrid [4] [0].type = 1;
            panelGrid [0] [5].clr = Color.blue;
            panelGrid [0] [5].type = 1;
            panelGrid [5] [2].clr = Color.blue;
            panelGrid [5] [2].type = 1;
            panelGrid [0] [4].clr = Color.red;
            panelGrid [0] [4].type = 1;
            panelGrid [3] [2].clr = Color.red;
            panelGrid [3] [2].type = 1;
            panelGrid [5] [0].clr = Color.magenta;
            panelGrid [5] [0].type = 1;
            panelGrid [0] [1].clr = Color.magenta;
            panelGrid [0] [1].type = 1;
            panelGrid [1] [4].clr = Color.orange;
            panelGrid [1] [4].type = 1;
            panelGrid [4] [2].clr = Color.orange;
            panelGrid [4] [2].type = 1;
            panelGrid [0] [2].clr = Color.cyan;
            panelGrid [0] [2].type = 1;
            panelGrid [2] [2].clr = Color.cyan;
            panelGrid [2] [2].type = 1;
            repaint ();
        }
    }
}

class gridBalls extends JLabel
{
    Color clr;
    int type = 0;


    public gridBalls ()
    {

    }


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

        g.setColor (clr);
        // blank
        if (type == 0)
        {

        }
        // ball
        if (type == 1)
        {
            g.fillOval ((getWidth () / 10), (getHeight () / 10), (getWidth () - 14), (getHeight () - 14));
        }
        // vertical line
        if (type == 2)
        {
            g.fillRect ((getWidth () / 3), 0, (getWidth () / 3), getHeight ());
        }
        // horizontal line
        if (type == 3)
        {
            g.fillRect (0, (getHeight () / 3), (getWidth ()), (getHeight () / 3));
        }
        // corner piece top>right
        if (type == 4)
        {
            g.fillRect ((getWidth () / 3), 0, (getWidth () / 3), (getHeight () / 2));
            g.fillRect ((getHeight () / 3), (getHeight () / 3), (getWidth ()), (getHeight () / 3));
        }
        // corner piece top>left
        if (type == 5)
        {
            g.fillRect ((getWidth () / 3), 0, (getWidth () / 3), (getHeight () / 2));
            g.fillRect (0, (getHeight () / 3), (getWidth () / 3 + getWidth () / 3), (getHeight () / 3));
        }
        // corner piece bottom>right
        if (type == 6)
        {
            g.fillRect ((getWidth () / 3), (getHeight () / 2), (getWidth () / 3), (getHeight () / 2));
            g.fillRect ((getHeight () / 3), (getHeight () / 3), (getWidth ()), (getHeight () / 3));
        }
        // corner piece bottom>left
        if (type == 7)
        {
            g.fillRect ((getWidth () / 3), (getHeight () / 2), (getWidth () / 3), (getHeight () / 2));
            g.fillRect (0, (getHeight () / 3), (getWidth () / 3 + getWidth () / 3), (getHeight () / 3));
        }
    }
}

【问题讨论】:

  • 这个getContentPane().setLayout(null);t = new Thread(this); 把我吓坏了...你可能还想熟悉一下Java coding conventions - 你不会因为忽略而赢得任何朋友他们

标签: java swing mouseevent point swingutilities


【解决方案1】:

图层的整体构造使得真正让绘制效果正常工作变得非常困难。

但是,您的鼠标处理程序事件看起来像...

public void mousePressed(MouseEvent e) {
    clickPoint = e.getPoint();
    clickPoint = SwingUtilities.convertPoint(e.getComponent(), clickPoint, this);
}

public void mouseDragged(MouseEvent e) {
    dragPoint = e.getPoint();
    dragPoint = SwingUtilities.convertPoint(e.getComponent(), dragPoint, this);
    repaint();
}

clickPointdragPointjava.awt.Point 的实例。

你的下一个问题是让这个画出来……

您需要能够在内容上进行绘画。因为paint 不是双缓冲的,所以我个人会避免它,相反,我会覆盖paintChildren(因为paintComponent 将被涂在其他所有东西下面)...

@Override
protected void paintChildren(Graphics g) {
    super.paintChildren(g);
    if (clickPoint != null && dragPoint != null) {
        g.setColor(Color.RED);
        g.drawLine(clickPoint.x, clickPoint.y, dragPoint.x, dragPoint.y);
    }
}

建议

  • 避免使用null 布局。它们比它们的价值更麻烦,您最好根据需要使用布局管理器、复合组件和EmptyBorders 的组合...
  • 小心你的Thread,Swing 在线程管理方面有一些非常特殊的需求...

【讨论】:

    猜你喜欢
    • 2020-08-18
    • 1970-01-01
    • 2017-08-17
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多