Page 1 of 2 12 LastLast
Results 1 to 20 of 21
Like Tree2Likes

Thread: game componets

  1. #1
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default game componets

    before u ask fram is my window adapter
    im trying to rmove the jlabel after 1 second passes, but nothing happens

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.*;
    public class TFrame extends Fram
    {
        JLabel floor = new JLabel();
        public TFrame()
        {
            super("");
            setSize(500,500);
            setLayout(null);
            setBackground(Color.BLUE);
            add(floor);
            floor.setIcon(new ImageIcon("J:\\programmin\\WhiteBook\\pics\\bolt.jpg"));
            floor.setBounds(50,50,100,100);
            setVisible(true);
            //System.out.println(floor.getX());
            try
            {
                Thread.sleep(1000);
            }
            catch(InterruptedException e)
            {
            }
            remove(floor);
        }
    }
    Last edited by PRW56; 03-30-2012 at 06:54 PM.

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

    Default Re: game componets

    You need to exit the thread so the GUI thread can display the GUI. Use a Timer to execute a listener method a second later and have the listener method remove the label.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: game componets

    First, we are programmers and our brains often work like compilers. Meaning writing like 'before u ask' and 'trying to rmove' often results in a compile time error. Please take the time to write legibly, less you wish to confuse those you seek help from.

    Second, don't lock you your user interface. Swing is single threaded, and using Thread.sleep on that single thread (EDT) locks up the GUI. See Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)

  4. #4
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default Re: game componets

    okay i was gonna ask you about something but there is something odd about my computer
    this code works at my school, but not at my home so i know its right, nothing with key listeners works on this computer at my house, any suggestions?

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    public class KeyFrame extends Fram implements KeyListener
    {
        public KeyFrame(String a)
        {
            super(a);
            setTitle("Key Frame");
            addKeyListener(this);
        }
        public void keyPressed(KeyEvent e)
        {
            System.out.println("pressed");
        }
        public void keyTyped(KeyEvent e)
        {
           // System.out.println("typed");
        }
        public void keyReleased(KeyEvent e)
        {
            System.out.println("released");
        }
    }
    public class UseKeyFrame
    {
        public static void main(String[] args)
        {
            KeyFrame k = new KeyFrame("I dont know");
            k.setSize(250,50);
            k.setVisible(true);
        }
    }

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

    Default Re: game componets

    this code works
    Please explain what the code does on the different PCs.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: game componets


  7. #7
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default Re: game componets

    sorry all it does is print pressed and released whenever a key is pressed and released, it works fine on the computers i use at my school, but not at my home, i do not know why, doWhile that might explain why it didnt work the other time i was using it for an actual componet, but here (as in the short code i posted in #4)... i dont understand.

  8. #8
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default Re: game componets

    i forgot to mention something, if it helps, the constants defined in the keyEvent class were not recognized, i thought it was strange but i had a way around it so i forgot.

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

    Default Re: game componets

    If it's too much effort for you to use a capital letter at the start of each sentence, and for the first person singular 'I' you're never going to be able to summon the energy to write a Java program. Java is case sensitive.

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

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

    Default Re: game componets

    Where is the class Fram defined? Your code you posted won't compile for me.
    How were you able to compile and execute the code you posted?
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default Re: game componets

    ok 3 things 1 ill give you the code for fram right here, second darryl why the heck did you come and say that and not answer any question at all, yes it is not capitalized, who cares? all who see it can read and understand it so dont be so condescending to random people pls!! you are either the second or third moderator who has done that for absolutely no reason, last what does db mean? also just for the heck of typing exactly what im thinking, will you be one of the
    people to rage "kick" me just because you that mean, cause i can honestly see that happening, and the irony that your littly subscript-ish thing under you quote says to be nice...

    Java Code:
    import java.awt.event.*;
    import java.awt.*;
    public class Fram extends Frame implements WindowListener
    {
        public Fram(String str)
        {
            super(str);
            addWindowListener(this);
        }
        public void windowClosing(WindowEvent e)
        {
            System.exit(0);
        }
        public void windowClosed(WindowEvent e) {}
        public void windowDeiconified(WindowEvent e) {}
        public void windowIconified(WindowEvent e) {}
        public void windowOpened(WindowEvent e) {}
        public void windowActivated(WindowEvent e) {}
        public void windowDeactivated(WindowEvent e) {}
    }

  12. #12
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default Re: game componets

    (prw notices that db stands for darryl burke and retracts question)

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

    Default Re: game componets

    the constants defined in the keyEvent class were not recognized
    This is confusing. Where is the keyEvent class defined?
    Can you post the error messages showing the "not recognized" bit?

    The code works on my XP PC
    Last edited by Norm; 03-31-2012 at 07:00 PM.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: game componets

    Quote Originally Posted by PRW56 View Post
    yes it is not capitalized, who cares?
    Quite a few of us here do care. Recommended reading.

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

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

    Default Re: game componets

    Quote Originally Posted by PRW56 View Post
    ... second darryl why the heck did you come and say that and not answer any question at all, yes it is not capitalized, who cares?...
    You should care. Java naming conventions dictate that class names should start with a capital letter and that method and variable names should start with a lower-case letter. If you code this way, folks will understand your code much more fully and quickly.

    Since you're asking volunteers to help you with a project on their free time, do you really think that it's condescending to ask you to make it easier for us to be able to help you and to be able to better understand your code? Do you really think that his suggestions are being made for no reason?

  16. #16
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default Re: game componets

    first off sorry i exploded the other day i had heard several detrimental news, next do YOU honestly get confused by the way the punctuation is used... or should i say my lack of it, because i had assumed you guys were simply doing it for the sake of being... a know it all? If you honestly are confused by it, I will try to use it more often (after this post). I suck at debating anyway, so if we continued to I would look like a moron... a bigger moron. Ok anyway, it simply acts as if they were not defined in the code, as in " cannot find symbol - variable VK_SPACE ". Even though it should be defined in the KeyEvent class. I installed the JDK on two separate computers, it doesn't work on either one.

    here is code

    Java Code:
    import java.awt.*;
    import javax.swing.JLabel;
    import javax.swing.ImageIcon;
    import java.awt.event.*;
    import java.util.*;
    public class TFrame extends Fram implements KeyListener
    {
        JLabel floor = new JLabel();
        Button b = new Button("");
        Timer timer = new Timer();
        int x;
        int yloc = 300;
        public TFrame()
        {
            super("");
            setSize(500,500);
            setLayout(null);
            setBackground(Color.BLUE);
            add(floor);
            add(b);
            b.setBackground(Color.RED);
            b.setBounds(0,400,500,100);
            floor.setIcon(new ImageIcon("J:\\programmin\\WhiteBook\\pics\\bolt.jpg"));
            floor.setBounds(50,300,100,100);
            setVisible(true);
            //System.out.println("hey");
            addKeyListener(this);
            /*timer.schedule(new TimerTask() 
            {
                public void run() 
                {                
                    ++x;
                    yloc+=50;
                    floor.setBounds(50,yloc,100,100);
                }
            },5000);*/
        }
        public void grav() 
        {
            System.out.println("hey");
            x=0;
            timer.schedule(new TimerTask() 
            {
                public void run() 
                {                
                    ++x;
                    yloc-=20;
                    floor.setBounds(50,yloc,100,100);
                    if(x>10)
                    {
                        x=0;
                        timer.cancel();
                    }
                }
            },0,100);
            timer.schedule(new TimerTask() 
            {
                public void run() 
                {
                    if(floor.getY()+50<b.getY())
                    {
                        yloc+=20; 
                        floor.setBounds(50,yloc,100,100);
                    }
                    else
                    {
                        timer.cancel();
                    }
                }
            },1000,1000);
            
        }
        public void keyPressed(KeyEvent e)
        {
            
        }
        public void keyReleased(KeyEvent e)
        {
            System.out.println("hey");
            if(e.getKeyChar()==VK_SPACE)
            {
                System.out.println("hey");
                grav();
            }
        }
        public void keyTyped(KeyEvent e)
        {
            
        }
    }

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

    Default Re: game componets

    You need to either qualify the class constant VK_SPACE with the name of the class that defines it -- KeyEvent.VK_SPACE -- or add a static import of that or all fields of the class:
    Java Code:
    import static java.awt.event.KeyEvent.VK_SPACE;
    // or
    import static java.awt.event.KeyEvent.*;
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  18. #18
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default Re: game componets

    So why do I need to do that here, as opposed to how it would normally work by importing the entire package like I did in that simple code in post #4?

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

    Default Re: game componets

    Because the JLS says so, in Chapter*7.*Packages

    Go through all sub-sections of that section.

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

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

    Default Re: game componets

    Quote Originally Posted by PRW56 View Post
    So why do I need to do that here, as opposed to how it would normally work by importing the entire package like I did in that simple code in post #4?
    More to the point, you're not using a class, but instead are trying to access a static field of a class, and this requires a different type of import. Myself, I just use the class name with the constant as it makes things less confusing for me -- I know who "owns" the constant at a glance.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 3
    Last Post: 08-15-2011, 01:56 PM
  2. Replies: 1
    Last Post: 06-02-2011, 09:29 AM
  3. Replies: 2
    Last Post: 08-05-2010, 04:49 AM
  4. game code for any game
    By deathnote202 in forum Java Gaming
    Replies: 4
    Last Post: 06-10-2010, 08:06 AM
  5. 2D strategy game or 2D war game
    By led1433 in forum Java 2D
    Replies: 5
    Last Post: 02-10-2009, 06:00 AM

Tags for this Thread

Posting Permissions

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