| | |
Button Question/Issue
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 39
Reputation:
Solved Threads: 0
•
•
•
•
Maybe something along these lines would work for youjava Syntax (Toggle Plain Text)
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import javax.swing.*; public class FrameExample extends JFrame { JPanel panNorth; JPanel panSouth; JPanel panCenter; public FrameExample() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); // set up top panel panNorth = new JPanel(); panNorth.setLayout(new FlowLayout()); panNorth.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); JLabel lblUpper = new JLabel("Upper Panel"); panNorth.add(lblUpper); add(panNorth, BorderLayout.NORTH); // set up center panel panCenter = new JPanel(); panCenter.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); panCenter.setLayout(new BorderLayout()); JLabel lblMiddle = new JLabel("Middle thing"); lblMiddle.setHorizontalAlignment(JLabel.CENTER); panCenter.add(lblMiddle, BorderLayout.CENTER); add(panCenter); // set up bottom panel panSouth = new JPanel(); panSouth.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); panSouth.setLayout(new FlowLayout()); JButton btnFirst = new JButton("Button 1"); // if preferred size is not set then the button will size to fit the text on it btnFirst.setPreferredSize(new Dimension(100,18)); panSouth.add(btnFirst); JButton btnSecond = new JButton("Button 2"); btnSecond.setPreferredSize(new Dimension(100,18)); panSouth.add(btnSecond); add(panSouth, BorderLayout.SOUTH); setSize(300, 300); setVisible(true); } public static void main(String args[]) { new FrameExample(); } }
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.
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.
•
•
Join Date: Feb 2008
Posts: 39
Reputation:
Solved Threads: 0
•
•
•
•
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)
package Fireworks; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import javax.imageio.ImageIO; import java.util.*; import java.applet.*; import javax.swing.*; import javax.swing.event.*; import java.io.*; import java.net.*; import javax.sound.midi.*; import javax.sound.sampled.*; import java.applet.AudioClip; import javax.swing.JButton; import javax.imageio.*; import java.awt.Dimension; import java.beans.*; public class FireworksPanel extends AbsFireworksPanel { Fireworks Game; FireworksSprite fireworks; FireworksSpinnerSprite spinner; FireworksRocketSprite rocket; private BufferedImage burst[]; int GameScore; String explosion1 = "audio\\explosion1.wav"; String spinner1 = "audio\\spinnerexplosion1.wav"; String spinner2 = "audio\\fizzle.wav"; String rocket1 = "audio\\rocketexplosion1.wav"; Thread animation; Graphics2D dbg; private Image image; private int mx,my; JButton FireworksLaunch; JButton SpinnersLaunch; JButton RocketsLaunch; JPanel controls; public FireworksPanel() { //initComponents(); GameScore = 0; /*setLayout(new BorderLayout()); add(controls, BorderLayout.SOUTH);*/ setPreferredSize(new Dimension(Constants.WIDTH, Constants.HEIGHT)); initFireworks(); Thread animation = new Thread (this); animation.start (); //callEndGame(dbg); /* JPanel fwDisplayPanel = new JPanel(new FlowLayout()); JLabel scoreBox = new JLabel("Current Score"); fwDisplayPanel.setBackground(Color.BLACK); Dimension p1dimension = new Dimension(); p1dimension.setSize(900, 500); fwDisplayPanel.setSize(p1dimension); fwDisplayPanel.setBorder(BorderFactory.createEtchedBorder()); */ } public void paint(Graphics g) { g.setColor(Color.black); g.fillRect(0,0,mx+900,my+900); } public void setGame(Fireworks Game) { this.Game = Game; } public Fireworks getGame() { return Game; } public void initFireworks() { fireworks = new FireworksSprite(); spinner = new FireworksSpinnerSprite(); rocket = new FireworksRocketSprite(); System.out.println("Initializing fireworks."); fireworks = new FireworksSprite(); fireworks.setActive(false); fireworks.setVisible(false); System.out.println("Initializing spinner."); spinner = new FireworksSpinnerSprite(); spinner.setActive(false); spinner.setVisible(false); System.out.println("Initializing rocket."); rocket = new FireworksRocketSprite(); rocket.setActive(false); rocket.setVisible(false); } public void renderSprites(Graphics2D g2d) { DisplayGameScore(GameScore, g2d); fireworks.paintSprite(g2d); spinner.paintSprite(g2d); rocket.paintSprite(g2d); if(fireworks.getLocy()<55 && fireworks.getLocy()> 0) { System.out.println("firework burst and sound."); soundeffects(rocket1); //explosion1); initBurst(g2d, 0); } if(rocket.getLocy()<55 && rocket.getLocy()> 0) { System.out.println("rocket burst and sound."); soundeffects(rocket1); initBurst(g2d, 1); } if(spinner.getLocy()<55 && spinner.getLocy()> 0) { System.out.println("spinner burst and sound."); soundeffects(spinner1); initBurst(g2d, 2); soundeffects(spinner2); } FireworksShow(); } public void updateGame() { fireworks.updateSprite(); spinner.updateSprite(); rocket.updateSprite(); } public void callEndGame(Graphics2D dbg) { if(gameOver) { Font font = new Font("Times Roman", Font.BOLD, 50); dbg.setFont(font); dbg.setColor(Color.WHITE); String GameMsg = "Game Over"; dbg.drawString(GameMsg, 150, 150); dbg.setFont(new Font("Times New Roman", Font.BOLD, 50)); dbg.setColor(Color.YELLOW); dbg.drawString("Fireworks Display", 100, 100); dbg.drawString("Final Score:", 200, 200); dbg.setFont(new Font("Times New Roman", Font.BOLD, 15)); dbg.drawString("Fireworks Display Program.", Constants.WIDTH/2 + 20, Constants.HEIGHT/2 + 40); dbg.drawString("Final Project, CS8680, Dr. Xu, KSU MSACS", Constants.WIDTH/2 + 20, Constants.HEIGHT/2 + 60); dbg.drawString("Nathan Williams", Constants.WIDTH/2 + 20, Constants.HEIGHT/2 + 80); dbg.drawString("Fall 2007.", Constants.WIDTH/2 + 20, Constants.HEIGHT/2 + 100); } } public void SoundTrack() { //Initialize background music. /*bgsong = new SongSprite(); bgsong.setFilename(Constants.SONG); //bgSound[0] = bgsong; System.out.println("Now Playing Song"); //bgsong.loadClip(); bgsong.play();*/ } public void soundeffects(String filename) { //Initialize audio sound effects. try { AudioClip FireworkSound = Applet.newAudioClip(getClass().getResource(filename)); FireworkSound.play(); } catch(Exception e) { System.out.println("Problem with " + filename); } } private void DisplayGameScore(int theGameScore, Graphics2D dbg) { dbg.setFont(new Font("Times New Roman", Font.BOLD, 30)); dbg.setColor(Color.YELLOW); dbg.drawString("Current Score is: " + theGameScore, 150,30); } public void FireworksShow() { JPanel GameDisplay; JPanel Controls; // setLayout(new BorderLayout()); GameDisplay = new JPanel(); Controls = new JPanel(); Controls.setLayout(new FlowLayout()); FlowLayout RocketControl = new FlowLayout(); JButton FireworksLaunch = new JButton("Fireworks"); FireworksLaunch.setPreferredSize(new Dimension(400,50)); controls.add(FireworksLaunch); JButton SpinnersLaunch = new JButton("Spinners"); SpinnersLaunch.setPreferredSize(new Dimension(400,50)); controls.add(SpinnersLaunch); JButton RocketsLaunch = new JButton("Rockets"); RocketsLaunch.setPreferredSize(new Dimension(400,50)); controls.add(RocketsLaunch); /*ButtonGroup controls = new ButtonGroup(); controls.add(FireworksLaunch); controls.add(SpinnersLaunch); controls.add(RocketsLaunch); */ //add(FireworksLaunch, BorderLayout.SOUTH); addKeyListener(new KeyAdapter() { //public void keyReleased(KeyEvent event) public void keyPressed(KeyEvent event) { switch(event.getKeyCode()) { case KeyEvent.VK_LEFT: { System.out.println("FireworksShow fireworks code hit."); if(!fireworks.isLaunched() && !fireworks.isVisible()) { fireworks.setSpriteH(15); fireworks.setSpriteW(5); fireworks.setLocx(250); fireworks.setLocy(600); fireworks.setActive(true); fireworks.setVisible(true); fireworks.setLaunched(true); fireworks.setVel(3, -15); GameScore += 5; } } break; case KeyEvent.VK_UP: { System.out.println("FireworksShow spinner code hit."); if(!spinner.isLaunched() && !spinner.isVisible()) { System.out.println("Rendering spinner."); spinner.setSpriteH(15); spinner.setSpriteW(15); spinner.setLocx(250); spinner.setLocy(600); spinner.setActive(true); spinner.setVisible(true); spinner.setLaunched(true); spinner.setVel(-3, -15); GameScore += 10; } break; } case KeyEvent.VK_RIGHT: { System.out.println("FireworksShow rocket code hit."); if(!rocket.isLaunched() && !rocket.isVisible()) { rocket.setSpriteH(15); rocket.setSpriteW(5); rocket.setLocx(250); rocket.setLocy(600); rocket.setActive(true); rocket.setVisible(true); rocket.setLaunched(true); rocket.setVel(10, -15); GameScore += 15; } break; } } } }); } public void initBurst(Graphics2D g2d, int i) { burst = new BufferedImage[3]; try { burst[0] = ImageIO.read(getClass().getResource("images\\fwexplosion1.gif")); burst[1] = ImageIO.read(getClass().getResource("images\\rocketexplosion1.gif")); burst[2] = ImageIO.read(getClass().getResource("images\\spinnerexplosion1.gif")); } catch (Exception ex){System.out.println("Invalid Image");} switch(i) { case 1: { try { g2d.drawImage((BufferedImage)burst[0], 150, 50, null); animation.sleep(0); } catch(InterruptedException x){} } break; case 2: { try { g2d.drawImage((BufferedImage)burst[1], 150, 50, null); animation.sleep(0); } catch(InterruptedException x){} } break; case 3: { try { g2d.drawImage((BufferedImage)burst[2], 150, 50, null); animation.sleep(0); } catch(InterruptedException x){} } break; } try { g2d.drawImage((BufferedImage)burst[i], 150, 50, null); animation.sleep(0); } catch(InterruptedException x){} } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ // <editor-fold defaultstate="collapsed" desc=" Generated Code "> private void initComponents() { jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); jButton1.setLabel("firework"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setText("spinner"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jButton3.setText("rocket"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .add(126, 126, 126) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) .add(jButton1) .add(jButton2)) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(jButton3) .addContainerGap(134, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() .add(67, 67, 67) .add(jButton1) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 176, Short.MAX_VALUE) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(jButton2) .add(jButton3)) .addContainerGap()) ); }// </editor-fold> private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { System.out.println("Button 1 Pressed."); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; // End of variables declaration } /* class ButtonBean { PropertyChangeSupport pChange; String valueOK; public ButtonBean() { pChange = new PropertyChangeSupport(this); } public void setValueOK() { String oldValue = "ok"; String newValue = "It is OK"; pChange.firePropertyChange("text", oldValue, newValue); } public void addPropertyChangeListener(PropertyChangeListener listener) { pChange.addPropertyChangeListener(listener); } }*/
![]() |
Similar Threads
- Slow localhost (Networking Hardware Configuration)
- New Thread button hard to find? (DaniWeb Community Feedback)
- radio button ASP question?? (ASP)
- radio button ASP question?? (ASP)
- shutdown or powerdown issue for medion PC (Windows NT / 2000 / XP)
Other Threads in the Java Forum
- Previous Thread: How do I add data to a Jtable
- Next Thread: How the process of sending message of confirmation will be done ?
| Thread Tools | Search this Thread |
actuate android api applet application applications array arrays automation balls bank binary bluetooth business chat class classes clear client code codesnippet collections component coordinates database defaultmethod development dice dragging ebook eclipse educational error exception formatingtextintooltipjava fractal game givemetehcodez graphics gui hql html ide image infinite ingres input integer invokingapacheantprogrammatically j2me java javaprojects jni jpanel jtextarea julia linux list loop looping map method methods mobile mysql netbeans newbie openjavafx parameter php print problem program programming project recursion repositories scanner screen scrollbar server set size sms sort sorting sql sqlserver state storm string sun superclass swing swt text-file threads tree websites windows






