Advertisement

10.04.2003 at 05:00PM PDT, ID: 20757425
[x]
Attachment Details

flicker problem with runnable canvas in applet

Asked by donirap in Java Programming Language

Tags: canvas, flicker

Hi. I have a canvas that implements Runnable running inside an applet. It is fine, but flickers. Can anyone tell me why it flickers and what I can do about that? Here is the source code for the canvas:


package GUI;

import GCD_Engine.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;


public class MyCanvas extends Canvas implements Runnable {
   
    private Thread mover;
    private static final long interval = 200;
    private static final int x_interval = 5;
    private static final int canvasWidth = 1000000;
    private static final int canvasHeight = 500;
    private GUI gui;
    private int message_width;
    private int message_height;
    private int x;
    private int range;
    private int num;
    private int base;
    private Vector bases_range;
    private int y;
    private int temp;
    private Vector drawn_messages;
    private static final int start = 100;
    private String number;
    private GCD_Engine.Sysclass sysclass;
    private Drawn_Message ddm;
   
    public MyCanvas(GUI gui, Sysclass sysclass)
    {
        this.gui = gui;
        this.sysclass = sysclass;
    }
     
    public void init()
    {
        this.sysclass.begin();
        this.setSize( canvasWidth, canvasHeight);
        this.setBackground(Color.yellow);
        this.drawn_messages = new Vector();
        this.base = 0;
        this.x = 0;
        this.message_width = 50;
        this.message_height = 25;
        this.range = 200;
        this.bases_range = new Vector();
        int num_agents = sysclass.get_num_agents();
        this.num = this.range/num_agents;
        int base = MyCanvas.start;
        for(int i=0; i< num_agents; i++)
        {
            this.bases_range.addElement(new Integer(base));
            base += num;
        }        
        this.y = 0;
        this.temp = 0;    
    }
   
    public void run()
    {
        while(true)
        {
            if(GUI.get_step())
                    GUI.set_paint(false);
            try
            {
                Thread.sleep(this.interval);
            }
            catch(InterruptedException e)
            {
                this.get_gui().showStatus(e.toString());
            }
            this.repaint();
        }
    }

    public void update( Graphics g)
    {
        super.update(g);
        g.drawLine(this.x, 0, this.x, MyCanvas.canvasHeight);
        if(!GUI.get_paint())
            return;
        this.sysclass.simulate();
        this.x += MyCanvas.x_interval;
         
        Vector v = this.sysclass.get_messages();
        Vector messages = this.get_messages(v);
        for(int i=0; i<messages.size(); i++)
        {
            GCD_Engine.Queued_Message qm = (Queued_Message)(messages.elementAt(i));
            Message m = qm.get_m();
            if(m.get_number() != null)
                this.number = m.get_number();
            Drawn_Message dm = new Drawn_Message(qm, this);
            dm.check();
            dm.draw(g);
        }
     }
   
    public static int get_x_interval()
    {
        return MyCanvas.x_interval;
    }
   
    public void add_old_message(Drawn_Message dm)
    {
        this.drawn_messages.addElement(dm);  
    }
   
    public String get_number()
    {
        return this.number;
    }

    public void paint(Graphics g)
    {
//this part draws the agents...
        this.draw_agents(g);
//this part draws the delivered messages...
        this.draw_old_messages(g);
/*
//this part draws the delivered messages...
        if(!GUI.get_paint())
            return;    
        Vector v = this.sysclass.get_messages();
        Vector messages = this.get_messages(v);
       
        for(int i=0; i<messages.size(); i++)
        {
            GCD_Engine.Queued_Message qm = (Queued_Message)(messages.elementAt(i));
            Message m = qm.get_m();
            if(m.get_number() != null)
                this.number = m.get_number();
            Drawn_Message dm = new Drawn_Message(qm, this);
            dm.check();
            dm.draw(g);
        }
*/
}
   
   
    public void draw_old_messages( Graphics g )
    {
        for(int i=0; i< this.drawn_messages.size(); i++)
        {
            Drawn_Message dm = (Drawn_Message)this.drawn_messages.elementAt(i);
            g.drawLine(dm.get_initial_x(), dm.get_initial_y(), dm.get_final_x(), dm.get_final_y());
        }
    }

    public int get_num()
    {
        return this.num;
    }
   
   
    public int get_x()
    {
        return this.x;  
    }
   
    public void draw_agents(Graphics g)
    {
        for(int i=0; i< this.bases_range.size(); i++)
        {
            base =((Integer)(this.bases_range.elementAt(i))).intValue();
            g.drawString((new Integer(i)).toString(), 0, base);
            g.drawLine(0, base, this.canvasWidth, base );
        }
    }
       
    public int get_base(int sender)
    {
        return ((Integer)this.bases_range.elementAt(sender)).intValue();
    }
       
    public void add_x(int more)
    {
        this.x += more;
    }
   
    public void add_y(int more)
    {
        this.y += more;
    }

    public int get_y()
    {
        return this.y;
    }
   
    public Vector get_messages(Vector v)
    {
        Vector messages = new Vector();
        if(v != null)
        {  
            for(int i=0; i< v.size(); i++)
            {
                Vector queue;
                queue = (Vector)v.elementAt(i);
                if(queue != null)
                    for(int j=0; j<queue.size(); j++)
                    {
                        messages.addElement((Queued_Message)(queue.elementAt(j)));
                    }
            }
        }  
        return messages;
    }

    public void draw_envelope(Graphics g, int x, int y)
    {
        g.setColor(Color.black);
        g.drawRect(x, y, this.message_width, this.message_height);
        g.setColor(Color.white);
        g.fillRect(x, y, this.message_width, this.message_height);
        g.setColor(Color.black);
        g.drawLine(x, y, (this.message_width/2 + x), (y + this.message_height/2));
        g.drawLine((x + this.message_width), y, (x + this.message_width/2), (y + this.message_height/2));
    }

    public GUI get_gui()
    {
        return this.gui;
    }

} Start Free Trial
 
Loading Advertisement...
 
[+][-]10.04.2003 at 05:40PM PDT, ID: 9493087

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.04.2003 at 05:43PM PDT, ID: 9493097

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.04.2003 at 05:49PM PDT, ID: 9493125

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.06.2003 at 12:42AM PDT, ID: 9496810

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.15.2003 at 11:43AM PDT, ID: 9556932

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.22.2003 at 06:49PM PDT, ID: 9603742

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Java Programming Language
Tags: canvas, flicker
Sign Up Now!
Solution Provided By: donirap
Participating Experts: 5
Solution Grade: A
 
 
[+][-]10.23.2003 at 01:00PM PDT, ID: 9609735

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.03.2004 at 08:22PM PST, ID: 10036170

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32