gpt4 book ai didi

java - 帮助向 JFrame 添加桨

转载 作者:行者123 更新时间:2023-12-01 15:57:55 24 4
gpt4 key购买 nike

这是我必须为大学类(class)完成的练习,它不是一个标记的作业,我可以在一些帮助下完成。我可以让球出现在屏幕上并弹起侧面,目前如果它落入屏幕底部并不重要,我可以让 Racket 在不同时间出现在屏幕上,但我无法得到它们两者同时出现。请帮忙

这是我的类(class)

主类

package movingball;

public class Main
{
public static void main (String []args)
{
MovingBall world = new MovingBall("Moving Ball");
world.setVisible(true);
world.move();
}
}

球类

package movingball;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;

public class Ball
{
private final int RADIUS = 10;

private Point pos;
private Color ballColour;
private int yChange = 2;
private int xChange = 1;
private int height, width;
private int change;

public Ball (int frameWidth, int frameHeight)
{
change = 3;
ballColour = Color.RED;
width = frameWidth;
height = frameHeight;
pos = new Point();
pos.x = (int)(Math.random() * (width - RADIUS)) + RADIUS;
pos.y = (int)(Math.random() * (height/2 - RADIUS)) + RADIUS;
}

//There are lots of ways you can updateBallState
//Note that the menu bar occupies some of the visible space
public void move()
{
if(pos.y < RADIUS)
{
yChange = - yChange;
}
if(pos.x < RADIUS)
{
xChange = -xChange;
}
if(pos.x > width - RADIUS)
{
xChange = -xChange;
}
if(pos.y < height - RADIUS)
{
pos.translate(xChange, yChange);
}
if(pos.x < width - RADIUS)
{
pos.translate(xChange, yChange);
}
}

public void updateBallState()
{
if (pos.y + change < height - 3*RADIUS)
{
pos.translate(0, change);
// change++; //accelerate
}
}

//This method can be called with a provided graphics context
//to draw the ball in its current state
public void draw(Graphics g)
{
g.setColor(ballColour);
g.fillOval(pos.x - RADIUS, pos.y - RADIUS, 2*RADIUS, 2*RADIUS);
}

public void bounce()
{
yChange = -yChange;
pos.translate(xChange, yChange);
}

public Point getPosition()
{
return pos;
}
}

球赛

package movingball;

import java.awt.Graphics;
import java.awt.event.*;

public class BallGame extends MovingBall
{

private Paddle myPaddle = new Paddle(FRAME_WIDTH, FRAME_HEIGHT);


public BallGame(String title)
{
super(title);
addKeyListener(new KeyList());
}

public void paint(Graphics g)
{
super.paint(g);
myPaddle.paint(g);
if(isContact())
{
myBall.bounce();
}
else
{
myPaddle.paint(g);
}
}

public boolean isContact()
{
if (myPaddle.area().contains(myBall.getPosition()))
{
return true;
}
else
{
return false;
}
}

public class KeyList extends KeyAdapter
{
public void keyPressed(KeyEvent k)
{
if(k.getKeyCode() == KeyEvent.VK_LEFT)
{
myPaddle.moveLeft();
}
if(k.getKeyCode() == KeyEvent.VK_RIGHT)
{
myPaddle.moveRight();
}
}
}
}

移动球类

package movingball;

import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MovingBall extends JFrame
{
protected final int FRAME_WIDTH = 240;
protected final int FRAME_HEIGHT = 320;
protected Ball myBall = new Ball(FRAME_WIDTH, FRAME_HEIGHT);

public MovingBall (String title)
{
super(title);

setSize(FRAME_WIDTH, FRAME_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void paint(Graphics g)
{
super.paint(g);
myBall.draw(g);
}

public void move()
{
while(true)
{
myBall.move();
repaint();
try
{
Thread.sleep(50);
}
catch(InterruptedException e)
{
System.exit(0);
}
}
}
}

桨类

package movingball;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Paddle
{
private Color paddleColour = Color.GREEN;
private int x,y;
private int paddleWidth = 20;
private int paddleHeight = 10;
private int move = 5;


/** Creates a new instance of Paddle */
public Paddle(int frameWidth, int frameHeight)
{

x =(int)(Math.random()*(frameWidth - paddleWidth));
y = frameHeight - paddleHeight;

}

public void moveRight()
{
x = x + move;
}

public void moveLeft()
{
x = x - move;
}

public Rectangle area()
{
return new Rectangle(x,y, paddleWidth, paddleHeight);
}

public void paint(Graphics g)
{
g.setColor(paddleColour);
g.fillRect(x,y,paddleWidth, paddleHeight);
}
}

最佳答案

这里有一些可以帮助您入门的提示。我有很多建议,但我这么说只是为了让你比现在更进一步。您希望 JFrame 是双缓冲的。这是第一步。为此,请在使其可见后为 JFrame 创建一个新的缓冲区策略:

world.setVisible(true);
world.createBufferStrategy(2);

至于为什么你的Paddle不画画?你会踢自己的。看这一行:

MovingBall  world  =  new  MovingBall("Moving  Ball");

您实际上并没有创建一个 BallGame,它包含绘制 Racket 的逻辑! (BallGame.paint()) 尝试:

BallGame world  =  new  BallGame("Moving  Ball");

关于java - 帮助向 JFrame 添加桨,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4693255/

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