【问题标题】:Transparent Panels透明面板
【发布时间】:2016-05-22 14:18:04
【问题描述】:

我正在制作一个游戏,用户必须画线才能让球弹到目标上。我很难让球和线同时出现,我只能让其中一个出现。在我看来,这些面板相互阻挡,即使我让它们变得透明。我希望他们都出现在同一个框架上。在这篇文章中,线路面板覆盖了球面板。

import javax.swing.Timer;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JPanel;
import javax.swing.JFrame;

public class Game
{
  public static void main(String args[]) throws Exception 
  {
        JFrame f = new JFrame("Let's Play");
        f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        f.setSize(1280, 720);
        f.setLocation(300, 300);
        f.setResizable(false);

        //this part draws a ball that bounces around the screen
        BallPanel ballPanel = new BallPanel()
        {
           // draw rectangles and arcs

           public void paintComponent(Graphics g)
           {
            super.paintComponent(g); // call superclass's paintComponent 
            g.setColor(Color.red);

            // check for boundaries
            if (x < radius)         dx = Math.abs(dx);
            if (x > getWidth() - radius)    dx = -Math.abs(dx);
            if (y < radius)         dy = Math.abs(dy);
            if (y > getHeight() - radius)   dy = -Math.abs(dy);

            // adjust ball position
            x += dx;
            y += dy;
            g.fillOval(x - radius, y - radius, radius*2, radius*2);
           }

        };
        ballPanel.setOpaque(false);
        f.add(ballPanel);

        //this part allows you to draw lines on the frame with your mouse
        JPanel lineP = new JPanel()
        {
            Point pointStart = null;
            Point pointEnd   = null;

            {
                addMouseListener(new MouseAdapter() 
                {
                    public void mousePressed(MouseEvent me) 
                    {
                        pointStart = me.getPoint();
                    }

                    public void mouseReleased(MouseEvent me) 
                    {
                        pointStart = null;
                    }
                });
                addMouseMotionListener(new MouseMotionAdapter() 
                {
                    public void mouseMoved(MouseEvent me) 
                    {
                        pointEnd = me.getPoint();
                    }

                    public void mouseDragged(MouseEvent me) 
                    {
                        pointEnd = me.getPoint();
                        repaint();
                    }
                });
            }
            public void paint(Graphics dline) 
            {
              super.paint(dline);
                if (pointStart != null) 
                {
                    dline.setColor(Color.RED);
                    dline.drawLine(pointStart.x, pointStart.y, pointEnd.x, pointEnd.y);

              }
            }
        };
        lineP.setOpaque(false); //attempted to enable to see ball panel here
        f.add(lineP);
        f.setVisible(true); 
    }
}

class BallPanel extends JPanel implements ActionListener
{
private int delay = 10;
protected Timer timer;

public int x = 30;      // x position
public int y = 30;      // y position
public int radius = 15; // ball radius

public int dx = 10;     // increment amount (x coord)
public int dy = 10;     // increment amount (y coord)

public BallPanel()
{
     timer = new Timer(delay, this);
    timer.start();      // start the timer
}

public void actionPerformed(ActionEvent e)
// will run when the timer fires
{
     repaint();
   }


}

【问题讨论】:

标签: java swing jframe jpanel


【解决方案1】:

您有几个问题,但主要问题是您过度使用 GUI 组件。您应该只有一个单独的组件 JPanel 来进行绘图,即 DrawingPanel,而不是球面板和线条面板。 Ball 和 Line 应该是逻辑类,而不是 GUI 类,它们的显示应该在同一个 DrawingPanel 中。

其他问题包括:

  • 一个包含太多代码的主要方法。大多数代码应该被卸载到它所属的 OOP 世界中。
  • GUI 组件类也实现了监听器接口。这给了班级太多的责任,使调试和升级变得困难。将这些问题分开。
  • 您的一个类覆盖了paint 方法,应该避免这种情况。覆盖paintComponent。
  • 覆盖paintComponent 的另一个类在paintComponent 中有程序逻辑,应该避免这种情况,因为您对何时或是否调用此方法的控制有限。将逻辑从该类中取出并放入鼠标侦听器代码或游戏循环代码(Swing Timer)中。

【讨论】:

    猜你喜欢
    • 2011-04-16
    • 2010-09-20
    • 2014-09-11
    • 2015-07-08
    • 2012-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多