Inheritance Problem!

Theking123
Theking123 used Ask the Experts™
on
I have an applet like this

import java.awt.*;
import java.applet.*;
public class MainClass extends Applet
{
   Board b = new Board()
   public void paint(Graphics g)
   {
       
       b.draw(g);
   }
}

inside this class i can use the methods getImage() and getDocumentBase() and getCodeBase

however inside my Board class (object) it looks like this

import java.awt.*;
import java.applet.*;
public class Board
{
    Image img;
    public void draw(Graphics g)
    {
       g.drawString("this works graphics context ok",50,50);
       img = getImage(getDocumentBase(),"image.gif");
    }
}


now i know its not very wise to load the image in the paint/draw method this WONT be done when i get it working however the problem i have is in the board object i CANNOT reference getImage or getDocumentBase i have tried
super.getImage
MainClass.getImage
g.getImage
(in MainClass getImage() and getDocumentBase() work fine)
and dont understand why the methods are not being passed down the inheritance. Any help would be muchly appreciated as this has left me STUMPED for many days now.

thank you in advance.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
Make Board extend Applet.
Top Expert 2016

Commented:
No, forget that. You could make Board have a reference to its parent container -

public Board(Applet applet){
  this.applet = applet;
}

Author

Commented:
I have tried that and it doesn't help i have also attempted to access getImage() as Applet.getImage() but this didnt help either. but thanks for a quick response though
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Author

Commented:
not tried the Board(Applet applet) one thanks for the suggestion gonna give it a try!
Hi,
your Board don't extends any other class that have getImage or getDocumentBase  in its API, therefor these methods r not known in Board class.
u should send a refrence of your applet to your Board class and use it to get methods values:

import java.awt.*;
import java.applet.*;
public class Board
{
   Image img;
   Applet owner;

   // constructor
   public Board(Applet theOwner) {
      this.owner = theOwner;
   }

   public void draw(Graphics g)
   {
      g.drawString("this works graphics context ok",50,50);
      img = owner.getImage(owner.getDocumentBase(),"image.gif");
   }
}

-gkern

Author

Commented:
not tried the Board(Applet applet) one thanks for the suggestion gonna give it a try!
Top Expert 2016

Commented:
Not quite sure *why* you're doing what you're doing though. What's the idea behind this object model?

Author

Commented:
i see what your getting at however how do i send the parameter from inside my MainClass. is there somesort of getApplet() method like there is a getGraphics() otherwise what would i put in to pass the applet data to the constructor of board.

Author

Commented:
well its going to be big.
and the main paint will literally read.
Board.draw(g);
player1.draw(g);
player2.draw(g);
timer.draw(g);
etc
each sub-class (object) will be responsible for its own drawing just like they are responsible for there data manipulation for encapsulation purposes.
Top Expert 2016

Commented:
>>however how do i send the parameter from inside my MainClass

Board b = new Board(this);

Author

Commented:
thank you going to give it a quick try before i accept it as answer!
Hi,
it better that all of your classes that represent GUI items in the applet will extend JComponent (or other swing/awt classes) and will be added to the applet as GUI elements.
doing so, u won't need to "repaint" them yourself each time - it will be done automatically
-gkern

Author

Commented:
it didnt work here is my exact code im testing it with incase u can spot whats going wrong.

//Main Class
import java.awt.*;
import java.applet.*;

public class Main Applet extends Applet
{
     private Board b = new Board(1,1,this);

     public void paint(Graphics g)
     {
          b.paint(g);    
     }
}


//Board Class
import java.awt.*;
import java.applet.*;

public class Board
{
     Image background;
     Applet myApplet;
         
     Board(int x, int y, Applet applet)
     {
           this.myApplet = applet;
          xPos = x;
          yPos = y;
     }    
     
     public void paint(Graphics g, int width, int height)
     {
          g.drawString("Graphics Context Works...",150,150);
          background = getImage(getDocumentBase(),"background.gif");
          g.drawImage(background,0,0,this);          
     }
}



the error i get is
getDocumentBase() not found in class Board
Top Expert 2016

Commented:
b = new Board(this);

I'd put the above in init();
Top Expert 2016
Commented:
myApplet.getDocumentBase()
Hi,
look at my answer above, you need to do:
background = myApplet.getImage(myApplet.getDocumentBase(),"background.gif");

in order to use those methods.
-gkern

Author

Commented:
thank you both very much it is now working brilliantly! with this code

import java.awt.*;
import java.applet.*;

public class Board
{
     Image background;
     Applet myApplet;
         
     Board(Applet applet)
     {
           this.myApplet = applet;
     }    
     
     public void paint(Graphics g)
     {
          g.drawString("Graphics Context Works...",150,150);
          background = myApplet.getImage(myApplet.getDocumentBase(),"booksCLR.gif");
          g.drawImage(background,0,0,myApplet);          
     }
}

for the board and same code for MainClass. thank you
Top Expert 2016

Commented:
Board also ought to extend Panel.

Author

Commented:
so this would inherit all of Panels methods?
would that not allow getImage and getDocumentBase to work without sending the Applet Context to the constructor?
Top Expert 2016

Commented:
>>would that not allow getImage and getDocumentBase to work without sending the Applet Context to the constructor?

No. It might help with the painting though. But if the painting's OK, i wouldn't worry!

Author

Commented:
Yea i have controlled the drawing by sending the Graphics Context from the paint in the MainClass to the draw() in the Board object. This allows g.drawString etc: to work fine but thanks for the advice if i do encounter any problems that will be the first attempt to fix it :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial