JNI native drawing - Double Buffering issue

Hi,
I use JNI to paint from a C++ DLL onto a canvas, which works fine.
The problem is that double buffering is not working though.
In C++ I create an offscreen HDC, draw something on it and finally i bitblit the offscreen HDC to the parent window HDC.
That is how double buffering is supposed to work. But each time i repaint my Canvas in Java, i see flickering:
the background color is displayed first, then the C++ paintings are drawn.
Since i already do double buffering in my C++ drawings, i don't know why there still is flickering.
Should i do double buffering on my canvas also?
How would this work, since my paint method looks like;
     private native void drawChart(Graphics g);
     public void paint(Graphics g)
          drawChart(g);
     }

this is my lucky day :)
the following thread on http://forum.java.sun.com/thread.jspa?threadID=562392&messageID=2767046 suggested to override the update method on the canvas, which works.
     // overridden to eliminate flicker
    public void update(Graphics g)
        paint(g);
    }

Similar Messages

  • Double buffering issue

    I have a complex modal JDialog with many JTextFields. Before it becomes visible I call setText("") on these fields to reset their values. When the dialog appears for the second time I see for a moment the old values of these fields. There is no such behaviour if I turn off double buffering on JRootPane of this dialog. Is it a bug or by design? It looks very ugly.
    JDK 1.6.0.27, Linux 2.6.31

    Here is an example. Click 'open', click 'fill values', close the dialog. Repeat until you see the old values in the JTextFields for a blink of an eye. Uncomment XXX to avoid this effect.
    public class Test
        private static MyDialog dialog;
        public Test()
        public static void main(String[] args) throws Exception
            SwingUtilities.invokeLater(new Runnable()
                @Override
                public void run()
                    final JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new FlowLayout());
                    JButton b = new JButton(new AbstractAction("Open")
                        @Override
                        public void actionPerformed(ActionEvent e)
                            if (dialog == null)
                                dialog = new MyDialog(frame);
                                dialog.pack();
                                dialog.setLocationRelativeTo(frame);
                                dialog.setModal(true);
                            dialog.clear();
                            dialog.setVisible(true);
                    frame.add(b);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
        private static class MyDialog extends JDialog
            private JTextField[] textFields;
            public MyDialog(Frame owner)
                super(owner);
                // XXX
                //getRootPane().setDoubleBuffered(false);
                int n = 20;
                setLayout(new GridLayout(n + 1, 2));
                textFields = new JTextField[n];
                for (int i = 0; i < n; ++i)
                    JTextField tf = new JTextField();
                    tf.setColumns(10);
                    textFields[i] = tf;
                    add(new JLabel("Field " + i));
                    add(tf);
                JButton b = new JButton(new AbstractAction("Fill values")
                    @Override
                    public void actionPerformed(ActionEvent e)
                        Random rnd = new Random();
                        for (JTextField tf : textFields)
                            tf.setText(Integer.toString(rnd.nextInt()));
                add(b);
            private void clear()
                for (JTextField tf : textFields)
                    tf.setText("");
    }

  • How to draw a point without double buffering?

    Hello all,
    I am drawing points as small red circles, but they are a bit ugly without double buffering. Am I able to draw the points to be nice? I mean not to draw them as just circles, but some special circle with light border that will looks much more nicer?
    Or what radius and position of the circle must be to be a nice small circle containing only 5 pixels? (upper, lower, right, left, middle)
    ps - they are ugly in the way of having a strange dot on the left side

    I use this method:
        private static final float POINT_RADIUS = 2f;
        private static final float POINT_DIAMETER = POINT_RADIUS * 2;
    private void drawPoint(float x, float y, Color c) {
            Ellipse2D point = new Ellipse2D.Float(x - POINT_RADIUS, -y
                    - POINT_RADIUS, POINT_DIAMETER, POINT_DIAMETER);
            graphics.setStroke(new BasicStroke(0.2f));
            graphics.fill(point);
    // and points looks like this:
       ***

  • Drawing with double buffering to a JFrame

    I want to create a simple JFrame and draw stuff to it via Graphic object using double buffering.
    If I create a loop and draw constantly using JFrame.getGraphic().xxx, am i doing double buffering. ( 'cause i read somewhere that swing components automaticly use double buffering ). And is this the righ approuch ( or whatever it's spelled ) ?

    I want to create a simple JFrame and draw stuff to it
    Don't do that.
    If you want to do custom rendering then use JPanel or JComponent, not JFrame.
    If I create a loop and draw constantly using JFrame.getGraphic().xxx, am i doing double buffering.
    No.
    You're also painting outside the standard paint cycle, which is a Bad Idea.
    So, essentially, everything your suggesting is the wrong way to go about things.
    In its most simple form, double buffering works something like this, though there are a whole load of subtleties to consider (such as component resizing for a start)
    public class Foo extends JComponent
        private BufferedImage image = null;
        protected void paintComponent(Graphics g)
            if (image != null)
                image = createImage(getWidth(), getHeight());
                paintBuffer(ig);
            g.drawImage(image, 0, 0, this);
        private void paintBuffer()
            Graphics ig = image.getGraphics();
            // do the rendering
            ig.dispose();
    }

  • Dragging a drawing when double-buffering

    Hopefully someone out there can help me with this...
    To state the situation simply, I have an applet in which I need to select, drag, and drop in order to move them around. The applet works fine right now, but I can't figure out how to add dragging. The way I have it, you click the object, drag the mouse to where you want to move it, and let go. It then dissapears from where it was, and appears where it was placed. The prolem is that I want to actively see the object being dragged....even an outline would work. As it is, it's difficult to know what you were selecting to move.
    I added a mouseDragged() function to the program, but I see no evidence of it working, which I think has something to do with the double-buffering I used. As far as I can tell, it does draw the object being dragged, it just doesn't ever show up on the screen....
    I would appreciate any input anyone can offer!

    With double buffering
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import java.awt.image.BufferedImage;
    public class Omove extends Frame
         BufferedImage img;
         Graphics      mg;
         Vector  v  = new Vector();
         Mobject m;
         int    iv = -1;
         int    dx = -1;
         int    dy = -1;
    public Omove()
         addWindowListener(new WindowAdapter()
        {     public void windowClosing(WindowEvent ev)
              {     dispose();
                   System.exit(0);}});
         setBounds(10,10,400,300);
         v.add(new Mobject(1,30,30));
         v.add(new Mobject(1,89,120));
         v.add(new Mobject(2,130,230));
         v.add(new Mobject(3,210,60));
         addMouseListener(new MouseAdapter()     
         {     public void mouseReleased(MouseEvent m)
                   if (iv != -1) putTheObject(m.getX(),m.getY());               
         addMouseMotionListener(new MouseMotionAdapter()          
              {     public void mouseDragged(MouseEvent m)
                        if (dx == -1) findTheObject(m.getX(),m.getY());     
                        if (iv != -1)
                             dx = m.getX();     
                             dy = m.getY();
                             repaint();
         setVisible(true);
    private void findTheObject(int x, int y)
         for (int i=0; i < v.size(); i++)
              m = (Mobject)v.get(i);
              if (x >= m.x && y >= m.y && x <= m.x+30 && y <= m.y+30)
                   iv = i;
    private void putTheObject(int x,int y)
         iv = -1;
         dx = -1;
    public void paint(Graphics g)
         if (img == null)
              img = (BufferedImage)this.createImage(getWidth(),getHeight());
              mg  = img.getGraphics();
         mg.setColor(Color.white);
         mg.fillRect(0,0,getWidth(),getHeight());
         for (int i=0; i < v.size(); i++)
              m = (Mobject)v.get(i);
              if (iv == i)           
                   m.x = dx;
                   m.y = dy;
              m.draw(mg);
         g.drawImage(img,0,0,null);
    public void update(Graphics g)
         paint(g);
    public class Mobject
         int x,y,t;
    public Mobject(int t, int x, int y)
         this.t = t;     
         this.x = x;     
         this.y = y;     
    public void draw(Graphics g)
         if (t == 1)
              g.setColor(Color.red);
              g.fillRect(x,y,30,30);
         if (t == 2)
              g.setColor(Color.orange);
              g.fillOval(x,y,30,30);
         if (t == 3)
              g.setColor(Color.blue);
              g.fillOval(x,y,50,30);
    public static void main (String[] args)
         new Omove();
    Noah

  • C# implementation issue with double buffering

    I am using the C# dll library to access the berkeley db, is there any way to pass the buffer, offset, count for the btree cursor move function. I dont want to double buffer to memory every time I conduct a cursor move.

    Sir,
    The call I am assuming you are using on the double buffered example is the DAQ_DB_TRANSFER. The output or return value on this is an integer array of i16's. I'm not very familiar with Borland C++ Builer 4, but I'm thinking the VCL label is just a control that you are trying to send a value to. I'm not sure why this would give you an violation, other than that array of integers not having an data in there, or you are pointing to the data in an element incorrectly.
    Daniel McChane
    Application Engineer
    National Instruments

  • Problem with Double Buffering and Swing

    Hi
    I made a game and basically it works pretty well, my only problem is it flickers really badly right now. I read up on a whole lot of forums about double buffering and none of those methods seemed to work. Then I noticed that Swing has double buffering built in so I tried that but then I get compilation errors and I'm really not sure why. My original code was a console application and worked perfectly, then I ported it into a JApplet and it still works but it flickers and thats what I'm tryign to fix now.
    The code below is in my main class under the constructor.
    Heres the double buffering code I'm trying to use, I'm sure you all seen it before lol
    public void update(Graphics g)
              // initialize buffer
              if (dbImage == null)
                   dbImage = createImage(this.getSize().width, this.getSize().height);
                   dbg = dbImage.getGraphics();
              // clear screen in background
              dbg.setColor(getBackground());
              dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
              // draw elements in background
              dbg.setColor(getForeground());
              paint(dbg);
              // draw image on the screen
              g.drawImage(dbImage, 0, 0, this);
         }My paint is right under neath and heres how it looks
    This snipet of code works but when I change the method to
    public paintComponent(Graphics g){
    super.paintComponent(g)...
    everythign stops working and get a compilation error and says that it can't find paintComponent in javax.swing.JFrame.
    public void paint(Graphics g)
              super.paint(g);
              //if game starting display menue
              if (show_menue)
                   //to restart lives if player dies
                   lives = 3;
                   menue.draw_menue(g);
                   menue_ufo1.draw_shape(g);
                   menue_ufo2.shape_color = Color.DARK_GRAY;
                   menue_ufo2.draw_shape(g);
                   menue_ufo3.shape_color = Color.BLUE;
                   menue_ufo3.draw_shape(g);
                   menue_ufo4.shape_color = new Color(82, 157, 22);
                   menue_ufo4.draw_shape(g);
                   menue_ufo5.draw_shape(g);
                   menue_ufo6.shape_color = new Color(130, 3, 3); ;
                   menue_ufo6.draw_shape(g);
                   menue_turret.draw_ship(g);
                   menue_ammo.draw_ammo(g);
              else
                   //otherwise redraw game objects
                   gunner.draw_ship(g);
                   y_ammo.draw_ammo(g);
                   grass.draw_bar(g);
                   o_ufo.draw_shape(g);
                   b_ufo.draw_shape(g);
                   m_ufo.draw_shape(g);
                   s_ufo.draw_shape(g);
                   z_ufo.draw_shape(g);
                   xx_ufo.draw_shape(g);
                   info.draw_bar(g);
                   live_painter.draw_lives(g, lives);
                   score_painter.draw_score(g, score);
                   level_display.draw_level(g, level);
                   explosion.draw_boom(g);
         }I just want to get rid of the flickering for now so any help will be greatly appreciated. Depending which will be simpler I can either try to double buffer this program or port it all to swing but I'm not sure which elements are effected by AWT and which by Swing. Also I read some of the Java documentation but couldn't really understand how to implement it to fix my program.
    Thanks in advance
    Sebastian

    This is a simple animation example quickly thrown together. I have two classes, an animation panel which is a JPanel subclass that overrides paintComponent and draws the animation, and a JApplet subclass that simply holds the animation panel in the applet's contentpane:
    SimpleAnimationPanel.java
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Point;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    class SimpleAnimationPanel extends JPanel
        private static final int DELAY = 20;
        public static final int X_TRANSLATION = 2;
        public static final int Y_TRANSLATION = 2;
        private Point point = new Point(5, 32);
        private BufferedImage duke = null;
        private Timer timer = new Timer(DELAY, new TimerAction());
        public SimpleAnimationPanel()
            try
                // borrow an image from sun.com
                duke = ImageIO.read(new URL(
                        "http://java.sun.com/products/plugin/images/duke.wave.med.gif"));
            catch (MalformedURLException e)
                e.printStackTrace();
            catch (IOException e)
                e.printStackTrace();
            setPreferredSize(new Dimension(600, 400));
            timer.start();
        // do our drawing here in the paintComponent override
        @Override
        protected void paintComponent(Graphics g)
            super.paintComponent(g);
            if (duke != null)
                g.drawImage(duke, point.x, point.y, this);
        private class TimerAction implements ActionListener
            @Override
            public void actionPerformed(ActionEvent e)
                int x = point.x;
                int y = point.y;
                Dimension size = SimpleAnimationPanel.this.getSize();
                if (x > size.width)
                    x = 0;
                else
                    x += X_TRANSLATION;
                if (y > size.height)
                    y = 0;
                else
                    y += Y_TRANSLATION;
                point.setLocation(new Point(x, y)); // update the point
                SimpleAnimationPanel.this.repaint();
    }AnimationApplet.java
    import java.lang.reflect.InvocationTargetException;
    import javax.swing.JApplet;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    public class AnimationApplet extends JApplet
        public void init()
            try
                SwingUtilities.invokeAndWait(new Runnable()
                    public void run()
                        // construct the panel
                        JPanel simpleAnimation = new SimpleAnimationPanel();
                        // put it in the contentPane of the JApplet
                        getContentPane().add(simpleAnimation);
                        setSize(simpleAnimation.getPreferredSize());
            catch (InterruptedException e)
                e.printStackTrace();
            catch (InvocationTargetException e)
                e.printStackTrace();
    }Here's a 3rd bonus class that shows how to put the JPanel into a stand-alone program, a JFrame. It's very similar to doing it in the JApplet:
    AnimationFrame.java
    import javax.swing.JFrame;
    public class AnimationFrame
        private static void createAndShowUI()
            JFrame frame = new JFrame("SimpleAnimationPanel");
            frame.getContentPane().add(new SimpleAnimationPanel());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        public static void main(String[] args)
            java.awt.EventQueue.invokeLater(new Runnable()
                public void run()
                    createAndShowUI();
    }Edited by: Encephalopathic on Mar 15, 2008 11:01 PM

  • Alternative to Double-Buffered Canvas

    I am working on a program in which I use a double-buffered Canvas inside a JScrollPane. The problem is that the Canvas draws over the scrollbars. I have tried extending JComponent, JPanel, JApplet, and Component instead of Canvas, and none of them are double buffered. Here is the code I used to debug this problem:
    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.image.*;
    import javax.swing.*;
    public class Test implements  Runnable
         JFrame f;
         JScrollPane scroller;
         JPanel panel;
         TestCanvas canvas;
         Thread runner;
         BufferStrategy strategy;
         public static void main (String[] args) {
              Test app = new Test();
              app.init();
         public void init() {
              f = new JFrame();
              f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              panel = new JPanel();
              canvas = new TestCanvas();
              panel.add(canvas);
              scroller = new JScrollPane(panel);
              scroller.setWheelScrollingEnabled(true);
              f.getContentPane().add(scroller);
              f.pack();
              f.setSize(300,300);
              f.setVisible(true);
              canvas.createBufferStrategy(2);
              strategy = canvas.getBufferStrategy();
              runner = new Thread(this);
              runner.run();
         public void run() {
              int x = 0;
              while(x != 65536) {
                   Graphics2D g = (Graphics2D)strategy.getDrawGraphics().create();
                   g.setColor(new Color(x%256,0,0));
                   g.fill(new Ellipse2D.Double(0,0,600,600));
                   strategy.show();
                   x++;
    }Any suggestions?

    The main culprit is that you are mixing AWT components with Swing ones.
    In addition, your are doing so many of useless things and wrong things in your code.
    Swing components are defaulted for using double-buffering.
    See: http://java.sun.com/products/jfc/tsc/articles/painting/index.html
    Try and study this code.
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import java.awt.image.*;
    import javax.swing.*;
    public class Fox1229{
      JFrame f;
      JScrollPane scroller;
      TestCanvas canvas;
      int x;
      Timer t;
      public static void main (String[] args) {
        Fox1229 app = new Fox1229();
        app.init();
      public void init() {
        x = 0;
        f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        canvas = new TestCanvas();
        scroller = new JScrollPane(canvas);
        scroller.setWheelScrollingEnabled(true);
        f.getContentPane().add(scroller, BorderLayout.CENTER);
        f.setSize(300, 300);
        f.setVisible(true);
        t = new Timer(50, new ActionListener(){
          public void actionPerformed(ActionEvent e){
            canvas.setOc(new Color(x % 256, 0, 0));
            canvas.repaint();
            if (++x == 1024){
              t.stop();
        t.start();
    class TestCanvas extends JPanel{
      Color oc;
      public TestCanvas (){
        oc = new Color(0, 0, 0);
      public void setOc(Color c){
        oc = c;
      public Dimension getPreferredSize(){
        return new Dimension(600, 600);
      public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D)g;
        g2d.setColor(Color.blue);
        g2d.fill(new Rectangle(0, 0, 600, 600));
        g2d.setColor(oc);
        g2d.fill(new Ellipse2D.Double(0, 0, 600, 600));
    }

  • Double buffering still gives flickering graphics.

    I copied code from a tutorail which is supposed to illustrate double buffering.
    After I run it, it still flickers though.
    I use applet viewer, which is part of netbeans to run my applet.
    Link to tutorial: http://www.javacooperation.gmxhome.de/TutorialStartEng.html
    My questions are:
    Is the strategy used for double buffering correct?
    Why does it flicker?
    Why does the program change the priority a couple of times?
    Can you make fast games in JApplets or is there a better way to make games? (I think C++ is too hard)
    Here is the code:
    package ballspel;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import javax.swing.JApplet;
    //import java.applet.*;
    * @author Somelauw
    public class BallApplet extends /*Applet*/ JApplet implements Runnable {
    private Image dbImage;
    private Graphics dbg;
    private int radius = 20;
    private int xPos = 10;
    private int yPos = 100;
    * Initialization method that will be called after the applet is loaded
    * into the browser.
    @Override
    public void init() {
    //System.out.println(this.isDoubleBuffered()); //returns false
    // Isn't there a builtin way to force double buffering?
    // TODO start asynchronous download of heavy resources
    @Override
    public void start() {
    Thread th = new Thread(this);
    th.start();
    public void run() {
    Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    while (true) {
    xPos++;
    repaint();
    try {
    Thread.sleep(20);
    } catch (InterruptedException ex) {
    ex.printStackTrace();
    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    @Override
    public void paint(Graphics g) {
    super.paint(g);
    //g.clear();//, yPos, WIDTH, WIDTH)
    g.setColor(Color.red);
    g.fillOval(xPos - radius, yPos - radius, 2 * radius, 2 * radius);
    @Override
    public void update(Graphics g) {
    super.update(g);
    // initialize buffer
    if (dbImage == null) {
    dbImage = createImage(this.getSize().width, this.getSize().height);
    dbg = dbImage.getGraphics();
    // clear screen in background
    dbg.setColor(getBackground());
    dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
    // draw elements in background
    dbg.setColor(getForeground());
    paint(dbg);
    // draw image on the screen
    g.drawImage(dbImage, 0, 0, this);
    // TODO overwrite start(), stop() and destroy() methods
    }

    Somelauw wrote:
    I copied code from a tutorail which is supposed to illustrate double buffering.
    After I run it, it still flickers though.
    I use applet viewer, which is part of netbeans.. AppletViewer is part of the JDK, not NetBeans.
    ..to run my applet.
    Link to tutorial: http://www.javacooperation.gmxhome.de/TutorialStartEng.html
    Did you specifically mean the code mentioned on this page?
    [http://www.javacooperation.gmxhome.de/BildschirmflackernEng.html]
    Don't expect people to go hunting around the site, looking for the code you happen to be referring to.
    As an aside, please use the code tags when posting code, code snippets, XML/HTML or input/output. The code tags help retain the formatting and indentation of the sample. To use the code tags, select the sample and click the CODE button.
    Here is the code you posted, as it appears in code tags.
    package ballspel;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import javax.swing.JApplet;
    //import java.applet.*;
    * @author Somelauw
    public class BallApplet extends /*Applet*/ JApplet implements Runnable {
        private Image dbImage;
        private Graphics dbg;
        private int radius = 20;
        private int xPos = 10;
        private int yPos = 100;
         * Initialization method that will be called after the applet is loaded
         * into the browser.
        @Override
        public void init() {
            //System.out.println(this.isDoubleBuffered()); //returns false
            // Isn't there a builtin way to force double buffering?
            // TODO start asynchronous download of heavy resources
        @Override
        public void start() {
            Thread th = new Thread(this);
            th.start();
        public void run() {
            Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
            while (true) {
                xPos++;
                repaint();
                try {
                    Thread.sleep(20);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
        @Override
        public void paint(Graphics g) {
            super.paint(g);
            //g.clear();//, yPos, WIDTH, WIDTH)
            g.setColor(Color.red);
            g.fillOval(xPos - radius, yPos - radius, 2 * radius, 2 * radius);
        @Override
        public void update(Graphics g) {
            super.update(g);
            // initialize buffer
            if (dbImage == null) {
                dbImage = createImage(this.getSize().width, this.getSize().height);
                dbg = dbImage.getGraphics();
            // clear screen in background
            dbg.setColor(getBackground());
            dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
            // draw elements in background
            dbg.setColor(getForeground());
            paint(dbg);
            // draw image on the screen
            g.drawImage(dbImage, 0, 0, this);
        // TODO overwrite start(), stop() and destroy() methods
    }Edit 1:
    - For animation code, it would be typical to use a javax.swing.Timer for triggering updates, rather than implementing Runnable (etc.)
    - Attempting to set the thread priority will throw a SecurityException, though oddly it occurs when attempting to set the Thread priority to maximum, whereas the earlier call to set the Thread priority to minimum passed without comment (exception).
    - The paint() method of that applet is not double buffered.
    - It is generally advisable to override paintComponent(Graphics) in a JPanel that is added to the top-level applet (or JFrame, or JWindow, or JDialog..) rather than the paint(Graphics) method of the top-level container itself.
    Edited by: AndrewThompson64 on Jan 22, 2010 12:47 PM

  • Double buffering && repaint

    Hi there,
    I have a frame F that contains two panels P1 and P2.
    P1 uses double buffering for drawing circles and lines.
    P2 has buttons and a JList.
    When i click on a JList to have popup menu
    or when i move the frame F on the screen
    the panel P2 lost some of JList drawing.
    Actually i iconify the frame in order to oblige
    JVM to do repaint.
    How can i resolve this problem please.

    Do not ever mix heavyweight and lightweight, or else
    you won't be able to set up the correct zorder.But when i iconfiy and desiconify my frame Java
    repaint correctly.
    I need a nice tip article of how to simulate
    desiconify repainting process.
    Thabk u

  • Double buffering circle not round

    I am making an applet where some circles are painted to the screen. When not using the double buffering scenario, the shapes appear correctly.
    The problem is when i use the double-buffering technique, the circles are still there, but they look like very ugly. I would call that some "squarcles".
    What is the bug here and how to get some decent double buffering with thoses circles?

    Here is the code so far i have that is used for painting on the screen:
    // Declaration
    private BufferedImage ecran;
    private Graphics2D buffer_ecran;
    // Instantiation
    this.ecran = new BufferedImage(getSize().width, getSize().height, BufferedImage.TYPE_INT_ARGB);
    this.buffer_ecran = ecran.createGraphics();
    // In paint() method
    Graphics be = buffer_ecran;
      // Clean the surface
    be.setColor(Color.WHITE);
    be.fillRect(0, 0, getSize().width, getSize().height);
      // Draw the circles
    be.setColor(Color.RED);
    be.drawOval(x, y, ra, rb);
      // Output the buffer to the screen
    g.drawImage(ecran, 0, 0, this);I was previously using Image and changed to BufferedImage with the hope it would resolve the situation. I can't tell a significant change in image display.

  • Double buffering on large pane

    I have a large JPanel which I'm drawing onto which is about 5000 x 5000. To eliminate screen flicker when dragging objects I'm trying to use double buffering. The problem is the image is too big and I get an out of memory error message.
    I've tried using clipped rectangles to display just the size of image needed but they give erratic results or the scrolling doesn't work. I need it to display the rest of the image when scrolling. Does anyone have any suggestions on how to solve this?
    I've thought about creating the image to be just the size of the screen and to just have the objects that are meant to be at that part but I think this would involve going from device space from user space but I don't know how to do this. Thanks.
    Alan

    ok, if you want it to be interactive with scrolling, then you have to keep the image large, but unless your video card has 71.5MB free space for that image, you're not going to get good results, not to mention CPU will be hurting if you need to do any processing of the image (transforms, loads, saves, etc.)
    options:
    1. forget interaction and just get the viewable image area and paint that portion of the image
    2. Get a graphics supercomputer!
    3. try to fake the interaction somehow
    what else? dunno, did that help

  • Double buffering method - confusion

    Hello everyone,
    I've been messing around with Applets and animation in them and needed a method to reduce/remove flicker, so I decided on double buffering and looked up a tutorial on it. I understand the concept of it clearly, however the code doesn't make too much sense to me even after reading paint/graphics pages. What confuses me is:
    Why is the "g.drawString()" in update method if it already paints the dbg graphics? I commented it out, and the ball won't move. In addition, in the drawImage method it draws dbImage which is only altered once (see when it was null), so how are we using it to double buffer if we never write to it except once?
    import java.applet.*;
    import java.awt.*;
    public class BallBasic extends Applet implements Runnable {
        int x_pos = 10;
        int y_pos = 10;
        int radius = 20;
        private Image dbImage;
        private Graphics dbg;
        public void init() {
            setBackground(Color.blue);
        public void start() {
            Thread th = new Thread(this);
            th.start();
        public void stop() {
        public void destroy() {
        public void run() {
            Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
            while (true) {
                x_pos++;
                repaint();
                try {
                    Thread.sleep(20);
                } catch (InterruptedException ex) {
                Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
        public void update(Graphics g) {
            if (dbImage == null) {
                System.out.println("dbImage was 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());
            paint(dbg);
            g.drawImage(dbImage, 0, 0, this);
        public void paint(Graphics g) {
            g.setColor(Color.red);
            g.fillOval(x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
    }Thanks for reading, hope you can help.
    Note: I understand how the panting process works, how it ties into update, ectcetera, please do not post explanations of how painting/applets work, just looking for heavy comments on what is happening throughout the update/paint method.

    Patrick_Ritchie wrote:
    I want to understand the concepts of this and how to use just plain ol' applets. I need to learn these things ;)Most people skip the old AWT. Did you learn how steam engines worked before you learned to drive a car?

  • Double Buffering a large image - is it a good idea to do so ?

    I'm developing a small maze like game. I'm using double buffering. So I double buffer the enter screen area (1024x768) and copy them all back to the on screen buffer...
    I need my code to run atleast at 18 - 24 fps.
    Is there a better/efficient way to do this...
    Note : I'm manually double buffer. I don't use any Buffer manager.
    like this
    public void update(Graphics g)
          Image i = createImage(...)      ;
          paint(i.getGraphics() );
          g.drawImage(i..)
    }

    Hi chaos,
    I am developing a game too and I achieve very high frame rate (up to 60 fps) using the hardware accelerated pipelines and the double buffering/page flipping strategy.
    here is the method I call to update my the entire screen of my game:
    private void screenUpdate()
        try
          Graphics gScr = bufferStrategy.getDrawGraphics();
          gameRender(gScr); //this is where I paint the game screen
          gScr.dispose();
          if (!bufferStrategy.contentsLost())
            bufferStrategy.show();
        } catch (Exception e)
          e.printStackTrace();
          running = false;
      }Here is how I create the buffer strategy in the main JFrame:
    try
          EventQueue.invokeAndWait(new Runnable()
            public void run()
              createBufferStrategy(NUM_BUFFERS);
        } catch (Exception e)
          System.out.println("Error while creating buffer strategy");
          System.exit(0);
        bufferStrategy = getBufferStrategy();In my gameRender method, I use only draming method that works without breaking the hardware acceleration (by default enabled with java 6.0). I just make sure to use BufferedImage without rotation or manually written filters and the game runs at 60 fps without any issue.
    The CPU is even still sleeping more than 70% of the time which gives a lot room for all other processing such as path finders, A.I., ...
    Cheers.
    Vince.

  • Double Buffering Slow..is it in the code? or..

    Hi all..
    i make small application which use double buffering..
    here's the code:
    public void update(Graphics g){
            tys_a=System.currentTimeMillis();
            Dimension d = getSize();       
            if (offscreenImage == null || offscreenImage.getWidth(null) < d.width || offscreenImage.getHeight(null) < d.height){
                if (offscreenImage != null) offscreenImage.flush();
                offscreenImage = createImage(d.width, d.height);
                offscreenSize = d;
                offscreenGraphics = offscreenImage.getGraphics();
            offscreenGraphics.clearRect(0, 0, d.width, d.height);
            paint(offscreenGraphics);
            g.drawImage(offscreenImage, 0, 0, null);
            g.dispose();
            tys_b=System.currentTimeMillis();
            tys_c=tys_b-tys_a;
            System.out.println("DRAW OFFSCREEN GRAPH " + tys_c + " --- " + tys_a + " ---- " + tys_b);
        }then i run it on my desktop PC, which specs are
    Core 2 duo E4500 - 2.20Ghz, 2 Gb RAM , Vista Home Premium
    its works fine..took 10 ms
    but when i try on Sony vaio laptop, which specs are:
    core 2 duo T8100 - 2.1Gghz, 2Gb RAM, Vista home premium
    its so slow..took average about 800-900ms (80-90 times slower)
    did i do something wrong with my code...i've been searching all day...but seems no answer
    but all i can think..is not about the code..because it works fine in my desktop...
    but i dunno what cause this problem?
    any body can help?or ever have this kind of problem willing to share?
    Thx,

    AndrewThompson64 wrote:
    >
    Hi,
    dunnoIs this a English word???>Is this an English word? No it is not, it is strine* for "I do not know". I understand the Americans also know of this strine abbreviation, but I cannot understand how, since their comprehension of strine is quite low.
    * local pronunciation of 'Australian'.
    But to the OP. It is a far better idea to phrase your replies in standard English, since many people who understand English do not understand (or will not tolerate) local abbreviations and slang.In the South West of England It can be translated as "I don't know and I don't care" often accompanied by a shrug of the shoulders.

Maybe you are looking for

  • Can I log into my iTunes account on any computer

    I am staying with my grandparents for a few weeks. Can I log on to my iTunes account, using their computer (which has iTunes installed for their account) and sync my new iPod with my iTunes account? Thanks. Nik

  • Why is my HD not shown as a startup disk?

    My macbook pro died on me, since it has stopped going into safe sleep (hibernate). Only this time I couldnt start it up again, it only gave me the question mark. When I start up from the CD and check the drive with Disk Utility everything is ok. But

  • Transitions  (Random) in Keynote.

    Why in the new Key Note that we still do not have "Random" Transitions. I find it very frustrating making a slide selection, and having to select each slide to get a different Transition. Then If I want to change the timing on all the Slides it chang

  • Why is iCloud calendar giving me information in German although Im in USA?

    I am using the calendar feature on iCloud, and is giving me calendar names in German, although I am in USA and my language setting is English.  Anyone know why this is happening and how to fix it?

  • How do I remove a DHCP static mapping that will not go away in Server Admin?

    I have a static DHCP mapping to a retired network that I can't remove from the Server Admin GUI. To be specific, I click remove and it disappears. To then click "SAVE" and it comes back. How do I remove it manually? Where are the static mappings loca