Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Double buffer problems
Index -> Programming, Java -> Java Help
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
freeridin101




PostPosted: Tue Mar 14, 2006 2:58 pm   Post subject: (No subject)

oh well now i no the basics of them fro later
Sponsor
Sponsor
Sponsor
sponsor
rizzix




PostPosted: Tue Mar 14, 2006 6:23 pm   Post subject: (No subject)

Ah.. this is the first time I've actually attempted to try out this Applet thing. (had to do it sometime anyway).

Apparently, they don't work like normal swing applications. You are required to repaint the component your self. Hence threads are pretty much necessary.

anyway.. here's a fantastic tutorial: http://www.javaworld.com/javaworld/jw-03-1996/jw-03-animation.html


Or you can try out my very cool and simple TimedBufferedApplet:

Java:
import java.awt.*;

public class TestApplet extends TimedBufferedApplet {
   
    public void drawship(Graphics2D g, int x, int y) {
        g.setColor (Color.green);
        g.fillOval (x, y, 10, 20);
        g.setColor (Color.red);
        g.fillOval (x - 10, y + 10, 30, 10);
        g.setColor (Color.black);
        g.fillOval (x + 3, y + 4, 4, 4);
    }
   
    @Override public void offscreenPaint(Graphics2D g, int frame) {
        drawship(g, frame, 0);
    }
}


Attached is the TimedBufferedApplet. What is does is basically "tick" at regular intervals upon which it will repaint the screen. All you are required to do is Override the offscreenPaint method as above.. Smile

Now that method has an extra agrument called "frame" well that basically tells you which frame is being drawn out.

By default the tick_interval is i think 100msec. If you find this too slow or too fast, you can change it by calling setTickInterval(some_int); Oh and there's a very special way you need to set this up:

Java:
@Override public void init() {
    setTickInterval(some_int);
    setBackground(Color.black);
    super.init();
}


Never make it Zero! Not a good idea, we have to give the awt thread a chance to excute, or else you won't see anything on screen. Yes, the ticking occurs in a different thread (this is why I said threads are sort-of necessary). Also, since this version is not thread safe, dont even think of trying to change the tick interval outside the init method.



TimedBufferedApplet.java
 Description:

Download
 Filename:  TimedBufferedApplet.java
 Filesize:  5.77 KB
 Downloaded:  929 Time(s)

freeridin101




PostPosted: Tue Mar 14, 2006 9:27 pm   Post subject: (No subject)

i cant run it i get an error about the ""@Override"" part. It says:

Syntax: Unexpected input discarded
rizzix




PostPosted: Tue Mar 14, 2006 9:28 pm   Post subject: (No subject)

you need java 1.5 ... or u can just take out the @Override part all together.
freeridin101




PostPosted: Tue Mar 14, 2006 9:31 pm   Post subject: (No subject)

upon futher inspection i am geting all sorts of errors right down to it cant find javax.swing. could this be because i am using ready to program?
rizzix




PostPosted: Tue Mar 14, 2006 9:32 pm   Post subject: (No subject)

do u get these erros after u removed the @Override?
freeridin101




PostPosted: Tue Mar 14, 2006 9:40 pm   Post subject: (No subject)

then i get a hole bunch more
rizzix




PostPosted: Tue Mar 14, 2006 9:42 pm   Post subject: (No subject)

do it from the command line... and tell me what errors you get.

CommandPrompt:
cd to\current\directory
\path\to\javac.exe -classpath . *.java > errors.txt
Sponsor
Sponsor
Sponsor
sponsor
freeridin101




PostPosted: Tue Mar 14, 2006 9:45 pm   Post subject: (No subject)

you've lost me
rizzix




PostPosted: Tue Mar 14, 2006 9:47 pm   Post subject: (No subject)

read the first post of: http://www.compsci.ca/v2/viewtopic.php?t=9576

Basically open ur Command Prompt.. and type the appropriate lines.. in the window.
rizzix




PostPosted: Tue Mar 14, 2006 9:50 pm   Post subject: (No subject)

For example..

if my .java files were in C:\
and my javac was in \Progarm Files\Java\jdk1.5.0_06\bin\

the it would look like this:

CommandPrompt:
cd \
"\Progarm Files\Java\jdk1.5.0_06\bin\javac.exe" -classpath . *.java > errors.txt
McKenzie




PostPosted: Tue Mar 14, 2006 11:20 pm   Post subject: (No subject)

