Results 1 to 12 of 12
Like Tree2Likes
  • 2 Post By KevinWorkman

Thread: Thread painting - simple game - Blank JFrame

  1. #1
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    98
    Rep Power
    0

    Default Thread painting - simple game - Blank JFrame

    I have been following a game tutorial loosely on YouTube. Yesterday I had everything working where I could move my fighter around with the cursor keys, and I had where my background was scrolling. The problem I ran into was that since I was scrolling 3 backgrounds, and the method I was using calls me to paint each background twice, I was noticing that the background, my fighter, or both would lag. My understanding of this is because of the linearity of java programs. Sorry I do not have the code from yesterday as I typically delete and recode over and over in order to learn.

    Yesterdays code example:
    JFrame called Board class.
    Board class extended JPanel and I painted everything in the Board class.

    Today (trying to implement threads):
    Java Code:
    package MyGame;
    
    import javax.swing.*;
    
    public class Main{
        public static JFrame frame;
        public static Thread bgThread;
        
        public static void main(String[] args){    
            frame = new JFrame("");
            frame.setSize(800, 600);
            frame.setResizable(false);
            frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
            frame.setVisible(true);
            bgThread = new Thread(new Background());
            bgThread.start();
        }
    }
    Java Code:
    package MyGame;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Background implements Runnable, ActionListener{
        Image bg, bg2, bg3;
        int bgStart = 0;
        int bg2Start = 0;
        int bg3Start = 0;
        int bgOffset = 1600;
        Timer time;
       
        
        @Override
        public void run(){
            
            LoadImages();
            time = new Timer(5, this);
            
            }
        public void paint(Graphics g){
            Main.frame.paint(g);
                Graphics2D g2d = (Graphics2D) g;
                    g2d.drawImage(bg, bgStart, 0, null);
                    g2d.drawImage(bg, bgStart+bgOffset, 0, null);
                    g2d.drawImage(bg2, bg2Start, 0, null);
                    g2d.drawImage(bg2, bg2Start+bgOffset, 0, null);
                    g2d.drawImage(bg3, bg3Start, 0, null);
                    g2d.drawImage(bg3, bg3Start+bgOffset, 0, null);
                  
                    bg3Start-=2;
                    bg2Start-=5;
                    bgStart-=1;
                    
    
                    if(bg2Start<=-bgOffset){
                        bg3Start=0;
                        bg2Start=0;
                        bgStart=0;
                    }
                
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            Main.frame.repaint();
        }
        public void LoadImages(){
        
            ImageIcon ibg = new ImageIcon(getClass().getResource("/images/bg.jpg"));
            ImageIcon ibg2 = new ImageIcon(getClass().getResource("/images/bg2.png"));
            ImageIcon ibg3 = new ImageIcon(getClass().getResource("/images/bg3.png"));
            
            bg = ibg.getImage();
            bg2 = ibg2.getImage();
            bg3 = ibg3.getImage();
        }
    }
    I guess my question is.... what have I done wrong either in theory or practice. My code compiles. It simply shows a grey box.

    My current goal is to create a separate Thread for the scrolling background, 1 for the fighter movement, 1 for the game clock. Is this the correct practice or am I going about this the wrong way.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default Re: Thread painting - simple game - Blank JFrame

    There's nothing in the JFrame.
    Your Background class plays no part in what is displayed in the JFrame.
    For that you would need some form of Component added to the frame, which would be drawn onto the frame.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

    Default Re: Thread painting - simple game - Blank JFrame

    All drawing must be done on the EDT. You can update the game state on another thread, but all drawing must be done on the EDT.

    More to the point though, when do you call the Background class's paint() function? Add some print statements in there to test your assumptions.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Thread painting - simple game - Blank JFrame

    Scrap all that, it's so wrong that I don't even want to pick it apart one mistake at a time. Then go through these two tutorials and make a fresh start.
    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    98
    Rep Power
    0

    Default Re: Thread painting - simple game - Blank JFrame

    ... thats the spirit. Thanks for those not trying to stab a knife in me.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Thread painting - simple game - Blank JFrame

    Quote Originally Posted by rru96 View Post
    ... thats the spirit. Thanks for those not trying to stab a knife in me.
    Please clarify.

  7. #7
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    98
    Rep Power
    0

    Default Re: Thread painting - simple game - Blank JFrame

    Clarification is that I appreciate those that took the time to actually help in a constructive manner instead of telling me how awful the code was. If its awful, people need to know. But maybe in a somewhat... tactful way.
    Maybe its just me. Sorry.

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

    Default Re: Thread painting - simple game - Blank JFrame

    Quote Originally Posted by rru96 View Post
    Clarification is that I appreciate those that took the time to actually help in a constructive manner instead of telling me how awful the code was. If its awful, people need to know. But maybe in a somewhat... tactful way.
    Maybe its just me. Sorry.
    This is a technical forum, so we oftentimes don't waste a lot of time being warm and fuzzy: if something's wrong, it's just wrong. We don't mean any offense, and we know that writing bad code is part of the process. Sorry if that seems rude, but programmers tend to develop a bit of a thick skin about their code after a while.

    But it seems like you're getting bad information from whoever you're learning from, which is a real problem that's going to bite you later on (not to mention right now), which is why we're telling you now that you might want to rethink how you're going about it. The tutorials Darryl pointed out (especially the painting one) are fantastic and contain good examples of correct code that you can use.

    We're here to help, but part of helping is telling people when the approach they're taking is wrong. In your case, there's a lot in the code that's going to cause you headaches, which I think you're discovering now. That's not a personal attack, it's just the truth.

    I suggest you start with the tutorials Darryl gave you, and then proceed with REALLY small steps. You're trying to do about 5 different things here, which I think is your problem. Instead, get the basics right before you try to add more advanced stuff, and even then only add one thing at a time before you move on to your next goal. Does that make more sense?

    Okay, I've used up my warm and fuzzy quotient for the week. Back to being cantankerous and grouchy. Get off my lawn!
    Fubarable and JosAH like this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    98
    Rep Power
    0

    Default Re: Thread painting - simple game - Blank JFrame

    And that is the problem I have yet to figure out with Java. When I first came here, I think someone asked me, "Does it do what you expect?" when I had a problem. Figured if I can keep getting it to work, I must have been on the right track. Go figure.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Thread painting - simple game - Blank JFrame

    Quote Originally Posted by rru96 View Post
    And that is the problem I have yet to figure out with Java. When I first came here, I think someone asked me, "Does it do what you expect?" when I had a problem. Figured if I can keep getting it to work, I must have been on the right track. Go figure.
    I've found that doing Swing graphics programming requires throwing away several assumptions and using a different mind-set. This is one (of many cases) where you can't just plug forward headlong without first looking at some of the better tutorials. I strongly second Darryl's recommendations and links. They've helped me quite a bit and can help you too if you will let them.

  11. #11
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    98
    Rep Power
    0

    Default Re: Thread painting - simple game - Blank JFrame

    I did read Darryl's links that were posted. As for scrapping everything and starting over, that is the practice I take just about everyday. This is how I help try to teach myself. After reading through the tutorials, I do see errors in my coding. But still stay confused on the paint() method, as well as other issues.

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default Re: Thread painting - simple game - Blank JFrame

    Sometimes it helps to look at the source code; it is located in a huge src.zip file in your JDK directory; have a look at the paint( ... ) method in the JComponent class; it does a lot more than just painting one rectangular surface, that's what the paintComponent( ... ) method is for and that's the method you should override.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Thread ( Game ) - help with painting object
    By Dex in forum Threads and Synchronization
    Replies: 3
    Last Post: 01-25-2013, 03:52 PM
  2. JPanel not showing when painting on JFrame
    By shall in forum AWT / Swing
    Replies: 3
    Last Post: 07-07-2012, 12:11 AM
  3. Replies: 2
    Last Post: 04-30-2012, 03:29 PM
  4. painting to a JPanel in a JFrame
    By Parkournerd in forum AWT / Swing
    Replies: 1
    Last Post: 01-16-2012, 09:24 PM
  5. Simple Blank Filler project Help
    By Idoq in forum New To Java
    Replies: 14
    Last Post: 07-18-2011, 11:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •