java - Problems with detecting mouseClick in a JPanel and preventing circle from painting in JPanel -
my current program lets user move circle around jframe , change color pressing 1 of colors presented in jpanel @ bottom of jframe.
my code:
import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jbutton; import javax.swing.border.border; import javax.swing.borderfactory; import java.awt.event.mouseevent; import java.awt.event.mouselistener; import java.awt.event.mousemotionlistener; import java.awt.color; import java.awt.graphics; import java.awt.flowlayout; import java.awt.borderlayout; import java.awt.gridlayout; public class sixthprogram { public static void main(string[] args) { gui prog=new gui("sixthprogram"); prog.setbounds(350,250,500,250); prog.setvisible(true); } } class gui extends jframe implements mouselistener, mousemotionlistener { jbutton button; jpanel colorpan, color1, color2, color3 ,color4 ,color5; color color=color.black; int x=3,y=30; public gui(string header) { super(header); setdefaultcloseoperation(jframe.exit_on_close); setlayout(new borderlayout()); maker(); addmouselistener(this); addmousemotionlistener(this); add(colorpan, borderlayout.south); } public void maker() { colorpan = new jpanel(); border raisedbevel = borderfactory.createraisedbevelborder(); border loweredbevel = borderfactory.createloweredbevelborder(); border compound = borderfactory.createcompoundborder(raisedbevel, loweredbevel); colorpan.setborder(compound); colorpan.setlayout(new gridlayout(1, 0)); color1 = new jpanel(); color2 = new jpanel(); color3 = new jpanel(); color4 = new jpanel(); color5 = new jpanel(); color1.setbackground(color.white); color2.setbackground(color.green); color3.setbackground(color.red); color4.setbackground(color.blue); color5.setbackground(color.black); colorpan.add(color1); colorpan.add(color2); colorpan.add(color3); colorpan.add(color4); colorpan.add(color5); } @override public void paint(graphics g) { //g.setcolor(color.white); //g.fillrect(0,0,getwidth(),getheight()); super.paint(g); //do same thing above(clear jframe) g.setcolor(color); g.filloval(x,y,50,50); } public void mouseexited(mouseevent e) //mouselistener overrided methods {} public void mouseentered(mouseevent e) {} public void mousereleased(mouseevent e) {} public void mousepressed(mouseevent e) { system.out.println("press"); if(e.getx()+50 < getwidth() && e.gety()+50 < getheight()) // preventing out of bounds { x=e.getx(); y=e.gety(); repaint(); } } public void mouseclicked(mouseevent e) //press+release=click { system.out.println("click"); if((e.getx()>=8 && e.getx()<=105) && (e.gety()>=232 && e.gety()<=243)) color=color.white; else if((e.getx()>=106 && e.getx()<=203) && (e.gety()>=232 && e.gety()<=243)) color=color.green; else if((e.getx()>=204 && e.getx()<=301) && (e.gety()>=232 && e.gety()<=243)) color=color.red; else if((e.getx()>=302 && e.getx()<=399) && (e.gety()>=232 && e.gety()<=243)) color=color.blue; else if((e.getx()>=400 && e.getx()<=489) && (e.gety()>=232 && e.gety()<=243)) color=color.black; repaint(); } public void mousedragged(mouseevent e) //mousemotionlistener overrided methods { system.out.println("dragged ("+ e.getx() +","+ e.gety() +")"); if((e.getx()>=3 && e.gety()>=30) && (e.getx()+50<getwidth() && e.gety()+50<getheight())) //if circle dragged in jframe { x=e.getx(); y=e.gety(); repaint(); } } public void mousemoved(mouseevent e) {} }
the above code works expected , produces output:
when click colors in jpanel in bottom too, program works expected , changes color of circle.
the 2 problems appears are:
when resize jframe, color chooser doesn't work expected. due stupid way of hardcoding in
mouseclicked
method:public void mouseclicked(mouseevent e) //press+release=click { system.out.println("click"); if((e.getx()>=8 && e.getx()<=105) && (e.gety()>=232 && e.gety()<=243)) color=color.white; else if((e.getx()>=106 && e.getx()<=203) && (e.gety()>=232 && e.gety()<=243)) color=color.green; else if((e.getx()>=204 && e.getx()<=301) && (e.gety()>=232 && e.gety()<=243)) color=color.red; else if((e.getx()>=302 && e.getx()<=399) && (e.gety()>=232 && e.gety()<=243)) color=color.blue; else if((e.getx()>=400 && e.getx()<=489) && (e.gety()>=232 && e.gety()<=243)) color=color.black; repaint(); }
- the second problem circle can dragged onto color chooser. don't know should in order prevent this. hardcode values did problem #1, create same issue problem #1.
i prefer not use setresizable(false)
, jframe resizeable.
how fix mentioned problems?
i've re implemented problem in better way. here how looks now: problems solved.
import java.awt.borderlayout; import java.awt.color; import java.awt.flowlayout; import java.awt.graphics; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.mouseevent; import java.awt.event.mousemotionlistener; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.swingutilities; public class circlepainter implements mousemotionlistener, actionlistener { private jframe mainframe; private jpanel colorpanel, circlepanel; private jbutton whitecolorbutton, redcolorbutton, greencolorbutton, bluecolorbutton; private int circlewidth = 3, circleheight = 15; private color circlecolor = color.black; public circlepainter() { initgui(); } public void initgui() { mainframe = new jframe("circle"); mainframe.setlayout(new borderlayout()); mainframe.setdefaultcloseoperation(jframe.exit_on_close); mainframe.setsize(500, 400); colorpanel = new jpanel(new flowlayout()); whitecolorbutton = new jbutton(); whitecolorbutton.setbackground(color.white); whitecolorbutton.setactioncommand("white"); whitecolorbutton.addactionlistener(this); redcolorbutton = new jbutton(); redcolorbutton.setbackground(color.red); redcolorbutton.setactioncommand("red"); redcolorbutton.addactionlistener(this); greencolorbutton = new jbutton(); greencolorbutton.setbackground(color.green); greencolorbutton.setactioncommand("green"); greencolorbutton.addactionlistener(this); bluecolorbutton = new jbutton(); bluecolorbutton.setbackground(color.blue); bluecolorbutton.setactioncommand("blue"); bluecolorbutton.addactionlistener(this); colorpanel.add(whitecolorbutton); colorpanel.add(redcolorbutton); colorpanel.add(greencolorbutton); colorpanel.add(bluecolorbutton); circlepanel = new jpanel() { @override public void paintcomponent(graphics g) { super.paintcomponent(g); g.setcolor(circlecolor); g.filloval(circlewidth, circleheight, 50, 50); } }; circlepanel.addmousemotionlistener(this); circlepanel.setbackground(color.yellow); mainframe.add(circlepanel, borderlayout.center); mainframe.add(colorpanel, borderlayout.page_end); mainframe.setvisible(true); } @override public void actionperformed(actionevent e) { switch (e.getactioncommand()) { case "white": circlecolor = color.white; circlepanel.repaint(); break; case "red": circlecolor = color.red; circlepanel.repaint(); break; case "green": circlecolor = color.green; circlepanel.repaint(); break; case "blue": circlecolor = color.blue; circlepanel.repaint(); break; default: break; } } public static void main(string args[]) { swingutilities.invokelater(new runnable() { @override public void run() { new circlepainter(); } }); } @override public void mousedragged(mouseevent e) { if ((e.getx() >= 0 && e.gety() >= 0) && (e.getx() <= mainframe.getwidth() - 60) && (e.gety() <= mainframe.getheight() - 110)) { circlewidth = e.getx(); circleheight = e.gety(); circlepanel.repaint(); } } @override public void mousemoved(mouseevent arg0) { } }
here output:
Comments
Post a Comment