Java绘图程序双缓冲

时间:2016-11-15 14:48:59

标签: java applet awt java-2d

所以我正在学校制作一个绘画程序,现在我必须加倍缓冲它。经过一周的搜索和在线使用教程我仍然无法正确缓冲我的程序...我写了一个双缓冲功能,但你不能画任何东西......当我禁用双缓冲区,你可以画好但屏幕闪烁。请帮忙

(是的我知道我的一些代码已被弃用,我应该修复它)

代码:

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

public class PaintRun extends Applet
{

    static int posx = 0;
    static int posy = 0;

    static int posxOld = 0;
    static int posyOld = 0;

    static int size = 20;

    static int toolNum = 0;

    static boolean ready = false;

    static Color col =  new Color(120,120,120);

    static Button clear = new Button(0,0,20,'C');
    static Button sizeInc = new Button(20,0,20,'+');
    static Button sizeDec = new Button(40,0,20,'-');

    static Tools tool = new Tools(posx,posy);

    public void paint(Graphics g)
    {
        drawButtons(g);
        detectButtons(g);
    }

    private static void detectButtons(Graphics g)
    {       
        if (clear.detect(posx,posy) == true)
        {
            g.setColor(Color.WHITE);
            g.fillRect(0, 0, 800000, 800000);
            drawButtons(g);
        }
        else if (sizeInc.detect(posx, posy))
        {
            size+=2;
            drawButtons(g);
        }
        else if (sizeDec.detect(posx, posy))
        {
            size-=2;
            if (size <= 2)
                size = 2;
            drawButtons(g);
        }
        else
        {
            if (ready == true)
            {
                g.setColor(Color.BLACK);
                g.drawLine(posxOld, posyOld, posx, posy);
                drawButtons(g);
            }
        }
    }

    private static void drawButtons(Graphics g)
    {
        clear.setColor(col, Color.BLACK);
        sizeInc.setColor(col, Color.BLACK);
        sizeDec.setColor(col, Color.BLACK);

        clear.drawButton(g);
        sizeInc.drawButton(g);
        sizeDec.drawButton(g);
    }

    public boolean mouseDown(Event e, int x, int y)
    {
        posx = x;
        posy = y;

        posxOld = x;
        posyOld = y;

        ready = true;

        repaint();
        return true;
    }

    public boolean mouseDrag(Event e, int x, int y)
    {
        posxOld = posx;
        posyOld = posy;

        posx = x;
        posy = y;

        repaint();
        return true;
    }

    // Normal Update function

    public void update(Graphics g)
    {
        paint(g);
    }

    // Double buffered Update function

    /*public void update(Graphics g) 
    {
        Graphics offgc;
        Image offscreen = null;
        Dimension d = size();

        offscreen = createImage(d.width, d.height);
        offgc = offscreen.getGraphics();

        offgc.setColor(getBackground());
        offgc.fillRect(0, 0, d.width, d.height);
        offgc.setColor(getForeground());

        paint(offgc);

        g.drawImage(offscreen, 0, 0, this);
    }
    */
}

class Button extends Applet
{

    int minX; // X coord where the button is drawn
    int minY; // Y coord where the button is drawn
    int maxX; // X size
    int maxY; // Y size

    char letter;

    Color back;
    Color line;

    // Constructor block

    public Button(char desc)
    {
        minX = 0;
        minY = 0;

        maxX = 40;
        maxY = 40;

        back = new Color(255,255,255);
        line = new Color(0,0,0);

        letter = desc;
    }

    public Button(int x, int y, char desc)
    {
        minX = x;
        minY = y;

        maxX = 40;
        maxY = 40;

        back = new Color(255,255,255);
        line = new Color(0,0,0);

        letter = desc;
    }

    public Button(int x, int y, int size, char desc)
    {
        minX = x;
        minY = y;

        maxX = size;
        maxY = size;

        back = new Color(255,255,255);
        line = new Color(0,0,0);

        letter = desc;
    }

    public Button(int x, int y, int sizeX, int sizeY, char desc)
    {
        minX = x;
        minY = y;

        maxX = sizeX;
        maxY = sizeY;

        back = new Color(255,255,255);
        line = new Color(0,0,0);

        letter = desc;
    }

    // Draws the button

    public void drawButton(Graphics g)
    {
        g.setColor(back);
        g.fillRect(minX, minY, maxX, maxY);

        g.setColor(line);
        g.drawRect(minX, minY, maxX, maxY);
        g.drawString(Character.toString(letter), (minX+maxX/2)-4, (minY+maxY/2)+4);
    }

    // Sets the button colors

    public void setColor(Color b, Color l)
    {
        back = b;
        line = l;
    }

    // Detects if those coordinates are in the button location or not

    public boolean detect(int x, int y)
    {
        boolean flag = false;

            if (x > minX && x < (minX + maxX) && y > minY && y < (minY + maxY))
                flag = true;
            else
                flag = false;

        return flag;
    }
}

0 个答案:

没有答案
相关问题