Wednesday, 15 February 2012

java - paintComponent() and JColorChooser -



java - paintComponent() and JColorChooser -

i trying write programme class , stuck. need create shape (my initial) , fill jcolorchooser. have been able create initial using paint, , graphics. have been able fill shape using g.setcolor(new color(11, 139, 198)); don't know how add together jcolorchooser this. first post allow me know whatever can create easier read. here code:

/* programme - letter * programme desc - * programmer - bradon fredrickson * class - * created - oct 1, 2014 */ bundle letter; import java.awt.container; import java.awt.gridlayout; import javax.swing.jframe; import javax.swing.jlabel; public class letter { public static void plainletter() { /* * creating gui letter */ letterdraw plainletter = new letterdraw(); jframe logo = new jframe("my logos"); jlabel plain = new jlabel("plain letter"); plain.setlocation(10, 0); plain.add(plainletter); container pane = logo.getcontentpane(); pane.setlayout(new gridlayout(1, 1)); logo.setlocationrelativeto(null); // center frame logo.add(plainletter); logo.setvisible(true); logo.setsize(400, 200); logo.setlocation(400, 200); logo.setvisible(true); logo.setdefaultcloseoperation(logo.exit_on_close); }//end plain letter public static void main(string[] args) { plainletter(); new colorchooser(); }//end main method }//end letter

second class - drawing object

/* programme - thirdletter * programme desc - * programmer - bradon fredrickson * class - * created - oct 6, 2014 */ bundle letter; import java.awt.basicstroke; import java.awt.color; import java.awt.graphics; import java.awt.graphics2d; import java.awt.renderinghints; import java.awt.geom.path2d; import javax.swing.jpanel; public class letterdraw extends jpanel { /* * creating shape/letter */ public void paint(graphics graphics) { path2d.double path = new path2d.double(); graphics2d g = (graphics2d) graphics; path.moveto(17, 63); /* * top horizontal line */ path.curveto(21, 60, 21, 43, 17, 37); //left vert path.curveto(30, 43, 100, 43, 120, 37); //top horiz path.curveto(115, 45, 115, 57, 117, 62); //right vert path.curveto(105, 53, 60, 57, 17, 63); //bottom horiz right /* * bottom horizontal line */ path.moveto(32, 97); path.curveto(37, 85, 35, 79, 35, 77); //left vert path.curveto(45, 79, 45, 79, 100, 79); //top left horiz path.curveto(96, 85, 98, 92, 98, 98);//right vert path.curveto(89, 93, 79, 94, 32, 97);//bottom right horiz /* * vertical line */ path.moveto(40, 130); path.curveto(48, 110, 46, 70, 37, 55); //left vert path.curveto(62, 55, 62, 55, 60, 55); //top horiz path.curveto(68, 80, 68, 100, 63, 130); //right vert path.curveto(60, 127, 50, 127, 40, 130);//bottom horiz g.setrenderinghint( renderinghints.key_antialiasing, renderinghints.value_antialias_on); g.setstroke(new basicstroke(3)); //g.setstroke(new basicstroke(4, basicstroke.join_bevel, 0)); g.draw(path); g.setcolor(new color(11, 139, 198)); g.fill(path); }//end paint }//end letterdraw

third - colorchooser

/* programme - colorchooser * programme desc - * programmer - bradon fredrickson * class - * created - oct 10, 2014 */ bundle letter; import java.awt.borderlayout; import java.awt.color; import java.awt.font; import javax.swing.jcolorchooser; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.colorchooser.colorselectionmodel; import javax.swing.event.changeevent; import javax.swing.event.changelistener; public class colorchooser { /** * creating color chooser */ public colorchooser() { jframe frame = new jframe("jcolorchooser popup"); frame.setdefaultcloseoperation(jframe.exit_on_close); final jlabel label = new jlabel("www.java2s.com", jlabel.center); label.setfont(new font("serif", font.bold | font.italic, 48)); frame.add(label, borderlayout.north); final jcolorchooser colorchooser = new jcolorchooser(label. getbackground()); colorselectionmodel model = colorchooser.getselectionmodel(); changelistener changelistener = new changelistener() { public void statechanged(changeevent changeevent) { color newforegroundcolor = colorchooser.getcolor(); label.setforeground(newforegroundcolor); } }; model.addchangelistener(changelistener); frame.add(colorchooser, borderlayout.center); frame.pack(); frame.setvisible(true); }//end colorchooser }//end colorchooser class

right 2 jframes 1 shape , 1 jcolorchooser. set them in 1 frame also.

in letterdraw class, instead of doing

g.setcolor(new color(11, 139, 198));

better have class fellow member color color, , have setter it. this

public class letterdraw extends jpanel { private color color = color.blue; // default; public void setcolor(color color) { this.color = color; repaint(); } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); ... g.setcolor(color); } }

notice paintcomponent , super.paintcomponent. see link @trashgod in comments.

as color selection, jcolorchooser has static method, can phone call show color chooser dialog , obtain returned color from. can utilize color phone call setcolor on letterdraw. no need create own frame. instance.

color color = jcolorchooser.showdialog(null, "title", color.blue); letterdraw.setcolor(color);

if want maintain color chooser frame open though , have , using current code, need either utilize kind of mvc/observer design, or pass instance of letterdraw colorchooser constructor, can phone call letterdraw's set color method, after changes color.

"i set them in 1 frame also"

i noticed part of question.

you forget colorchooser class , create jcolorchooser in letter class. have access letterdraw object. here's how refactor might look

import java.awt.basicstroke; import java.awt.borderlayout; import java.awt.color; import java.awt.dimension; import java.awt.graphics; import java.awt.graphics2d; import java.awt.gridbaglayout; import java.awt.renderinghints; import java.awt.geom.path2d; import javax.swing.jcolorchooser; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.swingutilities; import javax.swing.event.changeevent; import javax.swing.event.changelistener; public class letter { letterdraw letterdraw = new letterdraw(); public letter() { jframe frame = new jframe(); jpanel letterdrawwrapper = new jpanel(new gridbaglayout()); letterdrawwrapper.add(letterdraw); frame.add(letterdrawwrapper); frame.add(createcolorchooser(), borderlayout.page_end); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } private jcolorchooser createcolorchooser() { jcolorchooser colorchooser = new jcolorchooser(); colorchooser.getselectionmodel().addchangelistener(new changelistener() { @override public void statechanged(changeevent e) { letterdraw.setcolor(colorchooser.getcolor()); } }); homecoming colorchooser; } public static void main(string[] args) { swingutilities.invokelater(new runnable() { public void run() { new letter(); } }); } } class letterdraw extends jpanel { private color color; public void setcolor(color color) { this.color = color; repaint(); } @override protected void paintcomponent(graphics graphics) { super.paintcomponent(graphics); path2d.double path = new path2d.double(); graphics2d g = (graphics2d) graphics; path.moveto(17, 63); /* * top horizontal line */ path.curveto(21, 60, 21, 43, 17, 37); //left vert path.curveto(30, 43, 100, 43, 120, 37); //top horiz path.curveto(115, 45, 115, 57, 117, 62); //right vert path.curveto(105, 53, 60, 57, 17, 63); //bottom horiz right /* * bottom horizontal line */ path.moveto(32, 97); path.curveto(37, 85, 35, 79, 35, 77); //left vert path.curveto(45, 79, 45, 79, 100, 79); //top left horiz path.curveto(96, 85, 98, 92, 98, 98);//right vert path.curveto(89, 93, 79, 94, 32, 97);//bottom right horiz /* * vertical line */ path.moveto(40, 130); path.curveto(48, 110, 46, 70, 37, 55); //left vert path.curveto(62, 55, 62, 55, 60, 55); //top horiz path.curveto(68, 80, 68, 100, 63, 130); //right vert path.curveto(60, 127, 50, 127, 40, 130);//bottom horiz g.setrenderinghint( renderinghints.key_antialiasing, renderinghints.value_antialias_on); g.setstroke(new basicstroke(3)); //g.setstroke(new basicstroke(4, basicstroke.join_bevel, 0)); g.draw(path); g.setcolor(color); g.fill(path); } @override public dimension getpreferredsize() { homecoming new dimension(150, 150); } }

java swing paintcomponent jcolorchooser

No comments:

Post a Comment