JApplet paint() not getting called

I have 2 files in my project. The first (slider.java) extends JApplet and the second (MyCanvas.java) extends Canvas.  In slider.java i set up the screen so that the top portion is of type MyCanvas and the bottom is a mixture of JLabels and JButtons. I First add these to a panel, then add the Canvas and the panel to the container.  here is an excerpt of how the frame gets initialized:

public void createGamePanel() {
                Container c;
      c = getContentPane();
      c.setLayout(new BorderLayout());
      
      theBoard = new MyCanvas(this);
      
                pnlGameInfo = new JPanel();
      pnlGameInfo.setLayout(new GridLayout(2,2));
            
      lblMessage = new JLabel("Message:");
      lblNumMoves = new JLabel("Number of Moves:");
      lblInfo = new JLabel("Welcome!");
      lblCurMoves = new JLabel("0");
            
      btnReset = new JButton("Reset");
      btnReset.addActionListener(this);
            
            
      pnlGameInfo.add(lblMessage);
      pnlGameInfo.add(lblInfo);
      pnlGameInfo.add(lblNumMoves);
      pnlGameInfo.add(lblCurMoves);
            
      pnlGameFuncs = new JPanel();
      pnlGameFuncs.setLayout(new BorderLayout());
            
      pnlGameFuncs.add(pnlGameInfo,BorderLayout.NORTH);
      pnlGameFuncs.add(btnReset,BorderLayout.CENTER);
            
      pnlGameFuncs.setBorder(new BevelBorder(BevelBorder.LOWERED));
            
      c.add(theBoard,BorderLayout.CENTER);
      c.add(pnlGameFuncs,BorderLayout.SOUTH);
}

All of the objects are declared gloabally (not particulaly good programming i know...but i'm just starting and this is only for myself ;) ). In MyCanvas.java i have a paint(Graphics g) method.  My question is how do i call this..or why isn't it getting called for me?  I tried making a public void paint() method in slider.java that explicitly called the MyCanvs paint method using theBoard object i created. But when i did that...it painted over my bottom panel as well (the JLabales and buttons). So how do i make it only paint to the portion of the container i put theBoard in (BorderLayout.CENTER)?

This may be a confusing way to pose the question to someone who knows what they're doing...so if you need anything explained further...just let me know ;)

Thanks!

-Matt
LVL 3
mattfairwAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

StillUnAwareCommented:
instead of calling paint(), try validate() or repaint()
mattfairwAuthor Commented:
i tried that...where would i put that though? i tried putting both those lines in both public void start() and public void init() in slider.java.
StillUnAwareCommented:
Can You post all Your code. I'd try to run it.
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

mattfairwAuthor Commented:
sure...might be sloppy though :)
...also you're going to need a 490x500 Image (i haven't gotten to making that more dynamic yet)

=================
ZaroSlider.Java
=================

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.awt.event.*;
//import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.*;

