Java create graphics object for drawing complex graphics from working method -
i new java oo. using classic procedural aspects of java straightforward, includingevent processing , swing. having trouble objects.i have written small program in eclipsethat uses event processing detecting mouse movement , clicks in applet viewer. draw face in viewer on mouse click. face draw code done in method included in program, returns drawn face graphics object 'g' passed parameter when calling the 'drawface()' method. 'drawface()' method returns graphics type , program works fine! try make separate class in eclipse create new 'drawface' object when there mouse click, previous code, , instantiate via 'new'. face not drawn. sure not either defining face constructor class correctly or invoking correctly. attaching first working code using method ('drawfacemethod'), , non-working code using main code , second constructor class draw face ('drawfaceobject'). both codes short , similar. appreciated.
import java.awt.*; import java.awt.event.*; import java.applet.*; public class appletdrawfacemethod extends applet implements mouselistener , mousemotionlistener{ int mousex = 0, mousey = 30; // display area coordinates of mouse displayed int width = 0, height = 0; // size of applet viewing area string msg = ""; boolean mouseclick = false; //need test mouse click on true font font = new font("serif", font.bold, 36); public void init() { width = getsize().width; //get applet viewing size height = getsize().height; system.out.println("applet dimensions: " + width + " , " + height); //addmouselisteners addmouselistener(this); addmousemotionlistener(this); } // end init() // paint method executed on every event public void paint(graphics g) { //need associate font paint method g.setfont(font); //test mouseclick , set boolean if (mouseclick == true){ //calls method 'drawface', passes mouseclick x,y coordinates // , graphics object 'g' //the 'drawface' method returns graphic object of face drawn drawface(mousex, mousey, g); msg = "face drawn @ mouse click " + mousex + ", " + mousey; //re-initialize variables mouseclick = false; mousex = 0; mousey = 30; } // end of 'if mouseclick == true' face drawing code g.drawstring(msg, mousex, mousey); } // end of paint method // following methods mouse 'listener' methods detect mouse events // handle mouse moved. public void mousemoved(mouseevent me) { // show status msg = "moving mouse @ " + me.getx() + ", " + me.gety(); //showstatus("moving mouse @ " + me.getx() + ", " + me.gety()); repaint(); } // handle mouse clicked. public void mouseclicked(mouseevent me) { mouseclick = true; //set boolean 'mouseclick' true //save coordinates mousex = me.getx(); mousey = me.gety(); repaint(); } // handle mouse entered. public void mouseentered(mouseevent me) { // save coordinates //mousex = 0; //mousey = 30; msg = "mouse entered."; repaint(); } // handle mouse exited. public void mouseexited(mouseevent me) { // save coordinates //mousex = 0; //mousey = 30; msg = "mouse exited applet viewer."; repaint(); } //other method mouse event methods required if not used // handle button pressed. public void mousepressed(mouseevent me) {} // handle button released. public void mousereleased(mouseevent me) {} // handle mouse dragged. public void mousedragged(mouseevent me) {} /////////////////////////////////////////////////////////// // // 'drawface method' receive graphics object parameter g' // must return graphics object method draws // /////////////////////////////////////////////////////////// public graphics drawface(int xstart,int ystart, graphics g) { g.drawoval (mousex, mousey, 120, 150); // head. g.drawoval ((mousex + 45), (mousey + 60), 30, 30); // nose g.fillarc((mousex + 20), (mousey + 85), 80, 40, 180, 180); //mouth. g.drawoval ((mousex + 17),(mousey + 35), 30, 20); //left eye. g.drawoval ((mousex + 70),(mousey + 35), 30, 20); //right eye. g.filloval ((mousex + 28), (mousey + 41), 10, 10); //left pupil. g.filloval ((mousex + 81), (mousey + 41), 10, 10); //right pupil. g.drawoval ((mousex - 15), (mousey + 52), 15, 30); //left ear. g.drawoval ((mousex + 120), (mousey + 52), 15, 30); //right ear. g.drawarc ((mousex + 15), (mousey + 25),35,15,0,180);//left brow. g.drawarc ((mousex + 67), (mousey + 25), 35, 15, 0, 180);//right. return g; //returns pointer drawn graphics face } //end of drawface method } // end of class /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// // // // 'drawfaceobject' applet code , followed 'drawface' constructor class // // ////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// import java.awt.*; import java.awt.event.*; import java.applet.*; public class drawfaceobject extends applet implements mouselistener , mousemotionlistener{ int mousex = 0, mousey = 30; // display mouse coordinates int width = 0, height = 0; // size of applet viewing area string msg = ""; boolean mouseclick = false; //need test mouse click on true font font = new font("serif", font.bold, 36); public void init() { width = getsize().width; //get applet viewing size height = getsize().height; system.out.println("applet dimensions: " + width + " , " + height); //addmouselisteners addmouselistener(this); addmousemotionlistener(this); } // end init() // paint method executed on every event public void paint(graphics g) { //need associate font paint method g.setfont(font); //test mouseclick , set boolean if (mouseclick == true){ //creates object'drawface', passes mouseclick x,y coordinates // , graphics object 'g' class_drawface face = new class_drawface(g); msg = "face drawn @ mouse click " + mousex + ", " + mousey; //re-initialize variables mouseclick = false; mousex = 0; mousey = 30; } // end of 'if mouseclick == true' face drawing code g.drawstring(msg, mousex, mousey); } // end of paint method // following mouse 'listener' methods detect mouse events // handle mouse moved. public void mousemoved(mouseevent me) { // show status msg = "moving mouse @ " + me.getx() + ", " + me.gety(); //showstatus("moving mouse @ " + me.getx() + ", " + me.gety()); repaint(); } // handle mouse clicked. public void mouseclicked(mouseevent me) { mouseclick = true; //set boolean 'mouseclick' true //save coordinates mousex = me.getx(); mousey = me.gety(); repaint(); } // handle mouse entered. public void mouseentered(mouseevent me) { // save coordinates //mousex = 0; //mousey = 30; msg = "mouse entered."; repaint(); } // handle mouse exited. public void mouseexited(mouseevent me) { // save coordinates //mousex = 0; //mousey = 30; msg = "mouse exited applet viewer."; repaint(); } //other method mouse event methods required if not used // handle button pressed. public void mousepressed(mouseevent me) {} // handle button released. public void mousereleased(mouseevent me) {} // handle mouse dragged. public void mousedragged(mouseevent me) {} } // end of class ///////////////////////////////////////////////////////////// // // 'drawface' constructor //////////////////////////////////////////////////////////// import java.awt.*; import java.awt.event.*; import java.applet.*; public class class_drawface extends applet{ //instance variables public graphics g; public int x, y; class_drawface(graphics g) { int x = 0; int y = 0; } class_drawface(int mousex, int mousey, graphics g) { int x = mousex; int y = mousey; } public graphics drawface(int x, int y, graphics g) { g.drawoval (x, y, 120, 150); // head. g.drawoval ((x + 45), (y + 60), 30, 30); // nose g.fillarc ((x + 20), (y + 85),80,40,180, 180);//mouth. g.drawoval ((x + 17),(y + 35), 30, 20);//left eye. g.drawoval ((x + 70),(y + 35), 30, 20);//right eye. g.filloval ((x + 28), (y + 41),10,10);//left pupil. g.filloval ((x + 81), (y + 41),10,10);//right pupil. g.drawoval ((x - 15), (y + 52), 15, 30);//left ear. g.drawoval ((x + 120), (y + 52), 15, 30);//right ear. g.drawarc ((x + 15), (y + 25),35,15,0,180);//left eyebrow. g.drawarc ((x + 67), (y + 25),35,15,0,180);//right eyebrow. return g; //return drawnface graphics object } //end of drawface class constructor }//end of class
Comments
Post a Comment