java - 如何删除白色背景以及如何添加棍子?
问题描述
我正在尝试为我的项目创建台球游戏,但我很难同时添加棒和球。此外,当我添加球时,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);
}
解决方案
我很难同时添加棍子和球。
那么,您为什么要在不进行任何测试的情况下一次编写整个应用程序呢?为什么你的代码中有定时器?为什么你有 KeyListeners?
学习一步一步地开发应用程序。写一点代码做一些测试。当它工作时添加更多功能。在添加更复杂的逻辑之前先获得基本的权利。
Practice_Mode.add(myBalls);
//Practice_Mode.add(myStick);
JFrame 的默认布局管理器是 BorderLayout。默认情况下,当您将组件添加到框架(并且不指定约束)时,组件会转到 CENTER。只能将单个组件添加到 CENTER,因此只有最后添加的组件可见。
你的基本设计是错误的。您不想为球和棍子设置单独的面板。您想要一个可以绘制多个对象的“BilliardsGameBoard”面板。所以这个面板将绘制所有的球和棍子。这样,所有对象都由同一个类管理。
您也不应该使用变量名创建单个对象。这限制了您可以控制的对象数量。相反,将要绘制的对象保留在 ArrayList 中,然后绘制方法遍历 ArrayList 并绘制每个对象。
请参阅:在类之外获取 JPanel 的宽度和高度,以获取此方法的工作示例。
我添加了JFrame的背景变白的球,
Practice_Mode.getContentPane().setBackground(new Color(0, 1, 0, 0.5f));
指定颜色时不要使用 alpha 值。对于绿色,您可以使用:
practiceMode.getContentPane().setBackground( Color.GREEN );
推荐阅读
- java - rxjava2 基于发射数量的分支逻辑
- bash - 在 bash 中使用 && 在输出中查找两个字符串
- java - 使用 Google Admin Directory API 添加组 - 403 未授权访问此资源/API
- php - 使用 apachectl 启动的 Mac 上未加载 PHP 文件
- neural-network - 神经网络和big-o
- c# - 如何将所选项目(具有字符串格式)保存到多个文本框中
- regex - 正则表达式非贪婪命名捕获组
- vue.js - 关于 vue-cli 3.x main.js
- jsp - 升级项目后 JSP 中的未终止标记
- python - 尝试打印会产生索引错误,但没有代码运行是否正常?