Results 1 to 6 of 6
  1. #1
    omidelf is offline Member
    Join Date
    Jan 2016
    Posts
    8
    Rep Power
    0

    Default questions about this ping pong game's code ?

    so i have some question about this ping pong game code
    1. what is that enableEvents(java.awt.AWTEvent.KEY_EVENT_MASK); for ?
    2. what does true mean in this : Timer t = new Timer(true); ? why not just leave it with blank ?
    3. what is that class SetLook ?! if i remove it nothing happens and program runs fine ?
    4. what is the difference between saying : frame.add( new PingPong() ) instead of creating a new PingPing() object ( Pong for example ) and saying frame.add( pong) ?
    5. what is that public boolean isFocusable() for ? what does it do ?



    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JFrame;
    import javax.swing.UIManager;
    import javax.swing.ImageIcon;
    import java.util.*;
    import javax.swing.JPanel;
    
    public class PingPong extends JPanel
    {
    int x,y,dx,dy;
    int a,b,da;
    int c,d,dc;
    int p1 = 0;
    int p2 = 0;
    
    public PingPong()
    {
    enableEvents(java.awt.AWTEvent.KEY_EVENT_MASK);
    //requestFocus();
    
    x = 100;
    y = 250;
    dx = 5;
    dy = 5;
    
    a = 200;
    b = 450;
    da = 10;
    
    c = 150;
    d = 20;
    dc = 10;
    
    Timer t = new Timer(true);
    t.schedule(new java.util.TimerTask()
        {
        public void run()
            {
            doSomething();
            repaint();
            }
        },0,30);
    }
    
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
              RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setColor(Color.black);
        g2d.fillOval(x, y, 20, 20);
    
        g2d.setColor(Color.red);
        g2d.fillRect(a, b, 100, 10);
    
        g2d.setColor(Color.red);
        g2d.fillRect(c, d, 100, 10);
    
        g2d.drawString("Player 2 Score : " + p1 + "Points ", 100, 200);
        g2d.drawString("Player 1 Score : " + p2 + "Points ", 100, 230);
    }
    public void processKeyEvent(KeyEvent e)
    {
        if ( e.getID() == KeyEvent.KEY_PRESSED )
        {
            if ( e.getKeyCode() == KeyEvent.VK_S && a<275)
            {
            a = a + da; 
            }
    
            if ( e.getKeyCode() == KeyEvent.VK_A && a>0 )
            {
            a = a - da;
            }
    
            if ( e.getKeyCode() == KeyEvent.VK_L && c<275)
            {
            c = c + dc; 
            }
    
            if ( e.getKeyCode() == KeyEvent.VK_K && c>0 )
            {
            c = c - dc;
            }
        }
    }
    
    public void doSomething()
        {
        x = x + dx;
        y = y + dy;
    
            if(x < 0 || x+20 > 360)
            {
            dx = -dx;
            }
            
            else if(y<20)
            {
            System.out.println("1 Emtiaz Baraye Player Shomare 1 !!");
            y = 200;
            p2++;
            dy = -dy;
            }
            
            else if( y-10 == d && x > c && x < (c+100) )
            {
            dy = -dy;
            }
    
            else if( y+20 == b && x > a && x < (a+100) )
            {
            dy = -dy;
            }
            
            else if(y > 440)
            {
            System.out.println("1 Emtiaz Baraye Player Shomare 2 !!");
            p1++;
            y = 300;
            dy = -dy;
            }
    
            else{}  
        }
    
    public boolean isFocusable() 
        {
        return true;
        }
    
    public static void main(String[] args)
        {
        JFrame frame = new JFrame("Pin Ball");
        
        Toolkit tool = frame.getToolkit();
        Dimension screenSize = tool.getScreenSize();
        int width = screenSize.width;
        int height = screenSize.height;
        frame.setBounds((width/3),(height/4), width, height);
        ImageIcon img = new ImageIcon("frameIcon.GIF");
        frame.setIconImage(img.getImage());
        frame.setSize(375,500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new PingPong());
        frame.setVisible(true);
    
        SetLook.lookAndFeel();
        }
    }
    
    
    class SetLook
    {
        public static void lookAndFeel()
            {
            try
                {
                String s = UIManager.getSystemLookAndFeelClassName();
                UIManager.setLookAndFeel(s);
                }
            catch(Exception e){}
            }
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: questions about this ping pong game's code ?

    You need to learn to use the Java docs for the base Java API.
    Then you can look up enableEvents in JPanel (you need to dig down into a parent class) as well as isFocusable, and look up the java.util.Timer class and read what that constructor does.

    So that covers 3 of your points.

    For (3), if you remove SetLook then the code won't compile. If you also remove the call to its method then I believe Swing defaults to the system look and feel anyway, so the class is a bit pointless. Swing used to default to the Java look and feel, so maybe this is old code, or simply left overs?

    For (4) there is no practical difference, but it's generally not a good idea to leave variables hanging around if you don't have to. The PingPong instance is only needed for that add() call, so may as well simply create it as part of the call.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: questions about this ping pong game's code ?

    I see you added the code I provided in the other post on this topic.

    http://www.java-forums.org/new-java/...-properly.html

    Yet you never provided any feedback. Now you expect more help. Hmmm.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    omidelf is offline Member
    Join Date
    Jan 2016
    Posts
    8
    Rep Power
    0

    Default Re: questions about this ping pong game's code ?

    Quote Originally Posted by jim829 View Post
    I see you added the code I provided in the other post on this topic.

    http://www.java-forums.org/new-java/...-properly.html

    Yet you never provided any feedback. Now you expect more help. Hmmm.

    Regards,
    Jim
    sorry i was waiting untill i have the final code then i would replay since this program still has a lot of work to do

  5. #5
    omidelf is offline Member
    Join Date
    Jan 2016
    Posts
    8
    Rep Power
    0

    Default Re: questions about this ping pong game's code ?

    Quote Originally Posted by Tolls View Post
    You need to learn to use the Java docs for the base Java API.
    Then you can look up enableEvents in JPanel (you need to dig down into a parent class) as well as isFocusable, and look up the java.util.Timer class and read what that constructor does.

    So that covers 3 of your points.

    For (3), if you remove SetLook then the code won't compile. If you also remove the call to its method then I believe Swing defaults to the system look and feel anyway, so the class is a bit pointless. Swing used to default to the Java look and feel, so maybe this is old code, or simply left overs?

    For (4) there is no practical difference, but it's generally not a good idea to leave variables hanging around if you don't have to. The PingPong instance is only needed for that add() call, so may as well simply create it as part of the call.

    i only found this about IsFocusable() :
    public boolean isFocusable()
    Returns whether this Component can be focused.
    Returns:
    true if this Component is focusable; false otherwise.


    but it doesn't say much , why this program needs this function to run properly ? because if i remove it then the keyboard won't work , but why ?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: questions about this ping pong game's code ?

    A component can only react to the likes of a keyboard event if it has the focus. It can only have the focus if it is focusable.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 1
    Last Post: 01-12-2016, 08:46 PM
  2. I need source code for a JavaFX ping pong game
    By TOAA in forum New To Java
    Replies: 3
    Last Post: 06-21-2015, 02:56 AM
  3. simple ping pong game help.
    By silvercats in forum New To Java
    Replies: 1
    Last Post: 07-14-2014, 04:10 PM
  4. ping pong
    By ahmed_ijsh in forum Threads and Synchronization
    Replies: 2
    Last Post: 11-01-2010, 01:56 PM
  5. Ping pong game
    By adam405 in forum New To Java
    Replies: 8
    Last Post: 10-20-2010, 04:52 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
  •