gpt4 book ai didi

java - 如何删除白色背景以及如何添加棍子?

转载 作者:行者123 更新时间:2023-12-02 01:31:46 25 4
gpt4 key购买 nike

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

我们将非常感谢您的帮助。

我尝试使用图形。例如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);

}

最佳答案

I am having difficulty in adding the stick and the balls at the same time.

那么,为什么您要尝试一次编写整个应用程序而不在此过程中进行任何测试呢?为什么代码中有定时器?为什么要有 KeyListener?

学习一步一步开发应用程序。写一点代码做一些测试。当它起作用时添加更多功能。在添加更复杂的逻辑之前先了解基本的正确性。

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

JFrame 的默认布局管理器是 BorderLayout。默认情况下,当您将组件添加到框架(并且不指定约束)时,该组件将转到中心。只能将单个组件添加到 CENTER,因此只有最后添加的组件可见。

你的基本设计是错误的。您不希望球和球杆有单独的面板。您希望有一个“BilliardsGameBoard”面板来绘制多个对象。所以这个面板将绘制所有的球和棍子。这样所有的对象都由同一个类管理。

您也不应该创建具有变量名称的单独对象。这限制了您可以控制的对象数量。相反,将要绘制的对象保留在 ArrayList 中,然后绘制方法会迭代 ArrayList 并绘制每个对象。

参见:get width and height of JPanel outside of the class有关此方法的工作示例。

I add the balls the background of the JFrame goes white,

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

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

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

关于java - 如何删除白色背景以及如何添加棍子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55936543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com