Results 1 to 9 of 9
  1. #1
    dunafrothint is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default Problem with repaint();

    I cannot get the paint method to run at all.

    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();
        }
    }
    Any help at all, even if its just to clean up my code would be appreciated.

    Thanks

    Jamie

  2. #2
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    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.

  3. #3
    dunafrothint is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    This program is called by another and there is a frame.
    Java Code:
    final JFrame Menu = new JFrame();

  4. #4
    dunafrothint is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    The GUI runs fine, but I just cannot get the paint method to do anything.

  5. #5
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    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.

  6. #6
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    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.

  7. #7
    dunafrothint is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    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?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by dunafrothint View Post
    The canvas doesn't affect anything except that it allows there to be a paint() method.
    So since Canvas is not being displayed anywhere, of course paint won't be called and shouldn't be called.

    Without extending a frame, form, panel, etc. the paint() method won't work, but you should already know that.
    and without displaying the component that has the paint or paintComponent override, it won't work either.

    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?
    what???

    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 09:39 PM.

  9. #9
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    That my silence not be misunderstood:

    How often should I respond in the future to one who cavalierly rejects past advice?

Similar Threads

  1. Repaint problem
    By citizenXL in forum New To Java
    Replies: 4
    Last Post: 10-28-2009, 04:02 PM
  2. repaint problem
    By amith in forum Java 2D
    Replies: 2
    Last Post: 07-01-2008, 01:10 AM
  3. Problem in repaint
    By Preethi in forum AWT / Swing
    Replies: 16
    Last Post: 03-18-2008, 09:10 PM
  4. Repaint problem
    By swimberl in forum Java 2D
    Replies: 1
    Last Post: 02-16-2008, 10:12 PM
  5. Repaint problem
    By swimberl in forum Java 2D
    Replies: 0
    Last Post: 01-06-2008, 04:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •