-1
public SubClass extends SuperClass
{
}

for example a class like this how would i get access to JPanel methods

public void paintComponent(Graphics g)   {
 }

to do some animation in subClass because java doesn't support multiple inheritance and i tried looking for an interface equivalent of JPanel but i couldn't find one

for example

   public class Animal{
   ...some code
   }


  public class Dog extends Animal {
        // in this class i want to use the PaintComponent method 
          public void paintComponent(Graphics g)   {

        }
  }
thebigone
  • 61
  • 1
  • 9

2 Answers2

2

It sounds as if you're using inheritance incorrectly since the questions you ask are not solved through inheritance but rather through composition. For example, one way for a class to change the state of another is for the first class to hold a reference to the second class and have it call methods of the second class, and this is likely what you need to do. The details of a solution will depend on the details of your problem and your code.

You state:

what i was trying to do was use the paintCompontent method() in my own personal subclass but i couldn't extend the subclass with JPanel as the personal subclass has already been extended

You should do all your drawing within the JPanel's paintComponent period, and the JPanel is part of the view the visualized part of your program. But what is painted can depend on the state of other classes, in particular, the model, or the classes of your program that deal with program logic. I suggest that you separate your concerns along a M-V-C (model-view-controller) pattern to allow you to do just what you want -- allow state changes in the model be reflected in the view.

Again, for a more detailed answer, you're going to want to provide more detail about your problem and your code.

For a very (overly) simplistic example using your code:

// no need to extend JPanel....
public class Dog extends Animal {
    // give Dog a drawing method
    public void draw(Graphics2D g2) {
        // .... some code to draw
    }
}

public class DrawingPanel extends JPanel {
    private Dog dog; // hold Dog reference

    // allow passage of current Dog into class
    public DrawingPanel(Dog dog) {
        this.dog = dog;
    }

    @Override
    protected void paintComponent(g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        dog.draw(g2);  // draw Dog's picture
    }
}

elsewhere:

public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> {
        Dog myDog = new Dog();
        DrawingPanel drawingPanel = new DrawingPanel(myDog);

        JFrame myFrame = new JFrame("Dog Frame");
        myFrame.add(drawingPanel);

        //..... etc...

        myFrame.setVisible(true);
    });
}    
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
1

what i was trying to do was use the paintCompontent method() in my own personal subclass but i couldn't extend the subclass with JPanel as the personal subclass has already been extended

If that is the case, you may want to take a look at your deisgn again. There are at least 2 ways:

Use composition over inheritance:

class Subclass extends JPanel     //or extends to other component of your interest
{
    private MySuperClass obj;
}

Instead of extending to MySuperClass, see if it is possible to make it an attribute to your subclass. (This of course depends on what MySuperClass is).


Make your SuperClass a Component:

class MySuperClass extends JPanel    //or extends to other component of your interest
{
    //Your attributes and behaviours
}

class MySubClass extends MySuperClass
{
    //Your attributes and behaviours
}

This of course also depends on what your Superclass is. Until we know exactly what your super class is. These are the 2 possible ways.


Add on:

From your added codes, it is not necessary to extends the class you want to draw to a Component. You can write your own draw(g) method. For example: Animating Cars.

Community
  • 1
  • 1
user3437460
  • 17,253
  • 15
  • 58
  • 106
  • in the MySubClass class would you be able to access JPanel methods or would you only be able to access them in The MySuperClass class – thebigone Mar 17 '16 at 22:58
  • @thebigone According to your scenario, you will be using the first approach (composition over inheritance). You can take a look at the link I placed in my solution. The code is showing exactly what you want. You may look at the implementation for class `Car` from the link. – user3437460 Mar 17 '16 at 23:01