Your code was not far off. Here it is with a bit of nip-and-tuck:
code:
import java.applet.*;
import java.awt.*;

public class doublebufferball extends Applet implements Runnable
{
    int y, score, direction;
    private Image dbImage;
    private Graphics dbg;
    static Graphics g;
    static void delay (int del)
    {
        try
        {
            Thread.sleep (del);
        }
        catch (InterruptedException e)
        {
        }
    }


    public void init ()
    {
            direction = 1;
            y = 10;
            score = 0;
            resize (800, 600);
    }


    public void start ()
    {
        Thread th = new Thread (this);
        th.start ();
    }

    public void run ()
    {
        while (score < 10)
        {
            y = y + direction;
            if (y > 590)
                direction = -1;
            if (y < 0)
                direction = 1;
            repaint ();
            delay(30);
        }
    }


    public void update (Graphics g)
    {
        if (dbImage == null)
        {
            dbImage = createImage (this.getSize ().width, this.getSize ().height);
            dbg = dbImage.getGraphics ();
        }

        dbg.setColor (getBackground ());
        dbg.fillRect (0, 0, this.getSize ().width, this.getSize ().height);
       
        dbg.setColor (getForeground ());
        drawship(10,y);
        g.drawImage (dbImage, 0, 0, this);

    }


    public void drawship (int x, int y)
    {
        dbg.setColor (Color.green);
        dbg.fillOval (x, y, 10, 20);
        dbg.setColor (Color.red);
        dbg.fillOval (x - 10, y + 10, 30, 10);
        dbg.setColor (Color.black);
        dbg.fillOval (x + 3, y + 4, 4, 4);
    }

    public void paint (Graphics g)
    {
    }
}
freeridin101




PostPosted: Fri Mar 17, 2006 3:37 pm   Post subject: (No subject)

am i not putting things in the write spot or is this doulbe buffer not working.i tworks for the computer controlled ship but if you move the mouse up and down fast you see doubles any thoughts??
code:


import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class doublebufferworks extends Applet implements Runnable
{
    int y = 0, y2 = 295, x3 = 0, y3 = 0, score = 0, direction = 0;
    private Image dbImage;
    private Graphics dbg;
    static Graphics g;

    static void delay (int del)
    {
        try
        {
            Thread.sleep (del);
        }
        catch (InterruptedException e)
        {
        }
    }


    public void init ()
    {
        direction = 1;
        y = 10;
        score = 0;
        resize (800, 600);
    }


    public void start ()
    {
        Thread th = new Thread (this);
        th.start ();
    }


    public void MoveEnemyShip ()
    {
        while (score < 10)
        {
            y = y + direction;
            if (y > 590)
                direction = -1;
            if (y < 0)
                direction = 1;
            repaint ();
            delay (15);
        }
    }


    // public boolean keyDown (Event e, int key)
    // {
    //     if (key == 1004)
    //     {
    //         y2 = y2 - 2;
    //     }
    //
    //     //System.out.println ("Charakter: " + (char)key + " Integer Value: " + key);
    //     if (key == 1005)
    //     {
    //         y2 = y2 + 2;
    //     }
    //     return true;
    // }

    public boolean mouseMove (Event evt, int x3, int y3)
    {
        this.y2 = y3;
        return true;
    }


    public void run ()
    {
        MoveEnemyShip ();
    }


    public void update (Graphics g)
    {
        if (dbImage == null)
        {
            dbImage = createImage (this.getSize ().width, this.getSize ().height);
            dbg = dbImage.getGraphics ();
        }

        dbg.setColor (getBackground ());
        dbg.fillRect (0, 0, this.getSize ().width, this.getSize ().height);
        dbg.setColor (getForeground ());
        drawship (10, y);
        drawship (770, y2);
        g.drawImage (dbImage, 0, 0, this);

    }


    public void drawship (int x, int y)
    {
        dbg.setColor (Color.green);
        dbg.fillOval (x, y, 10, 20);
        dbg.setColor (Color.red);
        dbg.fillOval (x - 10, y + 10, 30, 10);
        dbg.setColor (Color.black);
        dbg.fillOval (x + 3, y + 4, 4, 4);
    }


    public void paint (Graphics g)
    {

    }
}
McKenzie




PostPosted: Fri Mar 17, 2006 11:25 pm   Post subject: (No subject)

If I'm not mistaken your code is fine. Looks like an optical illusion. Move your mouse across the screen quickly and you will get the same effect.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 29 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: