Results 1 to 11 of 11
Like Tree2Likes
  • 1 Post By DarrylBurke
  • 1 Post By DarrylBurke

Thread: I Really Need Help Understand Buffering Images And How To Properly Use Graphics...

  1. #1
    AmpedNavy is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Unhappy I Really Need Help Understand Buffering Images And How To Properly Use Graphics...

    Title says it all. Here's my little project, I'm looking for tips to clean my code or make it better in anyway, also I need to learn how to buffer images.. I try to buffer them, but I mess up the code everytime. I know there are a lot of random elements or stuff in this, but it's all just mainly a learning experiance for me. I'm really new to this stuff.

    Java Code:
    import java.applet.*;
    import java.awt.*;
    
    public class Ball extends Applet implements Runnable
    {
      private Image testimg;
      
      int radius = 50; // Ball's Radius
      
      boolean testspace = false; // Test If Space Is Pressed, You should Draw test.png
      
      int x_pos_P1 = 10;  // x - PLAYER1 Position
      int y_pos_P1 = 100; // y - PLAYER1 Position
      
      int x_pos_P2 = 10;  // x - PLAYER2 Position
      int y_pos_P2 = 100; // y - PLAYER2 Position
      
      int x_pos_AI = 1000; // x - AI Position
      int y_pos_AI = 750; // y - AI Position
      
      int x_pos_AI2 = 900; // x - AI2 Position
      int y_pos_AI2 = 650; // y - AI2 Position
      
      int x_mov_P1 = 1; // PLAYER1 Type Of Horizontal Movement
      int y_mov_P1 = 1; // PLAYER1 Type Of Vertical Movement
      
      int x_mov_P2 = 1; // PLAYER2 Type Of Horizontal Movement
      int y_mov_P2 = 1; // PLAYER2 Type Of Vertical Movement
      
      int spd = 9; // How Fast The PLAYER1&2 Ball Moves
      int spd_AI = 50; // How Fast The AI Moves (higher = slower, lower = faster)
      
      GridKeyListener gkl = new GridKeyListener();
      
      public void init()
      {
        setBackground (Color.black);
      }
      
      public void start ()
      {
        // Defining Thread
        Thread th = new Thread (this);
        // Starting Thread 
        this.addKeyListener(gkl);
        th.start ();
      }
      
      public void stop()
      {
        
      }
      
      public void destroy()
      {
        
      }
      
      public void run ()
      {
    
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
        
        while (true)
        {
          // Checks Type Of Movement
          x_mov_P1 = gkl.getP1XMov();
          y_mov_P1 = gkl.getP1YMov();
          
          x_mov_P2 = gkl.getP2XMov();
          y_mov_P2 = gkl.getP2YMov();
          
          testspace = gkl.getSpace();
          
          // Which Way Ball Moves
          if(x_mov_P1 == 1){x_pos_P1 += spd;}
          if(x_mov_P1 == 2){x_pos_P1 -= spd;}
          if(y_mov_P1 == 1){y_pos_P1 += spd;}
          if(y_mov_P1 == 2){y_pos_P1 -= spd;}
          
          if(x_mov_P2 == 1){x_pos_P2 += spd;}
          if(x_mov_P2 == 2){x_pos_P2 -= spd;}
          if(y_mov_P2 == 1){y_pos_P2 += spd;}
          if(y_mov_P2 == 2){y_pos_P2 -= spd;}
          
          // Make AI Follow You
            x_pos_AI += (x_pos_P1-x_pos_AI)/spd_AI;
            y_pos_AI += (y_pos_P1-y_pos_AI)/spd_AI;
            
            x_pos_AI2 += (x_pos_P2-x_pos_AI2)/spd_AI;
            y_pos_AI2 += (y_pos_P2-y_pos_AI2)/spd_AI;
          
          // Collision Testing
            
          // Refresh
          repaint();
          
          try
          {
            // Stops Thread For 20
            Thread.sleep (20);
          }
          catch (InterruptedException ex)
          {
            
          }
          
          // Priority Level
          Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
        }
      }
      
      public void update(Graphics g)
      {
        if(testspace){
          testimg = getImage(getCodeBase(), "test.png");
          
          g.setColor (getBackground ());
          g.fillRect (0, 0, this.getSize().width, this.getSize().height);
          
          g.setColor (getForeground());
          
          paint(g);
          
          g.drawImage (testimg, x_pos_P1-2*radius, y_pos_P1-2*radius, this);
        } else { 
          g.setColor (getBackground ());
          g.fillRect (0, 0, this.getSize().width, this.getSize().height);
          
          paint(g);
        }
      }
        
      
      public void paint (Graphics g)
      {
        // Draws The Ball And Colors It.
        g.setColor  (Color.yellow);
        g.fillOval (x_pos_AI - radius, y_pos_AI - radius, 2 * radius, 2 * radius);
        
        g.setColor  (Color.yellow);
        g.fillOval (x_pos_AI2 - radius, y_pos_AI2 - radius, 2 * radius, 2 * radius);
        
        g.setColor  (Color.red);
        g.fillOval (x_pos_P1 - radius, y_pos_P1 - radius, 2 * radius, 2 * radius);
        
        g.setColor  (Color.blue);
        g.fillOval (x_pos_P2 - radius, y_pos_P2 - radius, 2 * radius, 2 * radius);
        
        String p1_msg = "PLAYER 1";
        String p2_msg = "PLAYER 2";
        Font small = new Font("Helvetica", Font.BOLD, 32);
        FontMetrics metr = this.getFontMetrics(small);
        
        g.setColor(Color.cyan);
        g.setFont(small);
        g.drawString(p1_msg, ((x_pos_P1 + (radius+(radius/2+(radius/8)))) - metr.stringWidth(p1_msg)), y_pos_P1 - radius);
        g.drawString(p2_msg, ((x_pos_P2 + (radius+(radius/2+(radius/8)))) - metr.stringWidth(p2_msg)), y_pos_P2 - radius);
      }
      
    }

    Java Code:
    import java.awt.event.*;
    
    public class GridKeyListener implements KeyListener{
      
      int p1_xMov = 0;
      int p1_yMov = 0;
      int p2_xMov = 0;
      int p2_yMov = 0;
      boolean space = false;
      
      public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
          case KeyEvent.VK_SPACE:
            space = true;
            break;
          case KeyEvent.VK_LEFT:
            p1_xMov = 2;
            break;
          case KeyEvent.VK_RIGHT:
            p1_xMov = 1;
            break;
          case KeyEvent.VK_UP:
            p1_yMov = 2;
            break;
          case KeyEvent.VK_DOWN:
            p1_yMov = 1;
            break;
          case KeyEvent.VK_A:
            p2_xMov = 2;
            break;
          case KeyEvent.VK_D:
            p2_xMov = 1;
            break;
          case KeyEvent.VK_W:
            p2_yMov = 2;
            break;
          case KeyEvent.VK_S:
            p2_yMov = 1;
            
        } 
      }
      
      public void keyReleased(KeyEvent e) {
        switch (e.getKeyCode()) {
          case KeyEvent.VK_SPACE:
            space = false;
            break;
          case KeyEvent.VK_LEFT:
            p1_xMov = 0;
            break;
          case KeyEvent.VK_RIGHT:
            p1_xMov = 0;
            break;
          case KeyEvent.VK_UP:
            p1_yMov = 0;
            break;
          case KeyEvent.VK_DOWN:
            p1_yMov = 0;
            break;
          case KeyEvent.VK_A:
            p2_xMov = 0;
            break;
          case KeyEvent.VK_D:
            p2_xMov = 0;
            break;
          case KeyEvent.VK_W:
            p2_yMov = 0;
            break;
          case KeyEvent.VK_S:
            p2_yMov = 0;
        } 
      }
      
      public void keyTyped(KeyEvent e) { }
      
      public boolean getSpace(){
        return space;
      }
      
      public int getP1XMov(){
        return p1_xMov;
      }
      
      public int getP1YMov(){
        return p1_yMov;
      }
      
      public int getP2XMov(){
        return p2_xMov;
      }
      
      public int getP2YMov(){
        return p2_yMov;
      }
      
    }
    Last edited by AmpedNavy; 03-10-2012 at 06:42 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: I Really Need Help Understand Buffering Images And How To Properly Use Graphics..

    One suggestion, move the call to getImage() out of the update method.

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

    Default Re: I Really Need Help Understand Buffering Images And How To Properly Use Graphics..

    If you're new to Java, why are you doing AWT? Swing has been around for more than 10 years now, and gives you double buffering for free.

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

  4. #4
    AmpedNavy is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default Re: I Really Need Help Understand Buffering Images And How To Properly Use Graphics..

    Quote Originally Posted by DarrylBurke View Post
    If you're new to Java, why are you doing AWT? Swing has been around for more than 10 years now, and gives you double buffering for free.

    db
    I'm not sure what AWT or Swing is... I'll look it up though.

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

    Default Re: I Really Need Help Understand Buffering Images And How To Properly Use Graphics..

    AWT -> java.awt.* and subpackages.
    Swing -> javax.swing.* and subpackages.

    Note that Swing is built on top of AWT, so many non-visual AWT classes like layouts, events and listeners are still used in a Swing application.

    Learn Swing from Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)

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

  6. #6
    AmpedNavy is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default Re: I Really Need Help Understand Buffering Images And How To Properly Use Graphics..

    Quote Originally Posted by DarrylBurke View Post
    AWT -> java.awt.* and subpackages.
    Swing -> javax.swing.* and subpackages.

    Note that Swing is built on top of AWT, so many non-visual AWT classes like layouts, events and listeners are still used in a Swing application.

    Learn Swing from Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)

    db
    I don't think I'll use Swing, it's much slower and buggier than awt(according to google searches), but thanks.

    I didn't want to make a new thread for this question, but could you tell me how to set the size of applets? I've googled it a hundred times, all I can find is setSize(500, 500); which doesn't work in init() or start().. /:

    (using Dr.Java, I comply, run, then it opens without reflecting the size I want when using setSize)

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

    Default Re: I Really Need Help Understand Buffering Images And How To Properly Use Graphics..

    Quote Originally Posted by AmpedNavy View Post
    I don't think I'll use Swing, it's much slower and buggier than awt(according to google searches), but thanks.
    Your searches are faulty as this is most definitely not true. You are doing yourself a disservice if you stick with AWT as your GUI will lack much power and flexibility, and since hardly anyone is using it nowadays, you will have a hard time finding help for problems you have with your code. But this is entirely up to you. I just hate to see someone making such a terrible mistake out of ignorance that I had to say something.

    I didn't want to make a new thread for this question, but could you tell me how to set the size of applets? I've googled it a hundred times, all I can find is setSize(500, 500); which doesn't work in init() or start().. /:

    (using Dr.Java, I comply, run, then it opens without reflecting the size I want when using setSize)
    applets are sized in the HTML code, not the Java code.

  8. #8
    AmpedNavy is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default Re: I Really Need Help Understand Buffering Images And How To Properly Use Graphics..

    Hmm, so swing is faster? How would using both Swing and awt affect my program? Because, I don't want to switch to just using swing seeing as I have to replace all my code ... Unless that's the only way.
    Last edited by AmpedNavy; 03-11-2012 at 12:15 AM.

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

    Default Re: I Really Need Help Understand Buffering Images And How To Properly Use Graphics..

    Quote Originally Posted by AmpedNavy View Post
    Hmm, so swing is faster?
    I have not profiled them, but from my experience, their speeds are comparable. But also Swing comes with double-buffering of graphics out of the box, and has elegant ways to do background threading.

    How would using both Swing and awt affect my program? Because, I don't want to switch to just using swing seeing as I have to replace all my code ...
    With newer versions of Java, it has been easier to use both, but sometimes combining AWT with Swing components can make graphics, menus and popups behave funny.

  10. #10
    AmpedNavy is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default Re: I Really Need Help Understand Buffering Images And How To Properly Use Graphics..

    Thanks for the advice. I'll do my best to switch my code over to Swing. (Tutorial reading time.. xD)

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

    Default Re: I Really Need Help Understand Buffering Images And How To Properly Use Graphics..

    For a functionally equivalent GUI, Swing is expected to be marginally faster than AWT, not that a user would ever notice the microseconds of difference. It's also very definitely going to have a smaller memory footprint as a Swing component is 'lightweight' -- it is painted in the client area of its Container -- whereas AWT components are 'heavyweight' -- each AWT Component maps to a OS widget.

    I don't know what gave you the idea that Swing is
    much slower and buggier than awt
    That's simply not true.

    I'll do my best to switch my code over to Swing. (Tutorial reading time.. xD)
    Good attitude. I'm sure you'll find it easier that you expected. Most of the work involves prefixing the class names with "J" but the painting process, and the recommended way to do your own painting, is quite different so do go through this Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

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

Similar Threads

  1. Double buffering basics for 2d game images
    By xsimsyx in forum New To Java
    Replies: 7
    Last Post: 10-08-2011, 11:10 PM
  2. Replies: 0
    Last Post: 09-22-2011, 12:02 PM
  3. Double buffering graphics problem
    By Guy in forum Java 2D
    Replies: 13
    Last Post: 08-30-2011, 09:22 PM
  4. Replies: 1
    Last Post: 12-04-2010, 03:07 PM
  5. Double Buffering Graphics in a Frame
    By Atriamax in forum New To Java
    Replies: 6
    Last Post: 11-02-2009, 02:25 AM

Posting Permissions

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