943,649 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1868
  • Java RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Apr 4th, 2008
0

Re: Button Question/Issue

Click to Expand / Collapse  Quote originally posted by Ezzaral ...
Maybe something along these lines would work for you
java Syntax (Toggle Plain Text)
  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.FlowLayout;
  5. import javax.swing.*;
  6.  
  7. public class FrameExample extends JFrame {
  8.  
  9. JPanel panNorth;
  10. JPanel panSouth;
  11. JPanel panCenter;
  12.  
  13. public FrameExample() {
  14. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15.  
  16. setLayout(new BorderLayout());
  17.  
  18. // set up top panel
  19. panNorth = new JPanel();
  20. panNorth.setLayout(new FlowLayout());
  21. panNorth.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
  22. JLabel lblUpper = new JLabel("Upper Panel");
  23. panNorth.add(lblUpper);
  24. add(panNorth, BorderLayout.NORTH);
  25.  
  26. // set up center panel
  27. panCenter = new JPanel();
  28. panCenter.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
  29. panCenter.setLayout(new BorderLayout());
  30. JLabel lblMiddle = new JLabel("Middle thing");
  31. lblMiddle.setHorizontalAlignment(JLabel.CENTER);
  32. panCenter.add(lblMiddle, BorderLayout.CENTER);
  33. add(panCenter);
  34.  
  35. // set up bottom panel
  36. panSouth = new JPanel();
  37. panSouth.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
  38. panSouth.setLayout(new FlowLayout());
  39. JButton btnFirst = new JButton("Button 1");
  40. // if preferred size is not set then the button will size to fit the text on it
  41. btnFirst.setPreferredSize(new Dimension(100,18));
  42. panSouth.add(btnFirst);
  43. JButton btnSecond = new JButton("Button 2");
  44. btnSecond.setPreferredSize(new Dimension(100,18));
  45. panSouth.add(btnSecond);
  46. add(panSouth, BorderLayout.SOUTH);
  47.  
  48. setSize(300, 300);
  49. setVisible(true);
  50. }
  51.  
  52. public static void main(String args[]) {
  53. new FrameExample();
  54. }
  55. }
Cool, thanks. Good to know about PreferredSize control too. As for doing a layout for both the display screen and for the buttons, can I set that within the related panel, or do I have to set it up from a seperate panel? Specifically, if I have a panel named FireworksPanel that displays the game, can I setup the layout from within FireworksPanel to place it on North and the controls on South, or would I have to do that within a different panel?
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
CaffeineCoder is offline Offline
54 posts
since Feb 2008
Apr 4th, 2008
0

Re: Button Question/Issue

Each panel can keep all of it's layout setup completely internal (and should). The code that puts together the main frame should place the panels wherever they need to go.

Edit: Fireworks panel can still have as many internal panels as it wants to group components together. The main frame can still just treat it as a single component. If FireworksPanel has two internal panels, one for the screen and one for buttons, that won't affect the main frame at all. You don't have to put each piece into the frame - panels can have panels within panels within panels ad nauseum, so don't think each piece has to go into the top level container.
Last edited by Ezzaral; Apr 4th, 2008 at 6:58 pm.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Apr 6th, 2008
0

Re: Button Question/Issue

Click to Expand / Collapse  Quote originally posted by Ezzaral ...
Each panel can keep all of it's layout setup completely internal (and should). The code that puts together the main frame should place the panels wherever they need to go.

Edit: Fireworks panel can still have as many internal panels as it wants to group components together. The main frame can still just treat it as a single component. If FireworksPanel has two internal panels, one for the screen and one for buttons, that won't affect the main frame at all. You don't have to put each piece into the frame - panels can have panels within panels within panels ad nauseum, so don't think each piece has to go into the top level container.

OK, I'm trying to add the buttons as discussed (place the buttons down below display). 2 questions:
1) How would I set the display to be its own panel seperate from the buttons (or should I just have the buttons be its own panel layout and stop there)?
2) What I have below seems to have some sort of issue, as I can no longer see the score, or execute the rest of the program. What am I doing wrong?

