insurektion Posted June 8, 2004 Share Posted June 8, 2004 How would I set a button to certain co-ordinates in java as well as show and hide buttons. I know how to show a button at the top but cant do anything with it i need much help. Link to comment Share on other sites More sharing options...
0 kjordan2001 Posted June 8, 2004 Share Posted June 8, 2004 To set a place: setBounds(x,y,width,height) To show/hide: setVisible(boolean tf) Link to comment Share on other sites More sharing options...
0 SIG Posted June 8, 2004 Share Posted June 8, 2004 You should also do a setLayout(null); to make sure you can move it around to wherever you want it. Especially if you are creating non-visual applets. Link to comment Share on other sites More sharing options...
0 insurektion Posted June 8, 2004 Author Share Posted June 8, 2004 // The "Outlaw" class. import java.applet.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class Outlaw extends Applet { ? ? ? ?Button startButton, fightButton, healthButton, weaponButton; ? ?int rndnum; ? ?int health = 100; ? ?int weapon = 0; ? ?int police = 5; ? ?int player = 4; ? ?int score = 0; ? ?int days = 30; ? ?String charname, gunname; ? ?Image healthPic; ? ?Image titlePic; ? ? ? ?public void init () ? ?{ ? ? ? ?startButton = new Button("Start Game"); ? ? ? ?add(startButton); ? ? ? ?healthPic = getImage (getCodeBase (), "health.jpg"); ? ? ? ?titlePic = getImage (getCodeBase (), "title.jpg"); ? ? ? ? ? ?} // init method ? ?public void paint (Graphics g) ? ?{ ? ? ? ?g.drawImage (titlePic, 0, 0, this); ? ?} ? ? ? ?public void actionPerformed(ActionEvent event, Graphics g) ? ?{ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?g.drawImage (healthPic, 0, 0, this); ? ? ? ? ? ? ? ? ? ? ? ? } } // Outlaw class this is what i have i am totally new to applets andwould like to know how to have a different image pop up once i click start game and have that button to disapear and add new buttons our comp sci teacher thayt it would be cool to try to make us learn applets in the last week of school for a final project. Also any good begginer tut sites for writing applets would be awesome. basically for the progra mthere is going to be a health and score and days and you shoot bad cops. ur health is random as well as your accuracy you can go to shooting range, hospital, or fight the cops i have a button for those things but i dont want to show til after the intro. Its supposed to look like an old japanese game see attachement. Link to comment Share on other sites More sharing options...
0 SIG Posted June 8, 2004 Share Posted June 8, 2004 import java.applet.*; import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*; public class Outlaw extends java.applet.Applet { private static Button startButton = new Button("Start Game"), fightButton = new Button("Fight!!!!11"), healthButton = new Button("Health!"), weaponButton = new Button("Weapon"), endGame = new Button("End Game!"); private static Label lblLabel = new Label(""); int rndnum; int health = 100; int weapon = 0; int police = 5; int player = 4; int score = 0; int days = 30; String charname, gunname; Image healthPic; Image titlePic; public void init () { setLayout(null); add(startButton); add(fightButton); add(healthButton); add(weaponButton); add(endGame); add(lblLabel); fightButton.setVisible(false); healthButton.setVisible(false); weaponButton.setVisible(false); endGame.setVisible(false); lblLabel.setBounds(150, 1, 200, 30); startButton.setBounds(1, 20, 100, 30); fightButton.setBounds(1, 60, 100, 30); healthButton.setBounds(1, 100, 100, 30); weaponButton.setBounds(1, 140, 100, 30); endGame.setBounds(1, 180, 100, 30); healthPic = getImage (getCodeBase (), "a.jpg"); titlePic = getImage (getCodeBase (), "b.jpg"); // Add an action listener to the button // Check class below startButton.addActionListener(new buttonEventManager(1)); fightButton.addActionListener(new buttonEventManager(2)); endGame.addActionListener(new buttonEventManager(3)); } // init method public void paint(Graphics g) { //g.drawImage(titlePic, 10, 1, this); } public static void StartGame() { lblLabel.setText("Game Started"); startButton.setVisible(false); fightButton.setVisible(true); healthButton.setVisible(true); weaponButton.setVisible(true); endGame.setVisible(true); } public static void Fight() { lblLabel.setText("The Fight starts!"); } public static void EndGame() { lblLabel.setText("Game Ends!"); startButton.setVisible(true); fightButton.setVisible(false); healthButton.setVisible(false); weaponButton.setVisible(false); endGame.setVisible(false); } } // Outlaw class /* * Class that will manage Button Events */ class buttonEventManager implements ActionListener { // Button ID for the Button int ButtonID; // Constructor public buttonEventManager(int ID) { ButtonID = ID; } // Check Button ID and perform according action public void actionPerformed(ActionEvent e) { switch(ButtonID) { case 1: Outlaw.StartGame(); break; case 2: Outlaw.Fight(); break; case 3: Outlaw.EndGame(); break; } } } I didn't understand your first question :p Anyway, I hope this will help. You need to add an ActionListener to your button to have it do anything. I have added a class that will handle this. It takes a parameter which is an ID. -- I don't know of any online guides. I am also new to Java, but I bought a book and it has everything needed there. Just go on google and search whatever you want to do :p :) Link to comment Share on other sites More sharing options...
0 insurektion Posted June 9, 2004 Author Share Posted June 9, 2004 I love you and want you to bear my children. Link to comment Share on other sites More sharing options...
0 insurektion Posted June 10, 2004 Author Share Posted June 10, 2004 but seriously thank you muchly this will be a good start since its due in 2 days. Link to comment Share on other sites More sharing options...
0 insurektion Posted June 10, 2004 Author Share Posted June 10, 2004 how do I make the label background transparent so its just black text on my picture background edit: could a mod please rename the thread title to "Help with Applet Program" Link to comment Share on other sites More sharing options...
0 kjordan2001 Posted June 10, 2004 Share Posted June 10, 2004 I think a JLabel should be transparent by default. Link to comment Share on other sites More sharing options...
0 insurektion Posted June 10, 2004 Author Share Posted June 10, 2004 no i have a gray background to try to hide. Link to comment Share on other sites More sharing options...
0 insurektion Posted June 11, 2004 Author Share Posted June 11, 2004 bump code is: import java.applet.*; import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*; public class Outlaw extends java.applet.Applet { private static Button startButton = new Button ("Start Game"), fightButton = new Button ("Fight!!!!11"), healthButton = new Button ("Health!"), weaponButton = new Button ("Weapon"), endGame = new Button ("End Game!"); private static Label lblLabel = new Label (""); int rndnum; int health = 100; int weapon = 0; int police = 5; int player = 4; int score = 0; int days = 30; String charname, gunname; Image healthPic; Image titlePic; public void init () { setLayout (null); add (startButton); add (fightButton); add (healthButton); add (weaponButton); add (endGame); add (lblLabel); lblLabel.setVisible (false); fightButton.setVisible (false); healthButton.setVisible (false); weaponButton.setVisible (false); endGame.setVisible (false); lblLabel.setBounds (150, 1, 200, 30); startButton.setBounds (1, 20, 100, 30); fightButton.setBounds (1, 60, 100, 30); healthButton.setBounds (1, 100, 100, 30); weaponButton.setBounds (1, 140, 100, 30); endGame.setBounds (1, 180, 100, 30); healthPic = getImage (getCodeBase (), "health.jpg"); titlePic = getImage (getCodeBase (), "title.jpg"); weaponPic = getImage (getCodeBase (), "weapon.jpg"); fightPic = getImage (getCodeBase (), "fight.jpg"); selectPic = getImage (getCodeBase (), "select.jpg"); // Add an action listener to the button // Check class below startButton.addActionListener (new buttonEventManager (1)); fightButton.addActionListener (new buttonEventManager (2)); healthButton.addActionListener (new buttonEventManager (3)); weaponButton.addActionListener (new buttonEventManager (4)); endGame.addActionListener (new buttonEventManager (5)); } // init method public void paint (Graphics g) { Font f = new Font("Small Fonts", Font.BOLD, 15); screen.setFont(f); g.drawImage(titlePic, 10, 1, this); } public static void StartGame () { lblLabel.setText ("Game Started"); score = 0 health = 100 days = 30 lblLabel.setVisible (true); startButton.setVisible (false); fightButton.setVisible (true); healthButton.setVisible (true); weaponButton.setVisible (true); endGame.setVisible (true); } public static void Fight () { lblLabel.setText ("The Fight starts!"); //Random Generator 1-5+weapon if x>=3 then x = 3 You Killed x Crooked Cops in bottom with new lbl other label shows score health days //Random Generator 1-50 You lost y health If health <=0 then outlaw.endGame days-- score = score + x (cops killed) health = health - y if days <= 0 then outlaw.endgame lblLabel.setText (Score + " "+Health+" Days:"+days); } public static void Health () { lblLabel.setText ("The FHeltht starts!"); //random generator 1-100 health = health + x if health>=150 then health = 150 days-- score = score + x (cops killed) health = health - y if days <= 0 then outlaw.endgame lblLabel.setText (Score + " "+Health+" Days:"+days); } public static void Weapon () { lblLabel.setText ("The Wepon starts!"); //random generator 1-30 if x<4 then weapon = weapon + 1 if x<2 then weapon = weapon + 1 days = days - 1 } public static void EndGame () { lblLabel.setText ("Game Ends!"); startButton.setVisible (true); fightButton.setVisible (false); healthButton.setVisible (false); weaponButton.setVisible (false); endGame.setVisible (false); } } // Outlaw class /* * Class that will manage Button Events */ class buttonEventManager implements ActionListener { // Button ID for the Button int ButtonID; // Constructor public buttonEventManager (int ID) { ButtonID = ID; } // Check Button ID and perform according action public void actionPerformed (ActionEvent e) { switch (ButtonID) { case 1: Outlaw.StartGame (); break; case 2: Outlaw.Fight (); break; case 3: Outlaw.Health (); break; case 4: Outlaw.Weapon (); break; case 5: Outlaw.EndGame (); break; } } } Link to comment Share on other sites More sharing options...
Question
insurektion
How would I set a button to certain co-ordinates in java as well as show and hide buttons. I know how to show a button at the top but cant do anything with it i need much help.
Link to comment
Share on other sites
10 answers to this question
Recommended Posts