java - Why can't I paint on the panel -
i have made 3 panels south, east , north, trying draw circle on north panel can't figure out why not drawing. here code: want small application draws circles of different sizes , colors chosen user.
import com.sun.prism.shader.drawcircle_color_loader; import javax.swing.*; import javax.swing.event.changeevent; import javax.swing.event.changelistener; import java.awt.*; import java.awt.event.mouseevent; import java.awt.event.mouselistener; public class drawing extends jframe implements mouselistener{ private int r = 255, g = 0, b = 0; private jslider colorslider, redslider,greenslider,blueslider; private jlabel colorlabel,redlabel,greenlabel,bluelabel; private int x = 50; private int y = 50; public drawing(){ jframe frame = new jframe("drawing app"); frame.setdefaultcloseoperation(windowconstants.exit_on_close); frame.setsize(800,800); container contentpane = frame.getcontentpane(); jmenubar mb = new jmenubar(); frame.setjmenubar(mb); jmenu color = new jmenu("colour"); jmenu size = new jmenu("size"); mb.add(color); mb.add(size); jmenuitem colorred = new jmenuitem("red"); jmenuitem colorblue = new jmenuitem("blue"); jmenuitem colorgreen = new jmenuitem("green"); color.add(colorblue); color.add(colorgreen); color.add(colorred); jmenuitem 1 = new jmenuitem("1"); jmenuitem 2 = new jmenuitem("2"); jmenuitem 3 = new jmenuitem("3"); jmenuitem 4 = new jmenuitem("4"); jmenuitem 5 = new jmenuitem("5"); size.add(one); size.add(two); size.add(three); size.add(four); jpanel panel = new jpanel(); setbackground(color.white); contentpane.add(panel,borderlayout.north); jpanel panel1 = new jpanel(); contentpane.add(panel1,borderlayout.south); jpanel panel2 = new jpanel(); contentpane.add(panel2,borderlayout.east); panel1.setlayout(new gridlayout(0,1)); jcolorchooser colors = new jcolorchooser(); panel1.add(colors); frame.setvisible(true); panel.add(panel); } public void paint(graphics g) { g.setcolor(color.red); g.filloval(x,y,100,100); } public void mousepressed(mouseevent e) {} public void mousereleased(mouseevent e) {} public void mouseentered(mouseevent e) {} public void mouseexited(mouseevent e) {} public void mouseclicked(mouseevent e) { x = e.getx(); y = e.gety(); repaint(); } public static void main(string[] args) { drawing drawing = new drawing(); }
}
you created instance of jframe inside constructor drawing()
- remove instance , replace initialization inside constructor.
- add super.paint(g); first line of public void paint(graphics g) method.
Comments
Post a Comment