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

Thread: java game programming problem 1

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java game programming problem 1

    whats the problem in following code , why the ractangle not going to x++

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Toolkit;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;

    import javax.swing.JFrame;
    import javax.swing.JPanel;

    class class2 extends JFrame implements Runnable,KeyListener
    { public int width =500;
    public int height=300;
    public int x=30;
    public int y=50;
    public Thread t;
    private boolean r;
    class2()
    { //setIconImage(Toolkit.getDefaultToolkit().getImage( "C:\\Users\\user\\Desktop\\Snake Costume\\handpainted_cartoon_snake_05_vector_18140 0.jpg"));
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    this.setLocation(dim.width/3-this.getSize().width/2, dim.height/3-this.getSize().height/2);

    setSize(width,height);
    setResizable(false);
    setVisible(true);
    t =new Thread();
    t.start();
    }

    /*paint classess*/

    public void paint(Graphics g)
    {
    super.paintComponents(g);
    paint1(g);
    }
    public void paint1(Graphics g)
    { g.setColor(Color.BLUE);
    g.fillRect(x, y, 30, 20);
    }
    public void paint2(Graphics g)
    {

    }

    @Override
    public void run()
    {


    while(true)
    {x++;
    try{


    Thread.sleep(100);
    repaint();
    }

    catch(InterruptedException e)
    {
    e.setStackTrace(null);
    }




    }
    }


    /* control classess */


    @Override
    public void keyPressed(KeyEvent arg0) {
    // TODO Auto-generated method stub

    }



    @Override
    public void keyReleased(KeyEvent arg0) {
    // TODO Auto-generated method stub

    }



    @Override
    public void keyTyped(KeyEvent arg0) {
    // TODO Auto-generated method stub

    }




    }
    public class class1 {
    public static void main(String args[])
    {
    new class2();

    }

    }


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: java game programming problem 1

    Quote Originally Posted by swikot View Post
    whats the problem in following code
    Sorry but there are several wrong/inappropriate things in your code:

    1) JFrame is a complex top-level component (a window) and painting directly into it is not (generally) a good choice.
    2) In Swing components (not only simple ones but also JFrame, JDialog, etc) overriding the paint method is technically possible but should be done only if you know what are you doing.
    3) Overriding paint and then invoke super.paintComponents(g) is not a good choice (it's not bad by itself but it's not a common thing to do).
    4) You are using a new thread. The 'x' variable is read in the context of the EDT, Event Dispatch Thread, (in paint -> paint1) and written in the context of your new thread. Without appropriate synchronization, there's no guarantee that the modification operated by x++ is "seen" in the context of the EDT.

    For basic timings use javax.swing.Timer.
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,741
    My Mood
    Mellow
    Thanks
    172
    Thanked 622 Times in 609 Posts

    Default Re: java game programming problem 1

    Welcome! Please read this topic to learn how to post code in code or highlight tags and other useful info for newcomers.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,105
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: java game programming problem 1

    Is the run() method being called? Add a println() statement inside run() that prints a message to see.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: java game programming problem 1

    Quote Originally Posted by Norm View Post
    Is the run() method being called? Add a println() statement inside run() that prints a message to see.
    Good point ... I have not noticed immediately.

    @swikot:

    Instead to do:
    t =new Thread();

    do
    t =new Thread(this);

    because otherwise you certainly create a Thread object but that don't know to run your Runnable instance!!

    But please swikot, review carefully my conceptual notices, they are also equally (if not more) important.
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

Similar Threads

  1. Java Game Programming Tutorials - Slick2D library
    By worsewicked in forum Java Programming Tutorials
    Replies: 2
    Last Post: June 14th, 2013, 11:58 AM
  2. question - java game programming
    By haimdorlevi in forum Java Theory & Questions
    Replies: 1
    Last Post: April 30th, 2013, 03:44 AM
  3. android programming vs game programming using java
    By vgoel38 in forum Android Development
    Replies: 4
    Last Post: September 8th, 2012, 05:48 PM
  4. Java Programming - Dice Game Help!
    By blackvelvet in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 19th, 2012, 11:47 AM
  5. Robo Code - The funny Java Programming Game
    By Freaky Chris in forum The Cafe
    Replies: 20
    Last Post: October 8th, 2009, 03:42 PM