swing - Java drawing application -
i need develop application has 3 buttons drawing line,a rectangle , circle. application should behave this: user clicks on button draw wanted shape, mouse cursor changes dot, user moves mouse down container, draws 2 dots clicking mouse twice @ desired locations , wanted shape drawn using 2 dots. information have gathered, know should use mouseclicklistener
drawing dots , call shape class parameters passed dot class draw shape. need know container use shapes, put mouseclicklistener
in order allow drawing in container , how restrict user drawing more points until button pressed again. far code: `public class mypanel {
private jframe frame; /** * launch application. */ public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { try { mypanel window = new mypanel(); window.frame.setvisible(true); } catch (exception e) { e.printstacktrace(); } } }); } /** * create application. */ public mypanel() { initialize(); } /** * initialize contents of frame. */ private void initialize() { frame = new jframe(); frame.setresizable(false); frame.setbounds(100, 100, 500, 400); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.getcontentpane().setlayout(null); jpanel panel = new jpanel(); panel.setbackground(color.white); panel.setbounds(10, 25, 474, 336); frame.getcontentpane().add(panel); jbutton btnline = new jbutton("line"); btnline.addactionlistener(new actionlistener() { public void actionperformed(actionevent arg0) { drawpoint draw = new drawpoint(); } }); btnline.setbounds(10, 0, 110, 23); frame.getcontentpane().add(btnline); jbutton btnrectangle = new jbutton("rectangle"); btnrectangle.setbounds(196, 0, 110, 23); frame.getcontentpane().add(btnrectangle); jbutton btncircle = new jbutton("circle"); btncircle.setbounds(374, 0, 110, 23); frame.getcontentpane().add(btncircle); } } public class drawpoint implements mouselistener { /** * @param args */ public static void main(string[] args) { // todo auto-generated method stub } @override public void mouseclicked(mouseevent arg0) { // todo auto-generated method stub getcoordinates drawapoint drawline(coordinates) } @override public void mouseentered(mouseevent arg0) { // todo auto-generated method stub } }
what need know container use shapes
typically, components custom draw done subclassing jpanel
, overriding paintcomponent
method. less customary, , arguably more clean oo perspective, 1 may subclass jcomponent
. find far fewer example code on web via route.
where put mouseclicklistener
on jpanel
subclass work.
in order allow drawing in container
you can't prevent user clicking in jpanel
, dragging outside it. but, can try detect condition, , make sure code ignores kind of input.
and how restrict user drawing more points until button pressed again.
use variables. e.g., boolean variable ready
true
initially, gets set false
when drawing starts , reset true
button press. , have drawing points handler check ready
value , initiate drawing if true
. may need other variables keep track of how many additional clicks allowed part of current drawing operation.
Comments
Post a Comment