/**
 * @author Matt White
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class ZaroSlider extends JApplet implements ActionListener {
      /*Project Gloabal Variables*/
      private int ROWS;
      private int COLS;
      public int PuzzleWidth = 490;
      public int PuzzleHeight = 500;
      private int TileHeight;
      private int TileWidth;
      MyCanvas theBoard;
      
      //Container c;// = getContentPane();
      
      Graphics big;
      Image image;
      BufferedImage[][] sbi;
      
      JPanel pnlGame;
      JPanel pnlGameInfo;
      JPanel pnlGameFuncs;
      JLabel lblMessage;
      JLabel lblInfo;
      JLabel lblNumMoves;
      JLabel lblCurMoves;
      JButton btnReset;
      JButton[][] btnTiles;
      
      public void init() {
            setRows(5);
            setCols(5);
            
            captureImage();
            //createGamePanel();
            createGameMsgPanel();
            //validate();
            //repaint();
      }
      
      public void start() {
            //theBoard.paint(super.getGraphics());
            //repaint();
            //validate();
            //theBoard.repaint();
      }
      
      public void captureImage() {
            Image image;
            BufferedImage bi = new BufferedImage(this.PuzzleWidth,this.PuzzleHeight,BufferedImage.TYPE_INT_RGB);
            try {
                  //image = ImageIO.read(new FileInputStream("CATFACE.jpg"));
                  bi = ImageIO.read(new FileInputStream("CATFACE.jpg"));
            }
            catch (IOException e) {
                  System.out.println("Error reading file. Message:\n" + e.getMessage());
                  System.exit(0);
            }
            //BufferedImage bi = new BufferedImage(this.PuzzleWidth,this.PuzzleHeight,BufferedImage.TYPE_INT_RGB);
            this.PuzzleHeight = bi.getHeight();
            this.PuzzleWidth = bi.getWidth();
            this.TileHeight = setTileDim(this.ROWS,this.PuzzleHeight);
            this.TileWidth = setTileDim(this.COLS,this.PuzzleWidth);
            
            sbi = new BufferedImage[this.ROWS][this.COLS];
            
            for(int c = 0; c < ROWS; c++)
                  for(int r = 0; r < COLS; r++)
                        sbi[r][c] = bi.getSubimage(r*TileWidth,c*TileHeight,TileWidth,TileHeight);
      }
      
      public void createGamePanel() {
            
            //c.add(theBoard,BorderLayout.NORTH);
      }
      
      public void createGameMsgPanel() {
            Container c;
            c = getContentPane();
            c.setLayout(new BorderLayout());
            
            theBoard = new MyCanvas(this);
      
            pnlGameInfo = new JPanel();
            pnlGameInfo.setLayout(new GridLayout(2,2));
            
            lblMessage = new JLabel("Message:");
            lblNumMoves = new JLabel("Number of Moves:");
            lblInfo = new JLabel("Welcome!");
            lblCurMoves = new JLabel("0");
            
            btnReset = new JButton("Reset");
            btnReset.addActionListener(this);
            
            
            pnlGameInfo.add(lblMessage);
            pnlGameInfo.add(lblInfo);
            pnlGameInfo.add(lblNumMoves);
            pnlGameInfo.add(lblCurMoves);
            
            pnlGameFuncs = new JPanel();
            pnlGameFuncs.setLayout(new BorderLayout());
            
            pnlGameFuncs.add(pnlGameInfo,BorderLayout.NORTH);
            pnlGameFuncs.add(btnReset,BorderLayout.CENTER);
            
            pnlGameFuncs.setBorder(new BevelBorder(BevelBorder.LOWERED));
            
            //c.add(theBoard,BorderLayout.CENTER);
            c.add(pnlGameFuncs,BorderLayout.SOUTH);
      }
      
      public void actionPerformed(ActionEvent e) {
            
      }
      
      
      /*******************************************************************************************/
      
      /*Mutator Methods*/
      private void setRows(int r) {
            this.ROWS = r;
      }
      private void setCols(int c) {
            this.COLS = c;
      }
      private int setTileDim(int d1,int d2) {
            System.out.println("d1: " + d1 + "\td2: " + d2 + "\t\td2/d1: " + (float)d2/d1);
            return (int)(((float)d2) / d1);
      }
      
      /*Accessor Methods*/
      public int getRows() {
            return this.ROWS;
      }
      public int getCols() {
            return this.COLS;
      }
      public int getTileHeight() {
            return this.TileHeight;
      }
      public int getTileWidth() {
            return this.TileWidth;
      }
      public int getPuzzleHeight() {
            return this.PuzzleHeight;
      }
      public int getPuzzleWidth() {
            return this.PuzzleWidth;
      }
      public BufferedImage getTileBI(int r, int c) {
            return this.sbi[r][c];
      }
}




