我不确定我是否可能在EDT上推迟某些事情,或者我是否在滥用某种方法。 如果我将swingtimer的延迟增加到0以上,它就会变慢,但是我没有办法让它变得更快。
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Ellipse2D;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.SwingWorker;
import javax.swing.Timer;
/*
* Name: Owen Rosini Date: May 24, 2016 Program Name: SnakeBrickBreaker.java Description:
*/
@SuppressWarnings("serial")
public class SnakeBrickBreaker extends JFrame implements MouseListener {
private int mouseX, mouseY, pathStartX;
private Rectangle scoreBox = new Rectangle(0, 0, getWidth(), 150);
private int level = 5;
private Ball[] ballList = new Ball[level];
private boolean playing;
private int offset;
private int ballCount = 0;
public SnakeBrickBreaker() {
super("Snake Brick Breaker");
level = 5;
pathStartX = 250 - 20 / 2;
Color backgroundColor = new Color(255, 255, 255);
this.setBackground(backgroundColor);
Timer timer = new Timer(50, update);
timer.start();
addMouseListener(this);
}
@Override
public void mouseClicked(MouseEvent arg0) {
}
@Override
public void mouseEntered(MouseEvent arg0) {}
@Override
public void mouseExited(MouseEvent arg0) {}
@Override
public void mousePressed(MouseEvent arg0) {
}
@Override
public void mouseReleased(MouseEvent arg0) {
mouseX = arg0.getX();
mouseY = arg0.getY();
offset = 0;
playing = true;
addBalls.execute();
}
private SwingWorker<Void, Void> addBalls = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
while (ballCount < level) {
System.out.println("You made it!");
Ellipse2D.Double ball = new Ellipse2D.Double(pathStartX + offset, getHeight() - 20, 20, 20);
Ball b = new Ball(ball, ballCount);
b.determineDx(mouseX, mouseY);
b.determineDy(mouseX, mouseY);
System.out.println(ballCount);
ballList[ballCount] = b;
ballCount++;
System.out.println("Done!");
offset += 50;
}
return null;
}
@Override
protected void done() {
System.out.println("Done adding balls!");
}
};
// *********************************
// BEGINNING OF SELF-CREATED METHODS
// *********************************
Action update = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
for (Ball b : ballList) {
if (b != null && playing == true) {
b.translate();
repaint();
}
if (ballList[0] != null && ballList[0].getDoneMoving()) {
pathStartX = ballList[0].getPathEndX();
}
}
}
};
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(new Color(255, 100, 0));
g2.setFont(new Font("Arial", Font.PLAIN, 10));
g2.draw(scoreBox);
g2.fillRect((int) scoreBox.getX(), (int) scoreBox.getY(), (int) scoreBox.getWidth(),
(int) scoreBox.getHeight());
g2.drawString("Score: " + level, 10, 50);
for (Ball ball : ballList) {
if (ball != null) {
g2.fill(ball.getEllipse2D());
}
}
}
}
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Ellipse2D.Double;
Class Ball
{
private int x,y, num, pathEndX;
double dy, dx;
private Ellipse2D.Double ball;
private boolean active, doneMoving;
private static final int WIDTH = 500;
private static final int HEIGHT = 700;
private static final int TOP_BAR = 150;
public Ball(Double double1, int n)
{
ball = double1;
dx = 1;
dy = 1;
active = false;
num = n;
}
public int getBottom()
{
return (int) (ball.getY() + ball.getHeight());
}
public int getTop()
{
return (int) (ball.getY());
}
public int getLeft()
{
return (int) (ball.getX());
}
public int getRight()
{
return (int) (ball.getX() + ball.getWidth());
}
public Ellipse2D.Double getEllipse2D()
{
return ball;
}
public void determineDx(double xFinal, double yFinal)
{
if(ball.getX() < xFinal)
{
dx = 1;
}
if(ball.getX() > xFinal)
{
dx = -1;
}
}
public void determineDy(double xFinal, double yFinal)
{
double a = ball.getX() - xFinal;
double b = ball.getY() - yFinal;
dy = (dx*b)/a;
}
public double getDx()
{
return dx;
}
public double getDy()
{
return dy;
}
public void changeX(double xChange)
{
ball.x += xChange;
}
public void changeY(double yChange)
{
ball.y += yChange;
}
public void translate()
{
doneMoving = false;
if(ball.getX() <= 0 || ball.getX() >= WIDTH - ball.getWidth())
{
dx = -dx;
}
if(dx == -1)
{
if(ball.getX() >= 0)
{
ball.x = ball.getX() + dx;
ball.y = ball.getY() + dy;
}
}
else if(dx == 1)
{
if(ball.getX() <= WIDTH - ball.getWidth())
{
ball.x = ball.getX() + dx;
ball.y = ball.getY() + dy;
}
}
if(ball.getY() <= TOP_BAR)
{
dy = -dy;
}
if(ball.getY() > HEIGHT - ball.getHeight() + 5)
{
dy = 0;
dx = 0;
ball.y = HEIGHT - ball.getHeight();
pathEndX = (int) ball.x;
doneMoving = true;
}
}
public boolean getDoneMoving()
{
return doneMoving;
}
public int getPathEndX()
{
return pathEndX;
}
}
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
public class Brick
{
private int x,y, count;
private Rectangle brick;
public Brick(Rectangle b, int c)
{
brick = b;
count = c;
}
public int getBottom()
{
return (int) (brick.getY() + brick.getHeight());
}
public int getTop()
{
return (int) (brick.getY());
}
public int getLeft()
{
return (int) (brick.getX());
}
public int getRight()
{
return (int) (brick.getX() + brick.getWidth());
}
public int getCount()
{
return count;
}
public void decreaseCount()
{
count--;
}
}
public class SnakeBrickBreakerRunner
{
public static void main(String[] args)
{
JFrame game = new SnakeBrickBreaker();
game.setSize(500, 700);
game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.setLocationRelativeTo(null);
game.setVisible(true);
}