Java Syntax (Toggle Plain Text)
  1. package Fireworks;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.KeyAdapter;
  7. import java.awt.event.KeyEvent;
  8. import java.awt.image.BufferedImage;
  9. import java.beans.PropertyChangeEvent;
  10. import java.beans.PropertyChangeListener;
  11. import javax.imageio.ImageIO;
  12. import java.util.*;
  13. import java.applet.*;
  14. import javax.swing.*;
  15. import javax.swing.event.*;
  16. import java.io.*;
  17. import java.net.*;
  18. import javax.sound.midi.*;
  19. import javax.sound.sampled.*;
  20. import java.applet.AudioClip;
  21. import javax.swing.JButton;
  22. import javax.imageio.*;
  23. import java.awt.Dimension;
  24. import java.beans.*;
  25.  
  26. public class FireworksPanel extends AbsFireworksPanel
  27. {
  28. Fireworks Game;
  29. FireworksSprite fireworks;
  30. FireworksSpinnerSprite spinner;
  31. FireworksRocketSprite rocket;
  32. private BufferedImage burst[];
  33. int GameScore;
  34. String explosion1 = "audio\\explosion1.wav";
  35. String spinner1 = "audio\\spinnerexplosion1.wav";
  36. String spinner2 = "audio\\fizzle.wav";
  37. String rocket1 = "audio\\rocketexplosion1.wav";
  38. Thread animation;
  39. Graphics2D dbg;
  40. private Image image;
  41. private int mx,my;
  42. JButton FireworksLaunch;
  43. JButton SpinnersLaunch;
  44. JButton RocketsLaunch;
  45. JPanel controls;
  46.  
  47. public FireworksPanel()
  48. {
  49. //initComponents();
  50. GameScore = 0;
  51. /*setLayout(new BorderLayout());
  52.   add(controls, BorderLayout.SOUTH);*/
  53. setPreferredSize(new Dimension(Constants.WIDTH, Constants.HEIGHT));
  54. initFireworks();
  55. Thread animation = new Thread (this);
  56. animation.start ();
  57. //callEndGame(dbg);
  58. /*
  59.   JPanel fwDisplayPanel = new JPanel(new FlowLayout());
  60.   JLabel scoreBox = new JLabel("Current Score");
  61.   fwDisplayPanel.setBackground(Color.BLACK);
  62.   Dimension p1dimension = new Dimension();
  63.   p1dimension.setSize(900, 500);
  64.   fwDisplayPanel.setSize(p1dimension);
  65.   fwDisplayPanel.setBorder(BorderFactory.createEtchedBorder());
  66.   */
  67. }
  68.  
  69. public void paint(Graphics g)
  70. {
  71. g.setColor(Color.black);
  72. g.fillRect(0,0,mx+900,my+900);
  73. }
  74.  
  75.  
  76. public void setGame(Fireworks Game)
  77. {
  78. this.Game = Game;
  79. }
  80.  
  81.  
  82. public Fireworks getGame()
  83. {
  84. return Game;
  85. }
  86.  
  87.  
  88. public void initFireworks()
  89. {
  90. fireworks = new FireworksSprite();
  91. spinner = new FireworksSpinnerSprite();
  92. rocket = new FireworksRocketSprite();
  93.  
  94. System.out.println("Initializing fireworks.");
  95. fireworks = new FireworksSprite();
  96. fireworks.setActive(false);
  97. fireworks.setVisible(false);
  98.  
  99. System.out.println("Initializing spinner.");
  100. spinner = new FireworksSpinnerSprite();
  101. spinner.setActive(false);
  102. spinner.setVisible(false);
  103.  
  104. System.out.println("Initializing rocket.");
  105. rocket = new FireworksRocketSprite();
  106. rocket.setActive(false);
  107. rocket.setVisible(false);
  108. }
  109.  
  110.  
  111. public void renderSprites(Graphics2D g2d)
  112. {
  113. DisplayGameScore(GameScore, g2d);
  114. fireworks.paintSprite(g2d);
  115. spinner.paintSprite(g2d);
  116. rocket.paintSprite(g2d);
  117. if(fireworks.getLocy()<55 && fireworks.getLocy()> 0)
  118. {
  119. System.out.println("firework burst and sound.");
  120. soundeffects(rocket1); //explosion1);
  121. initBurst(g2d, 0);
  122. }
  123.  
  124. if(rocket.getLocy()<55 && rocket.getLocy()> 0)
  125. {
  126. System.out.println("rocket burst and sound.");
  127. soundeffects(rocket1);
  128. initBurst(g2d, 1);
  129. }
  130.  
  131. if(spinner.getLocy()<55 && spinner.getLocy()> 0)
  132. {
  133. System.out.println("spinner burst and sound.");
  134. soundeffects(spinner1);
  135. initBurst(g2d, 2);
  136. soundeffects(spinner2);
  137. }
  138. FireworksShow();
  139. }
  140.  
  141. public void updateGame()
  142. {
  143. fireworks.updateSprite();
  144. spinner.updateSprite();
  145. rocket.updateSprite();
  146. }
  147.  
  148.  
  149. public void callEndGame(Graphics2D dbg)
  150. {
  151. if(gameOver)
  152. {
  153. Font font = new Font("Times Roman", Font.BOLD, 50);
  154. dbg.setFont(font);
  155. dbg.setColor(Color.WHITE);
  156. String GameMsg = "Game Over";
  157. dbg.drawString(GameMsg, 150, 150);
  158. dbg.setFont(new Font("Times New Roman", Font.BOLD, 50));
  159.  
  160. dbg.setColor(Color.YELLOW);
  161. dbg.drawString("Fireworks Display", 100, 100);
  162. dbg.drawString("Final Score:", 200, 200);
  163. dbg.setFont(new Font("Times New Roman", Font.BOLD, 15));
  164. dbg.drawString("Fireworks Display Program.", Constants.WIDTH/2 + 20, Constants.HEIGHT/2 + 40);
  165. dbg.drawString("Final Project, CS8680, Dr. Xu, KSU MSACS", Constants.WIDTH/2 + 20, Constants.HEIGHT/2 + 60);
  166. dbg.drawString("Nathan Williams", Constants.WIDTH/2 + 20, Constants.HEIGHT/2 + 80);
  167. dbg.drawString("Fall 2007.", Constants.WIDTH/2 + 20, Constants.HEIGHT/2 + 100);
  168. }
  169. }
  170.  
  171. public void SoundTrack()
  172. {
  173. //Initialize background music.
  174. /*bgsong = new SongSprite();
  175.   bgsong.setFilename(Constants.SONG);
  176.   //bgSound[0] = bgsong;
  177.   System.out.println("Now Playing Song");
  178.   //bgsong.loadClip();
  179.   bgsong.play();*/
  180. }
  181.  
  182.  
  183. public void soundeffects(String filename)
  184. {
  185. //Initialize audio sound effects.
  186. try
  187. {
  188. AudioClip FireworkSound = Applet.newAudioClip(getClass().getResource(filename));
  189. FireworkSound.play();
  190. }
  191. catch(Exception e)
  192. {
  193. System.out.println("Problem with " + filename);
  194. }
  195. }
  196.  
  197.  
  198. private void DisplayGameScore(int theGameScore, Graphics2D dbg)
  199. {
  200. dbg.setFont(new Font("Times New Roman", Font.BOLD, 30));
  201. dbg.setColor(Color.YELLOW);
  202. dbg.drawString("Current Score is: " + theGameScore, 150,30);
  203. }
  204.  
  205. public void FireworksShow()
  206. {
  207. JPanel GameDisplay;
  208. JPanel Controls;
  209.  
  210.  
  211. // setLayout(new BorderLayout());
  212.  
  213. GameDisplay = new JPanel();
  214.  
  215. Controls = new JPanel();
  216. Controls.setLayout(new FlowLayout());
  217.  
  218. FlowLayout RocketControl = new FlowLayout();
  219. JButton FireworksLaunch = new JButton("Fireworks");
  220. FireworksLaunch.setPreferredSize(new Dimension(400,50));
  221. controls.add(FireworksLaunch);
  222. JButton SpinnersLaunch = new JButton("Spinners");
  223. SpinnersLaunch.setPreferredSize(new Dimension(400,50));
  224. controls.add(SpinnersLaunch);
  225. JButton RocketsLaunch = new JButton("Rockets");
  226. RocketsLaunch.setPreferredSize(new Dimension(400,50));
  227. controls.add(RocketsLaunch);
  228.  
  229. /*ButtonGroup controls = new ButtonGroup();
  230.   controls.add(FireworksLaunch);
  231.   controls.add(SpinnersLaunch);
  232.   controls.add(RocketsLaunch);
  233.   */
  234. //add(FireworksLaunch, BorderLayout.SOUTH);
  235.  
  236. addKeyListener(new KeyAdapter()
  237. {
  238. //public void keyReleased(KeyEvent event)
  239. public void keyPressed(KeyEvent event)
  240. {
  241. switch(event.getKeyCode())
  242. {
  243. case KeyEvent.VK_LEFT:
  244. {
  245. System.out.println("FireworksShow fireworks code hit.");
  246. if(!fireworks.isLaunched() && !fireworks.isVisible())
  247. {
  248. fireworks.setSpriteH(15);
  249. fireworks.setSpriteW(5);
  250. fireworks.setLocx(250);
  251. fireworks.setLocy(600);
  252. fireworks.setActive(true);
  253. fireworks.setVisible(true);
  254. fireworks.setLaunched(true);
  255. fireworks.setVel(3, -15);
  256. GameScore += 5;
  257. }
  258. }
  259. break;
  260.  
  261. case KeyEvent.VK_UP:
  262. {
  263. System.out.println("FireworksShow spinner code hit.");
  264. if(!spinner.isLaunched() && !spinner.isVisible())
  265. {
  266. System.out.println("Rendering spinner.");
  267. spinner.setSpriteH(15);
  268. spinner.setSpriteW(15);
  269. spinner.setLocx(250);
  270. spinner.setLocy(600);
  271. spinner.setActive(true);
  272. spinner.setVisible(true);
  273. spinner.setLaunched(true);
  274. spinner.setVel(-3, -15);
  275. GameScore += 10;
  276. }
  277. break;
  278. }
  279.  
  280. case KeyEvent.VK_RIGHT:
  281. {
  282. System.out.println("FireworksShow rocket code hit.");
  283. if(!rocket.isLaunched() && !rocket.isVisible())
  284. {
  285. rocket.setSpriteH(15);
  286. rocket.setSpriteW(5);
  287. rocket.setLocx(250);
  288. rocket.setLocy(600);
  289. rocket.setActive(true);
  290. rocket.setVisible(true);
  291. rocket.setLaunched(true);
  292. rocket.setVel(10, -15);
  293. GameScore += 15;
  294. }
  295. break;
  296. }
  297. }
  298. }
  299. });
  300. }
  301.  
  302. public void initBurst(Graphics2D g2d, int i)
  303. {
  304. burst = new BufferedImage[3];
  305. try
  306. {
  307. burst[0] = ImageIO.read(getClass().getResource("images\\fwexplosion1.gif"));
  308. burst[1] = ImageIO.read(getClass().getResource("images\\rocketexplosion1.gif"));
  309. burst[2] = ImageIO.read(getClass().getResource("images\\spinnerexplosion1.gif"));
  310. }
  311. catch (Exception ex){System.out.println("Invalid Image");}
  312. switch(i)
  313. {
  314. case 1:
  315. {
  316. try
  317. {
  318. g2d.drawImage((BufferedImage)burst[0], 150, 50, null);
  319. animation.sleep(0);
  320. }
  321. catch(InterruptedException x){}
  322. }
  323. break;
  324.  
  325. case 2:
  326. {
  327. try
  328. {
  329. g2d.drawImage((BufferedImage)burst[1], 150, 50, null);
  330. animation.sleep(0);
  331. }
  332. catch(InterruptedException x){}
  333. }
  334. break;
  335.  
  336. case 3:
  337. {
  338. try
  339. {
  340. g2d.drawImage((BufferedImage)burst[2], 150, 50, null);
  341. animation.sleep(0);
  342. }
  343. catch(InterruptedException x){}
  344. }
  345. break;
  346. }
  347.  
  348. try
  349. {
  350. g2d.drawImage((BufferedImage)burst[i], 150, 50, null);
  351. animation.sleep(0);
  352. }
  353. catch(InterruptedException x){}
  354. }
  355.  
  356.  
  357. /** This method is called from within the constructor to
  358.   * initialize the form.
  359.   * WARNING: Do NOT modify this code. The content of this method is
  360.   * always regenerated by the Form Editor.
  361.   */
  362. // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
  363. private void initComponents() {
  364. jButton1 = new javax.swing.JButton();
  365. jButton2 = new javax.swing.JButton();
  366. jButton3 = new javax.swing.JButton();
  367.  
  368. jButton1.setLabel("firework");
  369. jButton1.addActionListener(new java.awt.event.ActionListener() {
  370. public void actionPerformed(java.awt.event.ActionEvent evt) {
  371. jButton1ActionPerformed(evt);
  372. }
  373. });
  374.  
  375. jButton2.setText("spinner");
  376. jButton2.addActionListener(new java.awt.event.ActionListener() {
  377. public void actionPerformed(java.awt.event.ActionEvent evt) {
  378. jButton2ActionPerformed(evt);
  379. }
  380. });
  381.  
  382. jButton3.setText("rocket");
  383. jButton3.addActionListener(new java.awt.event.ActionListener() {
  384. public void actionPerformed(java.awt.event.ActionEvent evt) {
  385. jButton3ActionPerformed(evt);
  386. }
  387. });
  388.  
  389. org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
  390. this.setLayout(layout);
  391. layout.setHorizontalGroup(
  392. layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  393. .add(layout.createSequentialGroup()
  394. .add(126, 126, 126)
  395. .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
  396. .add(jButton1)
  397. .add(jButton2))
  398. .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
  399. .add(jButton3)
  400. .addContainerGap(134, Short.MAX_VALUE))
  401. );
  402. layout.setVerticalGroup(
  403. layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  404. .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
  405. .add(67, 67, 67)
  406. .add(jButton1)
  407. .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 176, Short.MAX_VALUE)
  408. .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
  409. .add(jButton2)
  410. .add(jButton3))
  411. .addContainerGap())
  412. );
  413. }// </editor-fold>
  414.  
  415. private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
  416. // TODO add your handling code here:
  417. }
  418.  
  419. private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
  420. // TODO add your handling code here:
  421. }
  422.  
  423. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  424. System.out.println("Button 1 Pressed.");
  425. }
  426.  
  427.  
  428.  
  429. // Variables declaration - do not modify
  430. private javax.swing.JButton jButton1;
  431. private javax.swing.JButton jButton2;
  432. private javax.swing.JButton jButton3;
  433. // End of variables declaration
  434.  
  435.  
  436.  
  437. }
  438.  
  439.  
  440. /*
  441. class ButtonBean
  442. {
  443.   PropertyChangeSupport pChange;
  444.   String valueOK;
  445.  
  446.   public ButtonBean()
  447.   {
  448.   pChange = new PropertyChangeSupport(this);
  449.   }
  450.  
  451.   public void setValueOK()
  452.   {
  453.   String oldValue = "ok";
  454.   String newValue = "It is OK";
  455.   pChange.firePropertyChange("text", oldValue, newValue);
  456.   }
  457.  
  458.   public void addPropertyChangeListener(PropertyChangeListener listener)
  459.   {
  460.   pChange.addPropertyChangeListener(listener);
  461.   }
  462. }*/
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
CaffeineCoder is offline Offline
54 posts
since Feb 2008
Apr 7th, 2008
0

Re: Button Question/Issue

Thanks for your help. This is resolved; will now play with getting the right size for each of the buttons and then add the listeners.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
CaffeineCoder is offline Offline
54 posts
since Feb 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: How do I add data to a Jtable
Next Thread in Java Forum Timeline: How the process of sending message of confirmation will be done ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC