java - Is it possible to create an object that has a generic attached to it that is an extension of an abstract class? -


i'm making abstract class (let's call screen) extension of jpanel. want in future users/myself able extend it. part isn't issue.

i'm trying make class (let's call 1 "scratchdrawingproject") extension of jframe, , has generic attached it. class has instance of abstract class mentioned above screen added it.

i want user, in project's main method (inside other random file create), able create instance of scratchdrawingproject generic being extension of screen. in scratchdrawingproject's constructor, want it's screen variable instantiated new object of extension of screen user passes in.

so far screen have:

public abstract class screen extends jpanel{      private jframe parent;     private graphics2d graphics;      //constructor     public screen(jframe parent){         this.parent = parent;         parent.add(this);         setbounds([bounds]);         repaint();     }      //basically stuff put in extension's constructor, method     //can created ide eclipse      public abstract void start();            //basically paintcomponent() extension, in disguise ease of     //use of newcomers aren't *too* knowledgeable of java.     public abstract void drawmethod();       //repaint() in disguise     public void draw(){         repaint();     }              public void paintcomponent(graphics g){         graphics = (graphics2d) g;         draw();     }      //...methods extensions of class can put in "drawmethod" definition     //that call graphics.*, user never has see word graphics     //as creating extension...  } 

so far scratchdrawingproject have:

public class scratchdrawingproject<screentype> extends jframe{      private screen screen;      public scratchdrawingproject(){         setdefaultcloseoperation(exit_on_close);         setbounds([bounds]);         setvisible(true);         screen = (screentype)new object(); //which gives me runtime error because can't         screen.setparent(this);            //cast object screen, , can't make         screen.start();                    //screen new screentype() because     }                                      //gives me compiling error saying                                             //screentype can't instantiated. } 

i've tried doing thought going stupid going give me compiling error, , was:

public class scratchdrawingproject<screentype extends screen> extends jframe{ 

never would've guessed wouldn't me compiling error purely because of how ridiculous seemed in concept. instead, got error again when came instantiating screen, whether tried

screen = new screentype(); 

which still tells me screentype cannot instantiated, or

screen = (screentype)new screen(); 

which tells me screen can't instantiated (which understand because it's abstract)


so in conclusion! goal people able make own class

public class whatever{      public static void main(string[] args){         scratchdrawingproject<idkscreen> screen = new scratchdrawingproject<idkscreen>();     } } 

and

public class idkscreen extends screen{      start(){         //do stuff!     }      drawmethod(){         //do more stuff!     } } 

and have whatever automatically create scratchdrawingproject, create instance of idkscreen, add it, call idkscreen.repaint() instantiate graphics future use, , call idkscreen's start() , whatever user puts in there.

so, first off, seem lack understand how swing painting works. i'd suggest having closer @ painting in awt , swing , performing custom painting, seem violating important concepts.

starting abstract screen...

public abstract class screen extends jpanel{      // bad idea     //private graphics2d graphics;      //constructor     public screen(){         // bad idea, use getpreferredsize instead         //setbounds([bounds]);         // pointless, you're not displayable yet         //repaint();     }      //basically stuff put in extension's constructor, method     //can created ide eclipse      public abstract void start();            //basically paintcomponent() extension, in disguise ease of     //use of newcomers aren't *too* knowledgeable of java.     //public abstract void drawmethod();      // based on seem trying do, 1      //repaint() in disguise     public void draw(){         repaint();     }              @override     protected void paintcomponent(graphics g){         graphics2d g2d = (graphics2d) g.create();         // painting stuff...         performpaint(g2d);         g2d.dispose();         // among worse things can do, put         // don't change or modify state of component in paint         // method might, directly or indirectly, trigger repaint,         // you're asking trouble         //draw();     }      // basically, gets called when component needs repainted     protected abstract void performpaint(graphics2d g2d);      //...methods extensions of class can put in "drawmethod" definition     //that call graphics.*, user never has see word graphics     //as creating extension...  } 

next, need define requirements scratchdrawingproject class...

public class scratchdrawingproject<s extends screen> extends jframe{      private s screen;      public scratchdrawingproject(s screen){             setdefaultcloseoperation(exit_on_close);             this.screen = screen;             add(screen);             pack();             setvisible(true);             screen.start();                         }                                            public s getscreen() {         return screen;     }  }     

basically says s must instance of class extends screen

now, can define our implementation of screen...

public class myawesomescreen extends screen {      @override     public void start() {         // broom     }      @override     protected void performpaint(graphics2d g2d) {         // paint me rainbow     } } 

and create instance of scratchdrawingproject our implementation...

myawesomescreen myawesomescreen = new myawesomescreen(); scratchdrawingproject<myawesomescreen> myscratchdrawingproject = new scratchdrawingproject<>(myawesomescreen); 

the question becomes why? myscratchdrawingproject doesn't seem care implementation of screen, it's instance of screen, that's out-of-context observation


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -