小程序停止双缓冲Java小程序闪烁 [英] stop applet flickering with double buffering Java Applet

查看:213
本文介绍了小程序停止双缓冲Java小程序闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉让问我的程序的问题,但我觉得我几乎没有,我自学java的,所以请大家多多包涵。我创建移动羊对象在屏幕上随机方向时,一只狗对象移动靠近羊的小程序。获取随机方向移动羊采取了一些工作,并与你们这里上的帮助,现在的工作(在某种程度上),但我想要现在要做的就是从停止闪烁,当我在屏幕上拖动对象。我读过有关双缓冲,我可以得到它在一个主类的paint方法绘制的项目工作,但不能让它为它们定义为单独的类不同的对象我的羊和狗对象。任何帮助将大大AP preciated。这里是我的code:

 包mandAndDog;进口java.applet.Applet中;
进口java.awt中的*。
java.awt.event中导入*。
进口了java.util.Random;
公共类牧羊犬扩展的Applet实现的ActionListener,的MouseListener,的MouseMotionListener
{
    / **
     *
     * /
    私有静态最后的serialVersionUID长1L =;
    / **
     *
     * /    狗狗;
    羊羊;
    INT [] directionNumbersLeft = {0,1,3};
    INT [] directionNumbersUp = {0,1,2};
    INT X;
    INT选择;
    INT xposR;
    INT yposR;
    INT sheepx;
    INT sheepy;
    INT sheepBoundsx;
    INT sheepBoundsy;
    INT MAX_DISTANCE = 50;
    INT方向;
    INT距离;
    布尔sheepisclosetodog;
    公共无效的init()
    {
        addMouseListener将(本);
        addMouseMotionListener(本);
        狗=新狗(10,10);
        sheepx = 175;
        sheepy = 75;
        羊=新羊(sheepx,sheepy);
        sheepBoundsx = 30;
        sheepBoundsy = 30;
        方向=(int)的(的Math.random()* 4);
        距离=(INT)(的Math.random()* MAX_DISTANCE)%MAX_DISTANCE;
        sheepisclosetodog = FALSE;
        随机兰特=新的随机();
        X = rand.nextInt(3);
        选择= directionNumbersLeft [X];    }
    公共无效漆(图形G)
    {
        dog.display(G);
        sheep.display(G);
        g.drawString(Integer.toString(距离)​​,85,100);
        g.drawString(Integer.toString(方向),85,125);
        g.drawString(Integer.toString(选择),85,140);    }
    公共无效的actionPerformed(ActionEvent的EV)
    {}
    公共无效鼠标pressed(的MouseEvent E)
    {}
    公共无效的mouseReleased(的MouseEvent E)
    {}
    公共无效的mouseEntered(的MouseEvent E)
    {}
    公共无效的mouseExited(的MouseEvent E)
    {}
    公共无效的mouseMoved(的MouseEvent E)
    {
    }
    公共无效的mouseClicked(的MouseEvent E)
    {}
    公共无效的mouseDragged(的MouseEvent E)
    {
        dog.setLocation(xposR,yposR);
        sheep.setLocation(sheepx,sheepy);
        如果(xposR>(sheepx - 20)及与放大器; xposR≤(sheepx - 20)+(sheepBoundsx - 20)及与放大器; yposR>(sheepy - 20)
                &功放;&安培; yposR< (sheepy - 20)+(sheepBoundsy - 20)及与放大器;方向== 0){
            sheepx = sheepx + 50;
            方向=(int)的(的Math.random()* 4);
        }
        如果(xposR>(sheepx - 20)及与放大器; xposR≤(sheepx - 20)+(sheepBoundsx - 20)及与放大器; yposR>(sheepy - 20)
                &功放;&安培; yposR< (sheepy - 20)+(sheepBoundsy - 20)及与放大器;方向== 1){
            sheepy = sheepy + 50;
            方向=(int)的(的Math.random()* 4);
        }        如果(xposR>(sheepx - 20)及与放大器; xposR≤(sheepx - 20)+(sheepBoundsx - 20)及与放大器; yposR>(sheepy - 20)
                &功放;&安培; yposR< (sheepy - 20)+(sheepBoundsy - 20)及与放大器;方向== 2){
            sheepx = sheepx - 50;
            方向=(int)的(的Math.random()* 4);
        }
        如果(sheepx&下; = 5){
            方向= directionNumbersLeft [X];
        }
        如果(xposR>(sheepx - 20)及与放大器; xposR≤(sheepx - 20)+(sheepBoundsx - 20)及与放大器; yposR>(sheepy - 20)
                &功放;&安培; yposR< (sheepy - 20)+(sheepBoundsy - 20)及与放大器;方向== 3){
            sheepy = sheepy - 50;
            方向=(int)的(的Math.random()* 4);
        }
        如果(sheepy&下; = 5){
            方向= directionNumbersUp [X];
        }        xposR = e.getX();
        yposR = e.getY();
        重绘();    }
}类犬
{
    INT XPOS;
    INT ypos;
    INT circleWidth = 30;
    INT circleHeight = 30;    公共犬(INT X,int y)对
    {
        XPOS = X;
        ypos = Y;    }    公共无效的setLocation(INT LX,诠释LY)
    {
        XPOS = LX;
        ypos = LY;
    }    公共无效显示(图形G)
    {
        g.setColor(Color.blue);
        g.fillOval(XPOS,ypos,circleWidth,circleHeight);
    }
}
羊类
{
    INT XPOS;
    INT ypos;
    INT circleWidth = 10;
    INT circleHeight = 10;    公共羊(INT X,int y)对
    {
        XPOS = X;
        ypos = Y;    }    公共无效的setLocation(INT LX,诠释LY)
    {
        XPOS = LX;
        ypos = LY;
    }    公共无效显示(图形G)
    {
        g.setColor(Color.green);
        g.fillOval(XPOS,ypos,circleWidth,circleHeight);
        g.drawOval(XPOS - 20,ypos - 20,50,50);
    }
}


