如何去除白色背景,如何添加棍子?

时间:2019-05-01 12:48:22

标签: java swing billiards

我正在尝试为我的项目创建一个台球游戏,但是我很难同时添加球棒和球。另外,当我添加这些球时,JFrame的背景变为白色,实际上应该是绿色(表格的颜色)。

非常感谢您的帮助。

我尝试使用Graphics。例如g2d.setBackground(Color.green)。哪个没用

public class Practice_Window implements MouseListener, MouseMotionListener, KeyListener {

    JFrame Practice_Mode = new JFrame(); 
    Balls myBalls = new Balls(); 
    Stick myStick = new Stick(); 

    public void PracticeWindow()
    {

        Practice_Mode.setSize(1000, 500);
        Practice_Mode.setVisible(true); 
        Practice_Mode.setResizable(false);
        Practice_Mode.setTitle("Practice Mode");
        Practice_Mode.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Practice_Mode.getContentPane().setBackground(new Color(0, 1, 0, 0.5f)); //Not visible after i add my balls
        Practice_Mode.getRootPane().setBorder(BorderFactory.createMatteBorder(10, 10, 10, 10, Color.GREEN));

        Practice_Mode.add(myBalls);
        //Practice_Mode.add(myStick); 

        //JPanel p = new JPanel();

        //Timer t= new Timer(10, myBalls); 


    }

//BALL CLASS
public class Balls extends JPanel implements ActionListener
{
    double Size = 35;

    double REDxSpeed = 5; 
    double REDySpeed = 5;
    double REDSpeed = 0;


    double YELLOWxSpeed = 2; 
    double YELLOWySpeed = 3; 
    double YELLOWSpeed = 0;


    double WHITExSpeed = 4; 
    double WHITEySpeed = 2; 
    double WHITESpeed = 0;

    double friction = 0.2; 

    boolean Collision = false; 

    Ellipse2D.Double red = new Ellipse2D.Double(200, 200, Size, Size);
    Ellipse2D.Double yellow = new Ellipse2D.Double(300, 300, Size, Size); 
    Ellipse2D.Double white = new Ellipse2D.Double(150, 400, Size, Size);

    Timer t = new Timer(10, this); 


    Balls()
    {
        t.start(); 
    }

    @Override
    public void actionPerformed(ActionEvent e) //Things are moving here
    {

        //RED BALL
        red.x += REDxSpeed; 
        red.y += REDySpeed; 
        REDSpeed = Math.sqrt(REDxSpeed*REDxSpeed + REDySpeed*REDySpeed); 

        repaint(); 

        if(red.x < 0 || red.x > getWidth() - red.width)
        {
            REDxSpeed = -REDxSpeed; 
        }

        if(red.y < 0 || red.y > getHeight() - red.height)
        {
            REDySpeed = -REDySpeed; 
        }

        //YELLOW BALL
        yellow.x += YELLOWxSpeed; 
        yellow.y += YELLOWySpeed; 
        YELLOWSpeed = Math.sqrt(YELLOWxSpeed*YELLOWxSpeed + YELLOWySpeed*YELLOWySpeed); 

        repaint(); 

        if(yellow.x < 0 || yellow.x > getWidth() - yellow.width)
        {
            YELLOWxSpeed = -YELLOWxSpeed; 
        }

        if(yellow.y < 0 || yellow.y > getHeight() - yellow.height)
        {
            YELLOWySpeed = -YELLOWySpeed; 
        }

        //WHITE BALL
        white.x += WHITExSpeed; 
        white.y += WHITEySpeed; 
        WHITESpeed = Math.sqrt(WHITExSpeed*WHITExSpeed + WHITESpeed*WHITEySpeed);
        repaint(); 

        if(white.x < 0 || white.x > getWidth() - white.width)
        {
            WHITExSpeed = -WHITExSpeed; 
        }

        if(white.y < 0 || white.y > getHeight() - white.height)
        {
            WHITEySpeed = -WHITEySpeed; 
        }
        Collision_Detection(); 
    }

    public void paintComponent(Graphics g) //DRAWING MY BALLS
    {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D)g; 
        //g2d.setBackground(Color.green);

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setColor(Color.red);
        g2d.fill(red);

        g2d.setColor(Color.yellow);
        g2d.fill(yellow);

        g2d.setColor(Color.black);
        g2d.fill(white);
    }

//STICK CLASS
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Rectangle2D;

import javax.swing.*; 

public class Stick extends JPanel implements MouseListener, MouseMotionListener, ActionListener {
    int xLocation = 50; 
    int yLocation = 50; 
    int Width = 50;  
    int Height = 15;

    Rectangle2D.Double stick = new Rectangle2D.Double(200, 200, Width, Height); 

    Timer t = new Timer(10, this); 

    Stick()
    {
        t.start();
    }

    public void PaintComponent(Graphics g)
    {
        super.paintComponent(g); 
        Graphics2D g2d = (Graphics2D)g; 

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);


        g2d.setColor(Color.ORANGE);
        g2d.fill(stick);

    }

2 个答案:

答案 0 :(得分:2)

  

我很难同时添加棒和球。

那么为什么您要一次编写整个应用程序而不进行任何测试呢?为什么代码中包含计时器?为什么会有KeyListeners?

一次学会开发应用程序。写一点代码做一些测试。当它起作用时,添加更多功能。首先添加基本知识,然后再添加更复杂的逻辑。

Practice_Mode.add(myBalls);
//Practice_Mode.add(myStick); 

JFrame的默认布局管理器是BorderLayout。默认情况下,将组件添加到框架时(不指定约束),组件将进入CENTER。只能将一个组件添加到CENTER,因此只有最后添加的组件才可见。

您的基本设计是错误的。您不想为球和棒子使用单独的面板。您希望有一个可以绘制多个对象的“ BilliardsGameBoard”面板。因此,该面板将为所有球和棒涂漆。这样,所有对象都由同一类管理。

您也不应该使用变量名创建单个对象。这限制了您可以控制的对象数量。相反,将要绘制的对象保留在ArrayList中,然后绘制方法遍历ArrayList并绘制每个对象。

有关此方法的有效示例,请参见:get width and height of JPanel outside of the class

  

我将球添加到JFrame的背景变为白色,

Practice_Mode.getContentPane().setBackground(new Color(0, 1, 0, 0.5f)); 

在指定颜色时不要使用alpha值。对于绿色,您可以使用:

practiceMode.getContentPane().setBackground( Color.GREEN );

答案 1 :(得分:0)

您的杆和球正在延伸JPanel,通常用作容器来分组一堆JComponents,这是按钮和UI元素。您看到的图形错误很可能是Java试图使用默认的BorderLayout并排排列面板,因为它认为您在尝试实现自由形状时希望使用按钮和填充物面板。

一种更好的方法是将杆和球渲染为JPanel上的原始形状,而不是JPanels本身。这可以通过使它们实现Shape或至少赋予它们draw方法来实现; Primitives教程将很有用。 This question的处境也与您相似,并且有与您相关的答案。

相关问题