部分代码没有正确执行

时间:2013-11-23 20:02:39

标签: 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 = 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){}
    }

如果你需要在这里运行或测试的全部内容都是评论,说明一切是什么以及我正在尝试做什么

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;}
}

1 个答案:

答案 0 :(得分:0)

你的坐标是向后的:

-public void eraseCircle(Graphics g, int x, int y)
-c.eraseCircle(g,temp.getY(),temp.getX());

切换x和y,它应该可以工作。

编辑: 好的问题是你的坐标中的x和y总是为0所以我修改了你的drawCircle方法来返回正确的坐标,你的paint方法来存储它们,所以这就是你得到的:

漆:

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);
}

为了确定这一点,我在paint方法中设置了一个断点,并通过eclipses调试器查看了temp的值。