Keresés

Új hozzászólás Aktív témák

  • robisz

    senior tag

    válasz concret_hp #702 üzenetére

    Helló! A lényeg, hogy először egy BufferedImage objektumra kell rajzolni
    (ezt a paintBuffer metódusba tettem át) és csak a kész képet rajzoljuk
    át a komponensre.

    import java.awt.*;
    import java.awt.event.*;

    public class Mozgas extends Frame {

    private int poz;
    private Image bufferImage;
    private Graphics bufferGraphics;

    public Mozgas() {
    super(''Mozgas'');
    setSize(500,500);
    setBackground(new Color(192,192,192));
    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e){
    System.exit(0);
    }
    });
    }

    public void paint(Graphics g){

    if(bufferGraphics==null){
    bufferImage = createImage (this.getSize().width, this.getSize().height);
    bufferGraphics = bufferImage.getGraphics();
    }

    bufferGraphics.clearRect(0,0,this.getSize().width, this.getSize().height);

    //calls the paintbuffer method with
    //the offscreen graphics as a param
    paintBuffer(bufferGraphics);

    //we finaly paint the offscreen image onto the onscreen image
    g.drawImage(bufferImage,0,0,this);
    }

    public void paintBuffer(Graphics g){
    g.setColor(new Color(255,255,0));
    g.fillRect(50,50,400,400);
    g.setColor(new Color(0,0,255));
    g.fillRect(50+poz,50+poz,50,50);
    }

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

    public void vandorol() {
    for (int c = 1; c < 350; c++) {
    try {
    Thread.sleep(10);
    } catch (InterruptedException e) {
    }
    poz++;
    repaint();
    }
    }

    public static void main(String[] args) {
    Mozgas mw = new Mozgas();
    mw.setVisible(true);
    mw.vandorol();
    }
    }


    Saját komponenst egyébként a Component vagy a Canvas osztályból szokás
    származtatni, de egy ilyen kis példa esetén így is megteszi.

Új hozzászólás Aktív témák