java - not able to set Background color of the panel -
in assignment of graphics, not able set background color of panel. if put colorpanel panel, color of panel background changes, circle doesn't move if change size of panel. in assignment required if change size of frame, circle should @ center of frame
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class colormenuframe extends jframe { private jmenu colormenu = new jmenu("colors"); private colorpanel p = new colorpanel(); public colormenuframe(){ p.setpreferredsize( new dimension( 100, 100 ) ); this.add(p); } // main method public static void main(string[] args) { colormenuframe frame = new colormenuframe(); frame.pack(); frame.setlocationrelativeto(null); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true); } //colorpanel class public class colorpanel extends jpanel { private int diameter; public colorpanel() { // creating colors jmenubar jmenubar menubar = new jmenubar(); setjmenubar(menubar); jmenu colormenu = new jmenu("colors"); menubar.add(colormenu); // creating background , foreground menus jmenu backgroundmenu = new jmenu("background"); jmenu foregroundmenu = new jmenu("foreground"); // items inside background , foreground menus jmenuitem reditem = new jmenuitem("red"); jmenuitem greenitem = new jmenuitem("green"); jmenuitem blueitem = new jmenuitem("blue"); jmenuitem reditem1 = new jmenuitem("red"); jmenuitem greenitem1 = new jmenuitem("green"); jmenuitem blueitem1 = new jmenuitem("blue"); // adding red, green , blue items background menu backgroundmenu.add(reditem); backgroundmenu.add(greenitem); backgroundmenu.add(blueitem); // adding red, green , blue items foreground menu foregroundmenu.add(reditem1); foregroundmenu.add(greenitem1); foregroundmenu.add(blueitem1); // adding background , foreground sub menus main menu bar colors colormenu.add(backgroundmenu); colormenu.add(foregroundmenu); //calling actionlisteners // calling actionlistner after clicking on red button reditem.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { setbackground(color.red); repaint(); } }); // calling actionlistner after clicking on green button greenitem.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { setbackground(color.green); repaint(); } }); // calling actionlistner after clicking on blue button blueitem.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { setbackground(color.blue); repaint(); } }); // calling actionlistner after clicking on red button reditem1.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { setforeground(color.red); repaint(); } }); // calling actionlistner after clicking on green button greenitem1.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { setforeground(color.green); repaint(); } }); // calling actionlistner after clicking on green button blueitem1.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { setforeground(color.blue); repaint(); } }); } //paint component method, create circle @ center of panel protected void paintcomponent(graphics g){ int width = getsize().width; int height =getsize().height; if (width <= height){ diameter = width / 2; } else if (height <= width){ diameter = height / 2; } int r = diameter / 2; int x = (width / 2) - r; int y = (height / 2)- r; //drawing circle g.filloval(x, y, diameter, diameter); } } }
don't forget call super.paintcomponent(g)
within paintcomponent
method override. on first line.
@override protected void paintcomponent(graphics g) { super.paintcomponent(g); // ....
this tell jpanel own housekeeping painting.
also, if want smooth out border of circle, set graphics rendering hints antialiasing:
// this: graphics2d g2 = (graphics2d) g; g2.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on); // before calling this: g.filloval(x, y, diameter, diameter);
note circle seems center fine when run code.
Comments
Post a Comment