解决方案

首先,我不明白究竟为什么你有你的羊和狗类中的显示方法。相反,这样做的,我建议你显示你的牧羊犬类中羊和狗。

此外,而不是使用图形,你应该使用的Graphics2D。为了使用这一简单地做

 公共无效漆(图形G){
    Graphics2D的G2 =(Graphics2D的)克;
}

这是可能的,因为Graphics2D的是显卡的一个子类。一旦你这样做,我会做的是覆盖update()方法,并做到这一点。

 公共无效更新(图形G){
    如果(像== NULL){
        图像=的createImage(this.getWidth(),this.getHeight());
        图形= image.getGraphics();
    }
    graphics.setColor(的getBackground());
    graphics.fillRect(0,0,this.getWidth(),this.getHeight());
    graphics.setColor(getForeground());
    油漆(图形);
    g.drawImage(图像,0,0,这一点);
}

当你调用重绘(),它实际上首先调用update()方法,这反过来又调用paint()方法。在课程上,你应该声明

 图像的图像;
图形显卡;

sorry to keep asking questions about my program but i think i'm nearly there and i'm teaching myself java so please bear with me. I'm creating an applet that moves sheep object across the screen in a random direction when a dog object moves close to the sheep. Getting the sheep to move in a random direction took some work and with the help of you guys on here it now works (sort of) but what I'm trying to do now is stop it from flickering when i drag objects across the screen. I've read about double buffering, I can get it to work for items drawn in the paint method of a main class but cant get it to work for my sheep and dog objects which are defined as separate objects in separate classes. Any help will be much appreciated. Here is my code:

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


}

解决方案

First of all, I dont exactly understand why you have a display method inside your Sheep and Dog class. Instead of doing that, I suggest you display the sheep and dog inside your SheepDog class.

Also instead of using Graphics, you should use Graphics2D. In order to use this simply do

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
}

This is possible because Graphics2D is a subclass of Graphics. Once you do that, what I would do is override the update() method and do this

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

When you call repaint(), it actually first calls the update() method, which in turn calls the paint() method. Towards the top of the class, you should declare

Image image;
Graphics graphics;

这篇关于小程序停止双缓冲Java小程序闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