Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: BufferedImage create flicker in simple code

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default BufferedImage create flicker in simple code

    I wrote a complex program that need bufferedImage but it is very flicker, so I simplify it and show it here, but still cannot solve.

    Please help. I am using java 6.0.
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    public class Main {
        Timer timer;
        Mill mill;
        JFrame frame;
        int t=0;
        public Main(){
            frame=new JFrame();
            mill=new Mill();
            frame.getContentPane().add(mill);
            frame.setSize(new java.awt.Dimension(700,700));
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            mill.setPreferredSize(new java.awt.Dimension(800,700));
            frame.setVisible(true);
            frame.pack();
            timer=new Timer(100,new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    t++;
                    mill.repaint();
                }
            });
            timer.start();
        }
        public static void main(String[] args) {
            new Main();
        }
        BufferedImage buff=new BufferedImage(1,1,BufferedImage.TYPE_INT_RGB);
        class Mill extends JPanel {
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g3=(Graphics2D)g;
                {//.. paint only
                    if( buff==null|| buff.getWidth()!=mill.getWidth() || 
                            buff.getHeight()!=mill.getHeight()        
                    ){
                        buff=new BufferedImage(mill.getWidth(),mill.getHeight(),BufferedImage.TYPE_INT_RGB);
                    }
                    Graphics2D g2 = (Graphics2D) buff.createGraphics();
                    g2.setColor(new Color(0,0,100));
                    g2.fillRect( 0,0,600,600);
                    g2.setColor(new Color(0,0,200));
                    g2.fillRect( 0,0,500,500);
                    g2.setColor(new Color(0,0,250));
                    g2.fillRect( 0,0,400,400);
                }
                g3.drawImage( buff,0,0,buff.getWidth(),buff.getHeight(),this);
            }
        }
    }
    result link here
    img145.imageshack.us/i/eatm.jpg/

    Last edited by arnbook; March 11th, 2011 at 11:27 PM.


Similar Threads

  1. Need help with simple code
    By suxen in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 21st, 2011, 07:10 PM
  2. [SOLVED] Help with simple Applet code
    By that_guy in forum What's Wrong With My Code?
    Replies: 13
    Last Post: January 11th, 2011, 09:22 PM
  3. Simple code error
    By robin28 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 7th, 2010, 02:25 PM
  4. im dying here..on a simple code
    By Jason in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 28th, 2010, 10:33 PM
  5. Help! how would you code these simple games?
    By makarov in forum Java Applets
    Replies: 1
    Last Post: November 14th, 2009, 11:31 AM

Tags for this Thread