java - draw control inside another using its graphics -


i'm trying draw jcomponent inside calling second component's paint passing first component's graphics.

i'm trying create gui editor, (reinventing wheel, know, it's proof of concept) have class extends jpanel want draw components vectorcontrols.

so far got method in extended jpanel:

@suppresswarnings("serial") public class sketch extends jpanel {     private vector<jcomponent> controls = new vector<jcomponent>();      public sketch() {         super();         this.setlayout(new boxlayout(this,boxlayout.y_axis));     }      public void addcontrol(jcomponent c) {         dimension d = new dimension(100,50);         c.setpreferredsize(d);         c.setminimumsize(d);         c.setmaximumsize(d);         controls.add(c);         this.repaint();         this.revalidate();     }      public void paintcomponent(graphics g) {         super.paintcomponent(g);         for(int i=controls.size()-1; i>=0; i--) {             jcomponent c = controls.get(i);             c.paint(g);         }            } } 

i'm building/attaching sketch panel this:

public guieditor() {   mainframe = new jframe("gui editor");   mainframe.setdefaultcloseoperation(jframe.exit_on_close);   sketch mainpanel = new sketch();   mainpanel.setpreferredsize(new dimension(640,480));    gridbaglayout gbl = new gridbaglayout();   gridbagconstraints gbc = new gridbagconstraints();    mainframe.setlayout(gbl);    jpanel toolspanel = new jpanel();   toolspanel.setpreferredsize(new dimension(160,480));   toolspanel.setlayout(new gridlayout(0,1));          for(control c : toolboxitems ) {       abstractaction action = new toolboxbuttonaction(mainpanel, c.type);       jbutton b = new jbutton(action);       b.settext(c.title);       toolspanel.add(b);   }    gbc.gridx = 0;   gbc.gridy = 0;   gbl.setconstraints(mainpanel, gbc);   mainframe.add(mainpanel);    gbc.gridx = 1;   gbc.gridy = 0;   gbl.setconstraints(toolspanel, gbc);   mainframe.add(toolspanel);    mainframe.pack();   mainframe.setlocationrelativeto(null);   mainframe.setvisible(true); } 

inside toolboxbuttonaction, i'm doing this:

public void actionperformed(actionevent e) {     try {         sketch.addcontrol(control.newinstance());     } catch (instantiationexception e1) {         e1.printstacktrace();     } catch (illegalaccessexception e1) {         e1.printstacktrace();     } } 

but i'm writing because doesn't work.

any ideas on how achieve this?

i'm trying draw jcomponent inside calling second component's paint passing first component's graphics.

components can painted when component has non-zero size. size of component determined layout manager.

your basic code looks reasonable, unless have code size , locate components won't see anything. if set size components paint on top of 1 another.

or problem may parent panel doesn't have size not painted. default flowlayout uses preferred size of child components determine panels size. since don't add components directly panel there no child components preferred size 0. when reinvent wheel need reinvent everything.

without sscce context of how use code unknown can guess.

edit:

create sscce when have problem , working hard coded values before trying work dynamically. like:

import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*;  public class sketch extends jcomponent {     private vector<jcomponent> controls = new vector<jcomponent>();      public void addcontrol(jcomponent c)     {         c.setsize(100, 50);         int location = controls.size() * 50;         c.setlocation(location, location);         controls.add(c);         repaint();     }      @override     public void paintcomponent(graphics g)     {         super.paintcomponent(g);          for(int i=controls.size()-1; i>=0; i--)         {             jcomponent c = controls.get(i);             point location = c.getlocation();             g.translate(location.x, location.y);             c.paint(g);             g.translate(-location.x, -location.y);         }     }      private static void createandshowui()     {         sketch sketch = new sketch();         sketch.addcontrol( new jbutton("button") );         sketch.addcontrol( new jtextfield(10) );         sketch.addcontrol( new jcheckbox("checkbox") );          jframe frame = new jframe("sketch");         frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.add( sketch );         frame.setsize(400, 400);         frame.setlocationbyplatform( true );         frame.setvisible( true );     }      public static void main(string[] args)     {         eventqueue.invokelater(new runnable()         {             public void run()             {                 createandshowui();             }         });     } } 

Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -