Results 1 to 9 of 9
Thread: Problem with repaint();
- 01-06-2010, 02:22 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 26
- Rep Power
- 0
Problem with repaint();
I cannot get the paint method to run at all.
Any help at all, even if its just to clean up my code would be appreciated.Java Code:package horschkinshimerdoodle; import javax.swing.*; import javax.imageio.*; import java.io.*; import java.awt.image.*; import java.awt.event.*; import java.awt.*; public class StatxMenu extends Canvas{ //Variable Declaration int nAtt = 5, nSP = 5, nDef = 5, nHP = 50, nMP = 25; public void paint(Graphics g){ System.out.println("Hiya"); g.setColor(Color.red); g.drawLine(300, 450, 300 + 300, nHP); } public StatxMenu() throws IOException{ //Images BufferedImage biSkills = ImageIO.read(new File("Skills.png")); BufferedImage biSystem = ImageIO.read(new File("System.png")); BufferedImage biClose = ImageIO.read(new File("Close.png")); //Labels, Buttons and a Panel... final JFrame Menu = new JFrame(); final Canvas CImage = new Canvas(); final JLabel LMP = new JLabel(); final JLabel LSP = new JLabel(); final JLabel LHP = new JLabel(); final JLabel LAttack = new JLabel(); final JLabel LDef = new JLabel(); final JButton BSystem = new JButton(); final JButton BClose = new JButton(); final JButton BSkills = new JButton(); final JButton BStats = new JButton(); final JButton BHP = new JButton(); final JButton BMP = new JButton(); final JButton BAttack = new JButton(); final JButton BDef = new JButton(); JButton INoHere = new JButton(); //Frame Menu.setSize(400, 600); Menu.add(CImage); Menu.add(BSystem); Menu.add(BClose); Menu.add(BSkills); Menu.add(BStats); Menu.add(BAttack); Menu.add(LAttack); Menu.add(LHP); Menu.add(BHP); Menu.add(LMP); Menu.add(BMP); Menu.add(LDef); Menu.add(BDef); Menu.add(LSP); Menu.add(INoHere); //Skill Components //Image CImage.setBounds(200, 300, 200, 300); CImage.setVisible(false); //SP LSP.setText("SP: " + nSP); LSP.setBounds(200, 15, 100, 20); //HP //Button BHP.setText("+"); BHP.setBounds(305, 70, 41, 20); BHP.setVisible(false); BHP.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ if (nSP > 0){ nSP--; nHP += 10; LSP.setText("SP: " + nSP); LHP.setText("HP: " + nHP); Hiya(); } } }); //Label LHP.setText("HP: " + nHP); LHP.setBounds(200, 70, 100, 20); //MP //Button BMP.setText("+"); BMP.setBounds(305, 100, 41, 20); BMP.setVisible(false); BMP.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ if (nSP > 0){ nSP--; nMP += 5; LSP.setText("SP: " + nSP); LMP.setText("MP: " + nMP); repaint(); } } }); //Label LMP.setText("MP: " + nMP); LMP.setBounds(200, 100, 100, 20); //Attack //Button BAttack.setText("+"); BAttack.setBounds(305, 130, 41, 20); BAttack.setVisible(false); BAttack.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ if (nSP > 0){ nSP--; nAtt++; LSP.setText("SP: " + nSP); LAttack.setText("Attack: " + nAtt); repaint(); } } }); //Label LAttack.setText("Attack: " + nAtt); LAttack.setBounds(200, 130, 100, 20); //Defence //Button BDef.setText("+"); BDef.setBounds(305, 160, 41, 20); BDef.setVisible(false); BDef.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ if (nSP > 0){ nSP--; nDef++; LSP.setText("SP: " + nSP); LDef.setText("Defence: " + nDef); repaint(); } } }); //Label LDef.setText("Defence: " + nDef); LDef.setBounds(200, 160, 100, 20); //System Button BSystem.setIcon(new ImageIcon(biSystem)); BSystem.setBounds(20, 465, biSystem.getWidth(), biSystem.getHeight()); BSystem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ BClose.setVisible(true); BAttack.setVisible(false); BDef.setVisible(false); BHP.setVisible(false); BMP.setVisible(false); } }); //Close Button BClose.setIcon(new ImageIcon(biClose)); BClose.setBounds(200, 465, biClose.getWidth(), biClose.getHeight()); BClose.setVisible(false); BClose.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ Menu.setVisible(false); } }); //Skills Button BSkills.setIcon(new ImageIcon(biSkills)); BSkills.setBounds(20, 235, biSkills.getWidth(), biSkills.getHeight()); BSkills.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ BClose.setVisible(false); BAttack.setVisible(false); BDef.setVisible(false); BHP.setVisible(false); BMP.setVisible(false); } }); //Stats Button BStats.setText("Stats"); BStats.setBounds(20, 20, biSkills.getWidth(), biSkills.getHeight()); BStats.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ BClose.setVisible(false); BAttack.setVisible(true); BDef.setVisible(true); BHP.setVisible(true); BMP.setVisible(true); } }); //Intangibles INoHere.setVisible(false); Menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Menu.setVisible(true); } public void Hiya(){ System.out.println("Hiya"); repaint(); } }
Thanks
Jamie
- 01-06-2010, 04:17 PM #2
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 282
- Rep Power
- 4
The code lacks a main() method, has no call on the constructor for StatxMenu, and does not have a Frame to create a window on screen. Any one of these will cause paint to fail.
See my sample program.
- 01-06-2010, 04:37 PM #3
Member
- Join Date
- Oct 2009
- Posts
- 26
- Rep Power
- 0
This program is called by another and there is a frame.
Java Code:final JFrame Menu = new JFrame();
- 01-06-2010, 04:38 PM #4
Member
- Join Date
- Oct 2009
- Posts
- 26
- Rep Power
- 0
The GUI runs fine, but I just cannot get the paint method to do anything.
- 01-06-2010, 05:04 PM #5
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 282
- Rep Power
- 4
Nothing in the code you have shown us will cause paint() to be called.
Try building a small test application along the lines of my sample program.
And for Pete's sake, rename your variables to follow Java conventions.
- 01-06-2010, 05:29 PM #6
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 282
- Rep Power
- 4
Do not declare the Frame inside a Canvas object.
It is well to always code as though two or more instances of a Canvas
will appear in one application. This will force you to distinguish
between Frame code for a whole window and the code for a particular screen area.
- 01-06-2010, 05:35 PM #7
Member
- Join Date
- Oct 2009
- Posts
- 26
- Rep Power
- 0
The canvas doesn't affect anything except that it allows there to be a paint() method. Without extending a frame, form, panel, etc. the paint() method won't work, but you should already know that. Also, all my variables are conventionally named. The other Canvas declaration is because I had previously added methods to it, but otherwise has no effect on the code. So, could you provide feedback that would actually affect my code?
-
So since Canvas is not being displayed anywhere, of course paint won't be called and shouldn't be called.
and without displaying the component that has the paint or paintComponent override, it won't work either.Without extending a frame, form, panel, etc. the paint() method won't work, but you should already know that.
what???Also, all my variables are conventionally named. The other Canvas declaration is because I had previously added methods to it, but otherwise has no effect on the code. So, could you provide feedback that would actually affect my code?
My advice:
1) Don't deal with Canvas here. You've got a Swing app and Canvas and other AWT components shouldn't be mixed with Swing Components.
2) Extend one of the JPanels that is displayed in your program, one that will have a region that requires the painting your desire. If you don't have any JPanels, then add one to your program and use it as the JFrame's contentPane.
3) Extend this JPanel's paintComponent method and do your drawing in there.
4) You are asking for free help from Java programmers and are using non-standard variable names. This makes it difficult for others to interpret your code and give you the help you seek. If you follow the advice given to you to change these variable names you will help make it easier for us to help you now and in the future, which is a good thing. The last thing you should be doing is complaining about this advice as you would appear to be biting the hand that feeds you. Rather you should be thanking that person. Just my 2 cents.Last edited by Fubarable; 01-06-2010 at 08:39 PM.
- 01-07-2010, 12:33 AM #9
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 282
- Rep Power
- 4
Similar Threads
-
Repaint problem
By citizenXL in forum New To JavaReplies: 4Last Post: 10-28-2009, 03:02 PM -
repaint problem
By amith in forum Java 2DReplies: 2Last Post: 07-01-2008, 12:10 AM -
Problem in repaint
By Preethi in forum AWT / SwingReplies: 16Last Post: 03-18-2008, 08:10 PM -
Repaint problem
By swimberl in forum Java 2DReplies: 1Last Post: 02-16-2008, 09:12 PM -
Repaint problem
By swimberl in forum Java 2DReplies: 0Last Post: 01-06-2008, 03:28 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks