使用双缓冲Java小程序停止小程序闪烁

使用双缓冲Java小程序停止小程序闪烁,java,swing,applet,awt,double-buffering,Java,Swing,Applet,Awt,Double Buffering,很抱歉,我一直在问关于我的程序的问题,但我想我快到了,我正在自学java,所以请耐心听我说。我正在创建一个小程序,当狗对象靠近羊时,它会在屏幕上以随机方向移动羊对象。让羊在一个随机的方向上移动需要一些工作,在你们的帮助下,它现在可以工作了,但我现在要做的是阻止它在屏幕上拖动对象时闪烁。我已经读过关于双缓冲的内容,我可以让它用于在主类的paint方法中绘制的项目,但不能让它用于我的sheep和dog对象,它们被定义为独立类中的独立对象。任何帮助都将不胜感激。这是我的密码: package

很抱歉,我一直在问关于我的程序的问题,但我想我快到了,我正在自学java,所以请耐心听我说。我正在创建一个小程序,当狗对象靠近羊时,它会在屏幕上以随机方向移动羊对象。让羊在一个随机的方向上移动需要一些工作,在你们的帮助下,它现在可以工作了,但我现在要做的是阻止它在屏幕上拖动对象时闪烁。我已经读过关于双缓冲的内容,我可以让它用于在主类的paint方法中绘制的项目,但不能让它用于我的sheep和dog对象,它们被定义为独立类中的独立对象。任何帮助都将不胜感激。这是我的密码:

    package mandAndDog;

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;


public class SheepDog extends Applet implements ActionListener, MouseListener, MouseMotionListener
{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    /**
     * 
     */

    Dog dog;
    Sheep sheep;
    int[] directionNumbersLeft = {0, 1, 3};
    int[] directionNumbersUp = {0, 1, 2};
    int x;
    int selection;
    int xposR;
    int yposR;
    int sheepx;
    int sheepy;
    int sheepBoundsx;
    int sheepBoundsy;
    int MAX_DISTANCE = 50;
    int direction;
    int distance;
    Boolean sheepisclosetodog;


    public void init()
    {
        addMouseListener(this);
        addMouseMotionListener(this);
        dog = new Dog(10, 10);
        sheepx = 175;
        sheepy = 75;
        sheep = new Sheep(sheepx, sheepy);
        sheepBoundsx = 30;
        sheepBoundsy = 30;
        direction = (int)(Math.random()*4); 
        distance = (int) (Math.random() * MAX_DISTANCE) % MAX_DISTANCE;
        sheepisclosetodog = false;
        Random rand = new Random();
        x = rand.nextInt(3);
        selection = directionNumbersLeft[x];

    }
    public void paint(Graphics g)
    {
        dog.display(g);
        sheep.display(g);
        g.drawString(Integer.toString(distance), 85, 100);
        g.drawString(Integer.toString(direction), 85, 125);
        g.drawString(Integer.toString(selection), 85, 140);

    }
    public void actionPerformed(ActionEvent ev)
    {}
    public void mousePressed(MouseEvent e)
    {}
    public void mouseReleased(MouseEvent e)
    {}
    public void mouseEntered(MouseEvent e)
    {}
    public void mouseExited(MouseEvent e)
    {}
    public void mouseMoved(MouseEvent e)
    {
    }
    public void mouseClicked(MouseEvent e)
    {}
    public void mouseDragged(MouseEvent e)
    {
        dog.setLocation(xposR, yposR);
        sheep.setLocation(sheepx, sheepy);
        if (xposR > (sheepx - 20)&& xposR < (sheepx - 20)+(sheepBoundsx - 20) && yposR > (sheepy - 20)
                && yposR < (sheepy - 20)+(sheepBoundsy - 20) && direction == 0){
            sheepx = sheepx + 50;
            direction = (int)(Math.random()*4); 
        }
        if (xposR > (sheepx - 20)&& xposR < (sheepx - 20)+(sheepBoundsx - 20) && yposR > (sheepy - 20)
                && yposR < (sheepy - 20)+(sheepBoundsy - 20) && direction == 1){
            sheepy = sheepy + 50;
            direction = (int)(Math.random()*4); 
        }

        if (xposR > (sheepx - 20)&& xposR < (sheepx - 20)+(sheepBoundsx - 20) && yposR > (sheepy - 20)
                && yposR < (sheepy - 20)+(sheepBoundsy - 20) && direction == 2){
            sheepx = sheepx - 50;
            direction = (int)(Math.random()*4); 
        }
        if (sheepx <= 5){
            direction = directionNumbersLeft[x];
        }


        if (xposR > (sheepx - 20)&& xposR < (sheepx - 20)+(sheepBoundsx - 20) && yposR > (sheepy - 20)
                && yposR < (sheepy - 20)+(sheepBoundsy - 20) && direction == 3){
            sheepy = sheepy - 50;
            direction = (int)(Math.random()*4); 
        }
        if (sheepy <=5){
            direction = directionNumbersUp[x];
        }

        xposR = e.getX();
        yposR = e.getY();
        repaint();

    }
}

class Dog 
{
    int xpos;
    int ypos;
    int circleWidth = 30;
    int circleHeight = 30;

    public Dog(int x, int y)
    {
        xpos = x;
        ypos = y;

    }

    public void setLocation(int lx, int ly)
    {
        xpos = lx;
        ypos = ly;
    }

    public void display(Graphics g)
    {
        g.setColor(Color.blue);
        g.fillOval(xpos, ypos, circleWidth, circleHeight);
    }       
}
class Sheep
{
    int xpos;
    int ypos;
    int circleWidth = 10;
    int circleHeight = 10;

    public Sheep(int x, int y)
    {
        xpos = x;
        ypos = y;

    }

    public void setLocation(int lx, int ly)
    {
        xpos = lx;
        ypos = ly;
    }

    public void display(Graphics g)
    {
        g.setColor(Color.green);
        g.fillOval(xpos , ypos, circleWidth, circleHeight);
        g.drawOval(xpos - 20, ypos - 20, 50, 50);
    }


}

首先,我不太明白为什么在Sheep and Dog类中有一个显示方法。与其这样做,我建议你在你的牧羊犬类中展示绵羊和狗

此外,您还应该使用Graphics2D,而不是使用图形。为了使用此功能,只需执行以下操作

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
}
这是可能的,因为Graphics2D是Graphics的一个子类。一旦您这样做了,我要做的就是重写update方法并执行此操作

public void update(Graphics g) {
    if (image == null) {
        image = createImage(this.getWidth(), this.getHeight());
        graphics = image.getGraphics();
    }
    graphics.setColor(getBackground());
    graphics.fillRect(0,  0,  this.getWidth(),  this.getHeight());
    graphics.setColor(getForeground());
    paint(graphics);
    g.drawImage(image, 0, 0, this);
}
调用repaint时,它实际上首先调用update方法,update方法反过来调用paint方法。在类的顶部,您应该声明

Image image;
Graphics graphics;

最简单的方法:将小程序更改为Swing JApplet,绘制JPanel的paintComponent。。。方法并利用Swing的自动双缓冲。另请参阅。