Java 部分代码执行不正确
我正在了解学校里排队的情况,我们的一个实验室出现了问题,我相信问题就在这里,问题是到了50岁以后,它不会消除圆圈Java 部分代码执行不正确,java,queue,Java,Queue,我正在了解学校里排队的情况,我们的一个实验室出现了问题,我相信问题就在这里,问题是到了50岁以后,它不会消除圆圈 public void paint(Graphics g) { int incX = 5; // initial x increment for circle locations int incY = 5; // initial y increment for circle locations Coord temp =
public void paint(Graphics g)
{
int incX = 5; // initial x increment for circle locations
int incY = 5; // initial y increment for circle locations
Coord temp = new Coord(0,0);
Queue<Coord> q = new LinkedList<Coord>();
Circle c = new Circle(g,circleSize,incX,incY,TIME_DELAY);
try
{
for(int i = 1; i <= TOTAL_NUM_CIRCLES; i++)
{
if(q.size() >= 50)
{
temp = q.remove();
c.eraseCircle(g,temp.getX(),temp.getY());
}
temp = new Coord(getX(),getY());
q.add(temp);
c.drawCircle(g);
c.hitEdge();
}
}
catch(InterruptedException e){}
}
public void绘制(图形g)
{
int incX=5;//圆位置的初始x增量
int incY=5;//圆位置的初始y增量
坐标温度=新坐标(0,0);
队列q=新的LinkedList();
圆圈c=新圆圈(g,圆圈大小,incX,incY,延时);
尝试
{
对于(int i=1;i=50)
{
温度=q.移除();
c、 橡皮擦圆(g,temp.getX(),temp.getY());
}
temp=新坐标(getX(),getY());
q、 添加(临时);
c、 拉丝圈(g);
c、 hitEdge();
}
}
捕获(中断异常e){}
}
如果您需要运行或测试整个程序,请在此处添加注释,说明所有内容以及我正在尝试执行的操作
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
/**
* Creates an instance of the GfxApp class, which uses the Circle class, Coord class, and
* a queue to create a screen saver.
* @param args not used
*/
public class ScreenSaver
{
public static void main(String args[])
{
GfxApp gfx = new GfxApp();
}
}
/**
* Creates a Screen Saver by placing Circle coordinates in a queue
*/
class GfxApp extends JFrame
{
private int circleCount, circleSize;
public static final int TIME_DELAY = 10; // controls the speed
public static final int TOTAL_NUM_CIRCLES = 1000; // controls how long it goes
/**
* Creates a GfxApp with 50 circles with diameter 30
*/
public GfxApp()
{
circleCount = 50;
circleSize = 30;
setSize(800,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
/**
* Draws a stream of circleCount circles of size circleSize. Uses a queue to erase circles
* at the end of the stream. The total number of circles that will be drawn is 2000.
* @param g the Graphics object
*/
public void paint(Graphics g)
{
int incX = 5; // initial x increment for circle locations
int incY = 5; // initial y increment for circle locations
Coord temp = new Coord(0,0);
Queue<Coord> q = new LinkedList<Coord>();
Circle c = new Circle(g,circleSize,incX,incY,TIME_DELAY);
try
{
for(int i = 1; i <= TOTAL_NUM_CIRCLES; i++)
{
if(q.size() >= 50)
{
temp = q.remove();
c.eraseCircle(g,temp.getY(),temp.getX());
}
temp = new Coord(getX(),getY());
q.add(temp);
c.drawCircle(g);
c.hitEdge();
}
}
catch(InterruptedException e){}
}
}
/**
* A class to represent Circle objects. Circles can be drawn and erased.
*/
class Circle
{
private int tlX; // top-left X coordinate
private int tlY; // top-left Y coordinate
private int incX; // increment movement of X coordinate
private int incY; // increment movement of Y coordinate
private boolean addX; // flag to determine add/subtract of increment for X
private boolean addY; // flag to determine add/subtract of increment for Y
private int size; // diameter of the circle
private int timeDelay; // time delay until next circle is drawn
/**
* Creates a Circle with a specified Graphics, size, x increment, y increment and time delay
*/
public Circle(Graphics g, int s, int x, int y, int td)
{
incX = x;
incY = y;
size = s;
addX = true;
addY = false;
tlX = 400;
tlY = 300;
timeDelay = td;
}
/**
* returns the top left X of this circle
* @return tlX
*/
public int getTLX() { return tlX;}
/**
* returns the top left Y of this circle
* @return tlY
*/
public int getTLY() { return tlY;}
/**
* delays the program for a specified number of miliseconds
* @param n number of miliseconds
*/
public void delay(int n) throws InterruptedException
{
Thread.sleep(n);
}
/**
* draws a blue circle and sets the tlX and tlY for the next drawing
* @param g Graphics object
*/
public void drawCircle(Graphics g) throws InterruptedException
{
g.setColor(Color.blue);
g.drawOval(tlX,tlY,size,size);
delay(timeDelay);
if (addX)
tlX+=incX;
else
tlX-=incX;
if (addY)
tlY+=incY;
else
tlY-=incY;
}
/**
* Randomly sets a new direction for the circle by randomly setting
* the x increment and y increment
*/
public void newData()
{
incX = (int) Math.round(Math.random() * 7 + 5);
incY = (int) Math.round(Math.random() * 7 + 5);
}
/**
* Determines if any of the four edges have been hit, and if so, reverses the
* appropriate flags (addX and addY) and calls newData
*/
public void hitEdge()
{
boolean a = false;
if (tlX < incX)
{
addX = true;
a = true;
}
if (tlX > 800 - (30 + incX))
{
addX = false;
a = true;
}
if (tlY < incY + 30)
{
addY = true;
a = true;
}
if (tlY > 600 - (30 + incY))
{
addY = false;
a = true;
}
if (a)
newData();
}
// add an eraseCircle method
public void eraseCircle(Graphics g, int x, int y)
{
g.setColor(Color.black);
g.drawOval(x,y,size,size);
}
}
// Create a Coord class, so that coordinates of drawn circles can be placed in the queue.
// As coordinates are removed from the queue, circles are erased with eraseCircle.
class Coord
{
private int x;
private int y;
public Coord(int a, int b)
{
x=a;
y=b;
}
public int getX(){return x;}
public int getY(){return y;}
public int setX(int a){x=a; return x;}
public int setY(int b){y=b; return y;}
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入java.util.*;
/**
*创建GfxApp类的实例,该类使用Circle类、Coord类和
*创建屏幕保护程序的队列。
*@param参数未使用
*/
公共类屏幕保护程序
{
公共静态void main(字符串参数[])
{
GfxApp gfx=新的GfxApp();
}
}
/**
*通过在队列中放置圆坐标创建屏幕保护程序
*/
类GfxApp扩展了JFrame
{
私有int circleCount,circleSize;
public static final int TIME_DELAY=10;//控制速度
public static final int TOTAL_NUM_CIRCLES=1000;//控制它运行的时间
/**
*创建具有50个直径为30的圆的GfxApp
*/
公共GfxApp()
{
循环计数=50;
圆圈大小=30;
设置大小(800600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(真);
}
/**
*绘制大小为circleSize的circleCount圆流。使用队列擦除圆
*在流的末尾。将绘制的圆圈总数为2000。
*@param g图形对象
*/
公共空间涂料(图g)
{
int incX=5;//圆位置的初始x增量
int incY=5;//圆位置的初始y增量
坐标温度=新坐标(0,0);
队列q=新的LinkedList();
圆圈c=新圆圈(g,圆圈大小,incX,incY,延时);
尝试
{
对于(int i=1;i=50)
{
温度=q.移除();
c、 (g,temp.getY(),temp.getX());
}
temp=新坐标(getX(),getY());
q、 添加(临时);
c、 拉丝圈(g);
c、 hitEdge();
}
}
捕获(中断异常e){}
}
}
/**
*表示圆形对象的类。圆圈可以画出来,也可以擦去。
*/
班级圈子
{
private int tlX;//左上角X坐标
private int tlY;//左上Y坐标
private int incX;//X坐标的增量移动
private int incY;//Y坐标的增量移动
private boolean addX;//用于确定X增量的加/减的标志
private boolean addY;//用于确定Y增量的加/减的标志
私有int size;//圆的直径
private int timeDelay;//绘制下一个圆之前的时间延迟
/**
*创建具有指定图形、大小、x增量、y增量和时间延迟的圆
*/
公共圈(图形g、整数s、整数x、整数y、整数td)
{
incX=x;
incY=y;
尺寸=s;
addX=真;
addY=假;
tlX=400;
tlY=300;
时间延迟=td;
}
/**
*返回此圆的左上角X
*@return-tlX
*/
public int getTLX(){return tlX;}
/**
*返回此圆的左上角Y
*@returnly
*/
public int getTLY(){return tlY;}
/**
*将程序延迟指定的毫秒数
*@param n毫秒数
*/
公共无效延迟(int n)引发中断异常
{
睡眠(n);
}
/**
*绘制蓝色圆圈,并为下一个图形设置tlX和tlY
*@param g图形对象
*/
public void drawCircle(图g)抛出InterruptedException
{
g、 setColor(Color.blue);
g、 drawOval(tlX、tlY、尺寸、尺寸);
延迟(时间延迟);
如果(addX)
tlX+=incX;
其他的
tlX-=incX;
如果(艾迪)
tlY+=incY;
其他的
tlY-=incY;
}
/**
*通过随机设置,随机设置圆的新方向
*x增量和y增量
*/
public void newData()
{
incX=(int)Math.round(Math.random()*7+5);
incY=(int)Math.round(Math.random()*7+5);
}
/**
*确定四条边中是否有任何一条已命中,如果是,则反转
*适当的标志(addX和addY)并调用newData
*/
公共无效hitEdge()
{
布尔值a=假;
如果(tlX800-(30+增量))
{
addX=假;
a=真;
}
如果(tlY600-(30+incY))
{
addY=假;
a=真;
}
如果(a)
newData();
}
//添加一个橡皮擦圆方法
公共圆(图形g、整数x、整数y)
{
g、 设置颜色(颜色为黑色);
g、 Draw椭圆形(x、y、尺寸、尺寸);
}
}
//创建一个Coord类,以便绘制cir的坐标
-public void eraseCircle(Graphics g, int x, int y)
-c.eraseCircle(g,temp.getY(),temp.getX());
public void paint(Graphics g)
{
int incX = 5; // initial x increment for circle locations
int incY = 5; // initial y increment for circle locations
Coord temp = new Coord(0,0);
Queue<Coord> q = new LinkedList<Coord>();
Circle c = new Circle(g,circleSize,incX,incY,TIME_DELAY);
try
{
for(int i = 1; i <= TOTAL_NUM_CIRCLES; i++)
{
if(q.size() >= 50)
{
temp = q.remove();
c.eraseCircle(g,temp.getX(),temp.getY());
}
temp = new Coord(getX(),getY());
//q.add(temp);
q.add(c.drawCircle(g));
c.hitEdge();
}
}
catch(InterruptedException e){}
}
/**
* draws a blue circle and sets the tlX and tlY for the next drawing
* @param g Graphics object
* @return
*/
public Coord drawCircle(Graphics g) throws InterruptedException
{
g.setColor(Color.blue);
g.drawOval(tlX,tlY,size,size);
delay(timeDelay);
if (addX)
tlX+=incX;
else
tlX-=incX;
if (addY)
tlY+=incY;
else
tlY-=incY;
return new Coord(tlX, tlY);
}