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 3 of 3

Thread: Double Buffering in game programming

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Double Buffering in game programming

    I was following http://www.youtube.com/watch?v=4T3WJEH7zrc tutorial and followed it through, it did remove the lines (double buffered it) but it also inverted my image, so the ball is white and background is black, which stops me from getting a coloured background.


    package javagame;
     
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import javax.swing.JFrame;
     
    public class JavaGame extends JFrame {
     
        int x, y;
        private Image dbImage;
        private Graphics dbg;        
     
        public class AL extends KeyAdapter {
            public void keyPressed(KeyEvent e) {
                int keyCode = e.getKeyCode();
                if(keyCode == e.VK_LEFT){
                    if(x <= 0)
                        x = 0;
                    else
                        x+= -5;
                }
                if(keyCode == e.VK_RIGHT){
                    if(x >= 485)
                        x = 485;
                    else
                        x+= +5;
                }
                if(keyCode == e.VK_UP){
                    if(y <= 20)
                        y = 20;
                    else
                        y+= -5;
                }
                if(keyCode == e.VK_DOWN){
                    if(y >= 485)
                        y = 485;
                    y+= +5;
                }
            }
            public void keyReleased(KeyEvent e) {
     
            }
        }
     
        public JavaGame() {
            addKeyListener(new AL());
            setTitle("Game");
            setSize(250, 250);
            setResizable(false);
            setVisible(true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBackground(Color.GREEN);
            x = 15;
            y = 15;
        }
     
        public void paint(Graphics g) {
            dbImage = createImage(getWidth(), getHeight());
            dbg = dbImage.getGraphics();
            paintComponent(dbg);
            g.drawImage(dbImage, 0, 0, this);
        }
     
        public void paintComponent(Graphics g) {
            g.fillOval(x, y, 15, 15);
     
            repaint();
        }
     
        public static void main(String[] args) {
            new JavaGame();
        }
     
    }


    I just want to know if anyone can help me. It goes back to normal if I remove public void paint. please help. Thanks in advance.

    Bye.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Double Buffering in game programming

    Correction: The paintComponent() method is not an override.

    The problem is probably with the call to repaint() from the paint() method or its called methods
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Double Buffering in game programming

    I'd recommend learning how to paint to swing components:
    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    ...and Swing components are already double buffered.

Similar Threads

  1. android programming vs game programming using java
    By vgoel38 in forum Android Development
    Replies: 4
    Last Post: September 8th, 2012, 05:48 PM
  2. Jar Embedded in Website: Double Buffering Issue?
    By Staticity in forum AWT / Java Swing
    Replies: 6
    Last Post: July 17th, 2012, 02:43 PM
  3. Android - SurfaceView flickers - Double Buffering
    By Nesh108 in forum Android Development
    Replies: 2
    Last Post: April 22nd, 2012, 06:09 AM
  4. Help With Double Buffering/Animations
    By bgroenks96 in forum Java Theory & Questions
    Replies: 1
    Last Post: July 18th, 2011, 06:58 PM
  5. Double Buffering
    By Ganezan in forum Java Theory & Questions
    Replies: 2
    Last Post: November 20th, 2009, 03:51 AM