Results 1 to 6 of 6
Thread: JPanel PaintComponent
- 10-30-2010, 09:07 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 2
- Rep Power
- 0
JPanel PaintComponent
I am trying to animate a dice roll image, but it is not working. here is my code:
Java Code:public class DiePanel extends javax.swing.JPanel { private int _LeftDie = 1; private int _RightDie = 1; private Timer timer; private Image LeftImage; private Image RightImage; private ImageIcon icon; /** Creates new form DiePanel */ public DiePanel() { initComponents(); setPlayerPiece(); icon = new ImageIcon(this.getClass().getResource("/img/car.jpg")); pieceLabel.setIcon(icon); this.pieceLabel.setPreferredSize(new Dimension(icon.getIconWidth(), icon.getIconHeight())); this.LeftPanel.setPreferredSize(new Dimension(60, 60)); this.RightPanel.setPreferredSize(new Dimension(60, 60)); } private void roll() { if (timer != null) return; timer = new Timer(100, new ActionListener() { int frames = 1; public void actionPerformed(ActionEvent evt) { _LeftDie = (int)(Math.random()*6) + 1; _RightDie = (int)(Math.random()*6) + 1; SetLeftDieImage(_LeftDie); SetRightDieImage(_RightDie); repaint(); frames++; if (frames == 10) { timer.stop(); timer = null; } } }); timer.start(); } @Override public void paintComponent(Graphics g) { super.paintComponents(g); if (LeftImage != null && RightImage != null) { g.drawImage(LeftImage, 0, 0, this.LeftPanel); g.drawImage(RightImage, 0, 0, this.RightPanel); } } private void setPlayerPiece(){ if(Players.CheckNumberOfPlayer() > 0){ PlayersPiece currentPlayerPiece = PlayersPiece.valueOf(Players.getPiece().get(0)); switch(currentPlayerPiece){ case car: icon = new ImageIcon(this.getClass().getResource("/img/car.jpg")); pieceLabel.setIcon(icon); break; case cone: icon = new ImageIcon(this.getClass().getResource("/img/cone.jpg")); pieceLabel.setIcon(icon); break; case hat: icon = new ImageIcon(this.getClass().getResource("/img/hat.jpg")); pieceLabel.setIcon(icon); break; case iron: icon = new ImageIcon(this.getClass().getResource("/img/iron.jpg")); pieceLabel.setIcon(icon); break; case ship: icon = new ImageIcon(this.getClass().getResource("/img/ship.jpg")); pieceLabel.setIcon(icon); break; case shoe: icon = new ImageIcon(this.getClass().getResource("/img/shoe.jpg")); pieceLabel.setIcon(icon); break; } } } private void SetLeftDieImage(int value){ switch(value) { case 1: icon = new ImageIcon(this.getClass().getResource("/img/stone1.gif")); LeftImage = icon.getImage(); break; case 2: icon = new ImageIcon(this.getClass().getResource("/img/stone2.gif")); LeftImage = icon.getImage(); break; case 3: icon = new ImageIcon(this.getClass().getResource("/img/stone3.gif")); LeftImage = icon.getImage(); break; case 4: icon = new ImageIcon(this.getClass().getResource("/img/stone4.gif")); LeftImage = icon.getImage(); break; case 5: icon = new ImageIcon(this.getClass().getResource("/img/stone5.gif")); LeftImage = icon.getImage(); break; case 6: icon = new ImageIcon(this.getClass().getResource("/img/stone6.gif")); LeftImage = icon.getImage(); break; } } private void SetRightDieImage(int value){ switch(value) { case 1: icon = new ImageIcon(this.getClass().getResource("/img/stone1.gif")); RightImage = icon.getImage(); break; case 2: icon = new ImageIcon(this.getClass().getResource("/img/stone2.gif")); RightImage = icon.getImage(); break; case 3: icon = new ImageIcon(this.getClass().getResource("/img/stone3.gif")); RightImage = icon.getImage(); break; case 4: icon = new ImageIcon(this.getClass().getResource("/img/stone4.gif")); RightImage = icon.getImage(); break; case 5: icon = new ImageIcon(this.getClass().getResource("/img/stone5.gif")); RightImage = icon.getImage(); break; case 6: icon = new ImageIcon(this.getClass().getResource("/img/stone6.gif")); RightImage = icon.getImage(); break; } } /** 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. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { LeftPanel = new javax.swing.JPanel(); RightPanel = new javax.swing.JPanel(); pieceLabel = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)); LeftPanel.setMaximumSize(new java.awt.Dimension(60, 60)); LeftPanel.setMinimumSize(new java.awt.Dimension(60, 60)); LeftPanel.setPreferredSize(new java.awt.Dimension(60, 60)); javax.swing.GroupLayout LeftPanelLayout = new javax.swing.GroupLayout(LeftPanel); LeftPanel.setLayout(LeftPanelLayout); LeftPanelLayout.setHorizontalGroup( LeftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 55, Short.MAX_VALUE) ); LeftPanelLayout.setVerticalGroup( LeftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 55, Short.MAX_VALUE) ); RightPanel.setMaximumSize(new java.awt.Dimension(60, 60)); RightPanel.setMinimumSize(new java.awt.Dimension(60, 60)); RightPanel.setPreferredSize(new java.awt.Dimension(60, 60)); javax.swing.GroupLayout RightPanelLayout = new javax.swing.GroupLayout(RightPanel); RightPanel.setLayout(RightPanelLayout); RightPanelLayout.setHorizontalGroup( RightPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 55, Short.MAX_VALUE) ); RightPanelLayout.setVerticalGroup( RightPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 60, Short.MAX_VALUE) ); pieceLabel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED)); pieceLabel.setMaximumSize(new java.awt.Dimension(19, 18)); pieceLabel.setMinimumSize(new java.awt.Dimension(19, 18)); pieceLabel.setPreferredSize(new java.awt.Dimension(19, 18)); jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/roll.jpg"))); // NOI18N jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(pieceLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addComponent(LeftPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 53, Short.MAX_VALUE) .addComponent(RightPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(LeftPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(RightPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(layout.createSequentialGroup() .addGap(34, 34, 34) .addComponent(pieceLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(28, 28, 28)))) ); }// </editor-fold> private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { roll(); } // Variables declaration - do not modify private javax.swing.JPanel LeftPanel; private javax.swing.JPanel RightPanel; private javax.swing.JButton jButton1; private javax.swing.JLabel pieceLabel; // End of variables declaration }
-
-
Also, your code above will not compile nor run for us as their are several dependencies that we don't have access to including the PlayersPiece and Players classes, images, and likely more. You would be best served to create an SSCCE that tries to demonstrate your problem. Please see the link in my signature for more on this.
-
Also, your paintComponent method will draw in the component it was called. So your paintComponent call will not have an effect on LeftPanel or RightPanel since it is located in DiePanel. The ImageObserver doesn't work like you think it does.
Also, in the paintComponent method you call super.paintComponents. That terminal s is not supposed to be there and will completely mess up this method call.
Myself, I'd display my dice in a JLabel and just swap out its ImageIcon if I want to display a different image.
- 10-30-2010, 10:47 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 2
- Rep Power
- 0
thanks Fubarable,
I separated the panels and it works fine now.
-
You're welcome. Best of luck, and again, welcome!
Similar Threads
-
Trouble with paintComponent()
By ShirlyPunk in forum AWT / SwingReplies: 17Last Post: 03-13-2010, 04:27 PM -
paintComponent is not working
By spazattack in forum New To JavaReplies: 1Last Post: 12-18-2009, 03:48 AM -
paint vs paintComponent for a JPanel
By lightstream in forum AWT / SwingReplies: 4Last Post: 01-29-2009, 02:26 AM -
Working around paintcomponent
By sahhhm in forum New To JavaReplies: 2Last Post: 05-16-2008, 02:43 AM -
Drawing outside paintComponent()
By DarkSide1 in forum Java 2DReplies: 2Last Post: 11-08-2007, 10:36 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks