Results 1 to 20 of 21
Thread: game componets
- 03-30-2012, 06:52 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 70
- Rep Power
- 0
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.
- 03-30-2012, 07:10 PM #2
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.
- 03-30-2012, 07:13 PM #3
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,606
- Rep Power
- 5
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)
- 03-30-2012, 09:21 PM #4
Member
- Join Date
- Mar 2012
- Posts
- 70
- Rep Power
- 0
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); } }
- 03-30-2012, 09:25 PM #5
Re: game componets
Please explain what the code does on the different PCs.this code worksIf you don't understand my response, don't ignore it, ask a question.
- 03-30-2012, 09:41 PM #6
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,606
- Rep Power
- 5
Re: game componets
A component must have focus to receive key events. Look into using How to Use Key Bindings (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
- 03-31-2012, 05:37 AM #7
Member
- Join Date
- Mar 2012
- Posts
- 70
- Rep Power
- 0
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.
- 03-31-2012, 05:40 AM #8
Member
- Join Date
- Mar 2012
- Posts
- 70
- Rep Power
- 0
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.
- 03-31-2012, 01:33 PM #9
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-31-2012, 01:44 PM #10
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.
- 03-31-2012, 06:42 PM #11
Member
- Join Date
- Mar 2012
- Posts
- 70
- Rep Power
- 0
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) {} }
- 03-31-2012, 06:47 PM #12
Member
- Join Date
- Mar 2012
- Posts
- 70
- Rep Power
- 0
Re: game componets
(prw notices that db stands for darryl burke and retracts question)
- 03-31-2012, 06:55 PM #13
Re: game componets
This is confusing. Where is the keyEvent class defined?the constants defined in the keyEvent class were not recognized
Can you post the error messages showing the "not recognized" bit?
The code works on my XP PCLast edited by Norm; 03-31-2012 at 07:00 PM.
If you don't understand my response, don't ignore it, ask a question.
- 04-01-2012, 07:02 AM #14
Re: game componets
Quite a few of us here do care. Recommended reading.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
-
Re: game componets
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?
- 04-05-2012, 07:30 AM #16
Member
- Join Date
- Mar 2012
- Posts
- 70
- Rep Power
- 0
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) { } }
- 04-05-2012, 08:01 AM #17
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:
dbJava Code:import static java.awt.event.KeyEvent.VK_SPACE; // or import static java.awt.event.KeyEvent.*;
Why do they call it rush hour when nothing moves? - Robin Williams
- 04-05-2012, 08:09 AM #18
Member
- Join Date
- Mar 2012
- Posts
- 70
- Rep Power
- 0
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?
- 04-05-2012, 08:38 AM #19
Re: game componets
Because the JLS says so, in Chapter*7.*Packages
Go through all sub-sections of that section.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
-
Re: game componets
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.
Similar Threads
-
How can I to print (printer) a JPanel and its componets (buttons and labels)?
By mad72584 in forum New To JavaReplies: 3Last Post: 08-15-2011, 01:56 PM -
Complete Game Engine for beginner and intermediate game programmers
By rdjava in forum Java GamingReplies: 1Last Post: 06-02-2011, 09:29 AM -
Implementing "Game Over" in Minesweeper game based on Gridworld framework.
By JFlash in forum New To JavaReplies: 2Last Post: 08-05-2010, 04:49 AM -
game code for any game
By deathnote202 in forum Java GamingReplies: 4Last Post: 06-10-2010, 08:06 AM -
2D strategy game or 2D war game
By led1433 in forum Java 2DReplies: 5Last Post: 02-10-2009, 06:00 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks