我的物体闪了一秒然后就消失了

时间:2013-12-19 03:20:43

标签: java applet abstract-class repaint

我有一些代码可以在屏幕上显示273个黑色侍从,但它们只显示了一秒钟。我知道重绘有些不对劲但我找不到它。

import java.applet.*;
import java.util.*;
import java.awt.*;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.event.*;

public class Start extends Applet implements Runnable, KeyListener
{
    public boolean running = true;

    int fall = 60;
    int count = 0;
    Thread thread;
    private Image i;
    private Graphics doubleG;
    Brick b[] = new Brick[273];//abtsert class
    public int[] x = {270,300,330,360,390,420,450,480,510,540,570,600,630};
    public int[] y = {-10,20,50,80,110,140,170,200,230,260,290,320,350,380,410,440,470,500,530,560,590,620,650};
    public boolean[]  bl = {false,true};
    public int[]  color = {0,1,2,3,4};
    public int xc = 0;//indx of x
    public int yc = 0;//indx of y
    public int indx = 0;//indx of b
    public int start = 0;
public void init() 
{
    Arrays.fill(b,new Background());
    addKeyListener(this);
    setFocusable(true);
    thread = new Thread(this);
    thread.start();
}
public void run() 
{
    while(running)
    {
        try
        {
            thread.sleep(17);
        }
        catch(InterruptedException e)
        {

        }
        repaint();
    }
}
public void paint(Graphics g)
{
    resize(700,700);
    if(start == 0)
    {
        set(g);//sets fills b with objects
        start = 1;
    }
    else//prints out the abjects stored in b[]
    {
        while(indx != 273)
        {
            b[indx].paint(g);
            indx++;
        }
        indx=0;
    }
    g.drawRect(300,50,300,600);
}
public void update(Graphics g)
{
    if(i == null)
    {
        i = createImage(this.getSize().width, this.getSize().height);
        doubleG = i.getGraphics();
    }
        doubleG.setColor(getBackground());
        doubleG.fillRect(0,0,700,700);

        doubleG.setColor(getForeground());
        paint(doubleG);

        g.drawImage(i,0,0,this);
    }
    public void keyPressed(KeyEvent e)
    {
        if(e.getKeyCode() == e.VK_LEFT)
        {

        }
            if(e.getKeyCode() == e.VK_RIGHT)
        {

        }
        repaint();
    }
    public void keyTyped(KeyEvent e)
    {
    }
    public void keyReleased(KeyEvent e)
    {

    }
    public void set(Graphics g)
    {
       while(yc <= 22)
       {
            b[indx].start(g,x[xc],y[yc]);
            if(xc <= 12)
            {
                xc++;
            }
            if(xc == 12)
            {
                yc++;
                xc=0;
            }
            indx++;
       }

       yc=0;
       indx=0;
    }
 }

这是抽象类

import java.applet.*;
import java.util.*;
import java.awt.*;

public abstract class Brick
{
    public int px;
    public int py;
    public abstract void paint(Graphics g);
    public abstract void start(Graphics g ,int x ,int y);
   }

这是使用抽象类

的类
import java.applet.*;
import java.util.*;
import java.awt.*;

public class Background extends Brick//thing thats being painted
{
    public int px;//where the x,y are stored
    public int py;
    public void paint(Graphics g)//used to repaint it
    {
         g.setColor(Color.BLACK);
         g.fillRect(px,py,29,29);
    }
    public void start(Graphics g,int x, int y)//used to set the object
    {
        g.setColor(Color.BLACK);
        g.fillRect(x,y,29,29);
        px = x;
        py = y; 
    }
}

0 个答案:

没有答案