11 Replies - 411 Views - Last Post: 02 October 2011 - 06:51 PM Rate Topic: -----

#1 samdoor  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 7
  • Joined: 02-October 11

Please kindly explain my codes. i really dont understand it.

Posted 02 October 2011 - 03:36 AM

package m_snake;

import java.awt.Container;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JApplet;
import javax.swing.JFrame;
import m_snake.MainPanel.myPanel;
import m_snake.MainPanel.snakeBody.snake;


public class MainFrame extends JApplet implements KeyListener{

    snake s = new snake();
    int windowSize = s.getWindow_Size();
    private final int LEFT = 37;
    private final int RIGHT = 39;
    private final int UP = 38;
    private final int DOWN = 40;
    private myPanel snakePanel=new myPanel();


    /** Creates a new instance of MainFrame */
    @Override
    public void init() {
        setSize(windowSize,windowSize);
        Container cpane=getContentPane();
        
        cpane.add(snakePanel);
        addKeyListener(this);
    }
    /**
     * @param args the command line arguments
     *
     */
    public static void main(String[] args) {
        // TODO code application logic here
        MainFrame my_Game=new MainFrame();
        JFrame myFrame=new JFrame(" Simple Snake Game ");
        snake sn = new snake();
        myFrame.setSize(sn.getWindow_Size(),sn.getWindow_Size());
        myFrame.getContentPane().add(my_Game);
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.addKeyListener(my_Game);
        
        my_Game.init();
        my_Game.start();
        my_Game.setVisible(true);
        myFrame.setVisible(true);
        
    }
    
    public void keyTyped(KeyEvent e) {
    }
    
    public void keyPressed(KeyEvent e) {
        
        if(e.getKeyCode()==UP) {
            snakePanel.set_Direction(1);
        } else if (e.getKeyCode()==RIGHT) {
            snakePanel.set_Direction(2);
        } else if(e.getKeyCode()==DOWN) {
            snakePanel.set_Direction(3);
        } else if(e.getKeyCode()==LEFT) {
            snakePanel.set_Direction(4);
        }
    }
    
    public void keyReleased(KeyEvent e) {
    }

}


This post has been edited by no2pencil: 02 October 2011 - 03:53 AM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Please kindly explain my codes. i really dont understand it.

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Please kindly explain my codes. i really dont understand it.

Posted 02 October 2011 - 03:56 AM

That's a lot of code to explain. Books have been written to explain that code. You ight own such a book. If so, I recommend you consult it. If not, you can find information online about each of the program's statements that you don't understand by Googling. If you're taking a class and are supposed to understand the code you've posted, you should ask your instructor or other students - the ones who seem to be getting it - for help.

If you can narrow your question down to one that can be answered in reasonable time and space, please do so.
Was This Post Helpful? 2
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Please kindly explain my codes. i really dont understand it.

Posted 02 October 2011 - 04:00 AM

So it is better if you tell where in the code you didnt understand..
SO this is the main part of your code:
public static void main(String[] args) {
// TODO code application logic here
MainFrame my_Game=new MainFrame();//create an instance of JApplet
JFrame myFrame=new JFrame(" Simple Snake Game "); // create an instance of JFrame
snake sn = new snake();//Create instance of snake class
myFrame.setSize(sn.getWindow_Size(),sn.getWindow_Size());//set size of the JFrame
myFrame.getContentPane().add(my_Game);//Adding to the Content pane of the JFrame 
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Set default operation when exit clicked
myFrame.addKeyListener(my_Game);//add key listener

my_Game.init();//initialize applet
my_Game.start();//start applet
my_Game.setVisible(true); //set visible
myFrame.setVisible(true);//set visible
}


So what part of the code you did not understand or confused or gives you error?
Was This Post Helpful? 2
  • +
  • -

#4 samdoor  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 7
  • Joined: 02-October 11

Re: Please kindly explain my codes. i really dont understand it.

Posted 02 October 2011 - 05:19 AM

View Postsmohd, on 02 October 2011 - 04:00 AM, said:

So it is better if you tell where in the code you didnt understand..
SO this is the main part of your code:
public static void main(String[] args) {
// TODO code application logic here
MainFrame my_Game=new MainFrame();//create an instance of JApplet
JFrame myFrame=new JFrame(" Simple Snake Game "); // create an instance of JFrame
snake sn = new snake();//Create instance of snake class
myFrame.setSize(sn.getWindow_Size(),sn.getWindow_Size());//set size of the JFrame
myFrame.getContentPane().add(my_Game);//Adding to the Content pane of the JFrame 
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Set default operation when exit clicked
myFrame.addKeyListener(my_Game);//add key listener

my_Game.init();//initialize applet
my_Game.start();//start applet
my_Game.setVisible(true); //set visible
myFrame.setVisible(true);//set visible
}


So what part of the code you did not understand or confused or gives you error?


Can u put comments with explanations every line in my codes? Please.. I will be very thankful if u help me with it.. because actually it will be my project in Java, a kind of game. My only objectives to do now is to analyze it with the help of your explanations like u did above..
Was This Post Helpful? -4
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Please kindly explain my codes. i really dont understand it.

Posted 02 October 2011 - 05:22 AM

I doubt that you'll find anyone to comment your code for you. That you'd ask adds significant doubt to the notion that it's your code.
Was This Post Helpful? 2
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Please kindly explain my codes. i really dont understand it.

Posted 02 October 2011 - 05:26 AM

I think you are the one to comment your code, please if you have specific problem with your code say it and we are happy to help. But commenting the whole is your work as the one who write it.
Was This Post Helpful? 2
  • +
  • -

#7 samdoor  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 7
  • Joined: 02-October 11

Re: Please kindly explain my codes. i really dont understand it.

Posted 02 October 2011 - 05:49 AM

View Postsmohd, on 02 October 2011 - 05:26 AM, said:

I think you are the one to comment your code, please if you have specific problem with your code say it and we are happy to help. But commenting the whole is your work as the one who write it.


Its okay, i understand.Yet im still very thankful to you because you explain it clear to me..
But one more thing big brother, Can i ask you a question?
Was This Post Helpful? 0
  • +
  • -

#8 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Please kindly explain my codes. i really dont understand it.

Posted 02 October 2011 - 06:02 AM

You are free to ask any question to the community and we are happy to help, just make sure to be precise and show your help.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Please kindly explain my codes. i really dont understand it.

Posted 02 October 2011 - 06:43 PM

"Explain my code. I really don't understand it"
How could you have written code that you don't understand ?
Are you sure it is your code ?
Do you think we are completly dumb ?
Was This Post Helpful? 1
  • +
  • -

#10 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5388
  • View blog
  • Posts: 27,384
  • Joined: 10-May 07

Re: Please kindly explain my codes. i really dont understand it.

Posted 02 October 2011 - 06:45 PM

View Postpbl, on 02 October 2011 - 09:43 PM, said:

Are you sure it is your code ?

Sure it is. It became his once he copied & pasted it into his project.
Was This Post Helpful? 3
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Please kindly explain my codes. i really dont understand it.

Posted 02 October 2011 - 06:50 PM

No need to waste more time of a such a topic type
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,273
  • Joined: 27-December 08

Re: Please kindly explain my codes. i really dont understand it.

Posted 02 October 2011 - 06:51 PM

We have plenty of tutorials in the Java Tutorials Section on Swing. Feel free to check those out and make an effort to learn how the code you found functions. You should also check out this link on Getting Better at Programming Java.

However, I think at this point, this more than applies.
Posted Image

Topic closed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1