Anthony Lee
Anthony Lee

Reputation: 9

Extending Graphics Void to Jframe?

I have Jframe Form and created class to draw graphics from it, and for to do that i need to extend that void class to jframe form

package grafiktest;

import java.awt.Color;
import java.awt.Graphics;


public class testoutput extends javax.swing.JFrame{
Graphics g;

public testoutput() {
    initComponents();
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    g=jPanel1.getGraphics();
test tod = new test(); 
tod.test();

}                                        

class i created :

     public class test extends testoutput{
     void test(){ 
      testoutput tod = new testoutput(); 
      g=tod.jPanel1.getGraphics();
   g.setColor(Color.decode("#7D9AB3"));
    g.fillRect(3,3,3,3);
   }
    }

is there any way to extend it? I'm newb for this, Thank you

Upvotes: 0

Views: 45

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347314

Ignoring your bad painting for the time been, you should start by taking a closer look at the Inheritance trail which will provide you with more information.

In short, no, this is not how inheritance works.

I appreciate that this can be confusing and I highly recommend you simplify your example by taking JFrame out of the picture and seeing how the operations might work

Let's start with simple enough example...

public class Parent {

    public Parent() {
        //...
    }

    public void performSomeAction() {
        System.out.println("I'm called from within parent");
    }
}

And some child class

public class Child extends Parent {

    @Override
    public void performSomeAction() {
        // Action overridden from parent
        // You can call super.performSomeAction()
        // if you wish to maintain the functionality the parent class was
        // performing as needed
        System.out.println("I'm called from within child");
    }

}

Now, let's have a look at what happens if we do...

Parent parent = new Parent();
Child child = new Child();

parent.performSomeAction();
child.performSomeAction();

This outputs...

I'm called from within parent
I'm called from within child

Not very exciting, but you get the idea.

But, wait, lets change Child

public class Child extends Parent {

    @Override
    public void performSomeAction() {
        super.performSomeAction()
        System.out.println("I'm called from within child");
    }

}

Now, if we re-run the test code, we get

I'm called from within parent
I'm called from within parent
I'm called from within child

Where did the extra "parent" call come from!? It comes from super.performSomeAction(), this simply calls the parent's method - this is how overriding a method can be used to extend it's functionality or even completely overwrite (as was done in the first example).

Now, lets do something really weird...

Parent parent = new Parent();
Parent child = new Child();

parent.performSomeAction();
child.performSomeAction();

What do you suppose the output will be?!

I'll leave it up to you to test, but you might also want to then have a look at Polymorphism and be prepared to be 🤯

And finally...Go and read through Performing Custom Painting and Painting in AWT and Swing for a better understanding of how painting works and what you need to do to work with it

Upvotes: 2

Related Questions