====================
MyCanvas.java
====================
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/*
 * Created on Jun 30, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author Matt White
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class MyCanvas extends Canvas{
      ZaroSlider game;
      
      public MyCanvas(ZaroSlider zs) {
            this.game = zs;
      }
      
      public void paint(Graphics g) {
            int r,c;
            int curPos; /* 1-25 */
            System.out.println("IN PAINT");
            
            for (r = 0; r < game.getRows(); r++)
            {
                  for (c = 0; c < game.getCols(); c++)
                  {
                        g.drawImage(game.getTileBI(r,c),r*game.getTileWidth(),c*game.getTileHeight(),this);
                  }
            }
            g.setColor(Color.BLACK);
            for (r = 1; r <= game.getCols(); r++)
          {
              g.drawLine(r*game.getTileWidth(),0,r*game.getTileWidth(),game.getCols()*game.getTileHeight());
          }
          for (r = 1; r <= game.getRows(); r++)
          {
              g.drawLine(0, r*game.getTileHeight(), game.getRows()*game.getTileWidth(), r*game.getTileHeight());
          }
      }
}
StillUnAwareCommented:
the only thing You need to do is to uncomment line 121 in Your ZaroSlider.java, it reads:

          pnlGameFuncs.add(pnlGameInfo,BorderLayout.NORTH);
          pnlGameFuncs.add(btnReset,BorderLayout.CENTER);
         
          pnlGameFuncs.setBorder(new BevelBorder(BevelBorder.LOWERED));
         
          c.add(theBoard,BorderLayout.CENTER);          <<<<<<<<<<<<<<<<<<<<<<<<<<
          c.add(pnlGameFuncs,BorderLayout.SOUTH);
     }
     
     public void actionPerformed(ActionEvent e) {
         
     }

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mattfairwAuthor Commented:
hmmm. i had that commented out later when i was trying several things, many to no avail. But when i initially put it in it was uncommented. When it was like this, the bottom panel showed fine, bu the top panel was blank.  Did you get it to work by uncommenting that line, or are you just saying that it needs to be uncommented?
override_y2kCommented:
I don't tink you added your canvas to your panel.

Also, if your canvas has 0 height and 0 width, then paint won't get called. It doesn't look like you set your canvas size.

Try the following in the MyCanvas constructor:

setSize(100,100);

or whatever size you wnat.

regards,

o2k

override_y2kCommented:
That worked for me nehoo.

Hope that helps,

o2k
StillUnAwareCommented:
I tried with appletviewer and it worked
StillUnAwareCommented:
that's Zaro.html:

<applet codebase=. code=ZaroSlider width="500" height="700"></applet>

that's .java.policy:

grant {
  permission java.security.AllPermission;
};

and that is the command to run the applet:

appletviewer -J-Djava.security.policy=.java.policy Zaro.html
mattfairwAuthor Commented:
well wonderful! i'll be able to test these after lunch sometime. Thanks for the quick replies!!
mattfairwAuthor Commented:
StillUnAware... could you provide a bit more detail on your other post. I understand the first part..about adding the applet to an html file. But the other two i don't get. What is that for? Do i need to add that to my web so that it can be ran? Thank you.
override_y2kCommented:
I tried doing it without specifying a canvas size and paint() does not get called. You MUST specify a canvas size for paint() to get called.
StillUnAwareCommented:
well, to read image, You'll have to sign Your applet anyway, cause You are accessing local file system. And the thing I did, was more specific for appletviewer.
override_y2kCommented:
appletviewer is a minaiture browser used for testing Applets. His post was ssample parameters for testing with that Appletviewer
override_y2kCommented:
Going back to the question that you asked.

"My question is how do i call this..or why isn't it getting called for me? "

Answer:

1. You never added the canvas to the panel

2. If your canvas has 0 height and 0 width, then paint won't get called. You never set your canvas size.

PAQ
StillUnAwareCommented:
Actually, it (canvas) should strech out, because it is added in BorderLayout.CENTER manner. So setting it's size shouldn't be the matter
override_y2kCommented:
>Actually, it (canvas) should strech out, because it is added in BorderLayout.CENTER manner. So setting it's size shouldn't be the matter

Indeed thats true. In his code the canvas hadn't even been added though.

Anyways, I'm off home from work, cant wait to put my feet up and relax for the wkend.

Enjoy.

o2k

mattfairwAuthor Commented:
man oh man. i don't know what i changed after i commented out that one line, but whatever it was made it work. ...boy is my face red :)  Thank you!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.