Java-Applet Drawn Drawing Drawing Gadgets As a member of the IT industry, I believe that many people have exposed the flowchart. During the development of a software, whether it is a pre-demand analysis, system design, or behind specific procedures
The counting involves the drawing of the flowchart, compared to the monotonic text description, it presented the entire system in front of the user in a more intuitive manner. Usually, we have developed using software developers.
The flowchart production software is drawn to draw the flow chart. I don't know if you have any of the process drawing tools. Here is the production of a flowchart production gadget based on Java-Applet.
I hope to help you learn. In addition, when reading this document code, if you find an unclear place, welcome to correct, thank you! // ************* First, the overall frame design Import javax.swing. *; Import java.awt. *; Import java.awt.image. *; Import java.awt.event IMPORT JAVA.Applet. *; Import Java.util. *; // API-Package you need to use during loading code
Public class applet7 extends applet {// Defines some variables that need to be used int count = 6; // Loaded pictures Vector v = new vector (); // Storage draw information image [] img = new image [count]; // Deposit the picture into the image = 0, myHeight = 0; // Arrow start point coordinate int widthcount = 0; // Double INTS = 0, starty = 0, endx = 0, endy = 0; // Draw the starting point and endpoint coordinate of the icon connection canvas startobj = null, endobj = null; // start and end Dynamic icon String Drawclear = ""; // Whether to draw cable and point to arrow int clickx = 0, Clicky = 0, MOVEX = 0, Movey = 0; // is the coordinate CANVAS [] CAN = NULL; // dynamic icon array Mycanvas [] mycan = new mycanvas [count]; /// Toolbar icon array canvas curcan = null; // Currently focus icon canvas curdelcan = null; // Required to delete the current icon image curimg = null; // Current focus icon JPanel JP = New JPanel (null); // Toolbar Mycanvas Drawline = New Mycanvas ("Drawline"); // Toolbar icon MYCANVAS line icon Mediatracker Mt = New Mediatracker (this); // Media object for loading diagram Like memory
Public void init () {img [0] = this.getimage (this.GETCODEBASE (), "1.gif"); // Get image img [1] = this.getiMage (this.getcodebase (), "2 .gif "); img [2] = this.getimage (this.getcodebase ()," 1.jpg "); img [3] = this.getiMage (this.getcodebase ()," 2.jpg "); img [4] = this.getimage (this.getcodebase (), "3.jpg"); img [5] = this.getimage (this.getcodebase (), "4.jpg"); this.resize (800,600); / / Set the size of the applet, browse in the browser according to the HTML code to set this.setLayout (null); // Set the layout manager, because the location of the icon is to be moved, so set to nullfor (int i = 0; i < Img.Length; i ) Mt.AddImage (IMG [i], 0); try {mt.waitforl ();} catch (exception ex) {system.err.println (ex.Tostring ());} // load Icon to memory}} Simple flowchart drawing tools include at least toolbars and drawing zones, toolbars, and dynamic icons, and build internal classes within the base class typically include icons and connection 1. Here, inherited Canvas class, construct icons and Line type tool class mycanvas extends canvas {image im = null; string str = ""; public mycanvas (string s) {// text constructor super (); this.str = s;}
Public Mycanvas (Image IM) {// Image Constructor Super (); this.SetCursor (New Cursor (Cursor.hand_cursor); // Set the style when the mouse is suspended, here is a hand THIS.IM = IM;}
Public void Paint (GRAPHICS G) {IF (Str.Equals (")) G.DrawImage (IM, 0, 0, this); // Draw, Tool Icon Else {g.drawstring (STR, 20, GetHeight () / 2); // text, line type}}}}
2. Add the following code in the init () member of the main area: public void init () {............ this.add (jp); // Add Toolbar JP.setBounds (0,0,800,80) ; // Place the toolbar jp.add (this.drawline); // Add line type icon on the toolbar JP.setBackground (New Color (230, 230, 230)); // Set the line-shaped icon background Drawline.setBounds (5, 5, 90, 30); // Place the line icon for (int i = 0; i Public void Paint (Graphics G) {// draws icon G. DrawImage (IM, 0, 0, this);}} Third, the registration event listener 1, the icon on the icon toolbar listened to the mouse event to get the icon to place the icon in the drawing area, modify the class mycanvas classification structure: public mycanvas {super () This.SetCursor (New Cursor (Cursor.hand_Cursor); this.im = im; this.addmouser () {// Register the mouse button Press the event, used to get the icon object to be drawn Public void MousePressed ( MouseEvent E) {ClickMYCANVAS (E);}});} 2, the mouse click event and the mouse click event in the primary class icon in the primary class, modify the init method: public void init () { ...................... Drawline.addmouselistener (new mouseadapter () {// Drawn Public Void MousePressed (MouseEvent E) {ClickDrawLine (E);}}); this.addmouselistener (new mouseadapter) () {// Used to place dynamic icon public void mouseclicked (mouseEvent E) {Clickhere (e);}});} 3 .addmouselistener (new mouseadapter () {// mouse button Press the event, get the current object, the operation of moving or deleting the object PUBLIC VOID MOUSEPRESSED (MouseEvent E) {Clickcanvas (E);}}); this.addKeylistener (New Keyadapter () {// Keyboard Press Event, Remove Icon Object Public Void Keypressed (KeyEvent E) {IF (E.GetKeyCode () == Keyevent.vk_delete) RemoveCanvas (E);}}; this.addmouseMotionListener (New Mo UseMotionAdapter () {// mouse drag event, mobile dynamic icon public void mousedragged (mouseEvent e) {MoveCanvas (E);}}) Fourth, write code to the event: 1. Toolbar Public Void ClickMycanvas (MouseEvent E) {((applet7) this.getParent (). GetParent ()). Drawline.setBackground (null); // Restore the default background of the line icon ( (Applet7) this.getParent (). getParent ()). Drawclear = ""; // Let the line icon initialization ((applet7) this.getParent (). getParent ()). startobj = null; // Initialize the drawing line segment Start object ((applet7) this.getParent (). GetParent ()). Endparent ()). Endobj = null; // Initialize the end of the drawing line segment CURCAN = (canvas) E.GETSource (); get the current icon object curimg = (Image ) im; // Get image of the current icon object} 2, the plot area public void clickhere (mouseEvent E) {if (this.curcan == null) return; if (E.GETMODIFIERS () == mouseevent.button3_mask { // Add a dynamic icon to the plot area, position at the mouse click Canvas Ca = New Canvas (CURIMG); this.Add (CA); ca.setbounds (E.GETX (), E.GETY (), curimg .Getwidth (this), curimg.getheight (this); this.Validate ();}} public void clickdrawLine (MouseEvent e) {if (. drawLine.getBackground () equals (new Color (230,230,255))) {// draw a line segment can drawLine.setBackground (new Color (230,230,230)); startObj = null; endObj = null; DrawcleR = "";} else {// cannot draw line segment Drawline.setBackground (New Color (230, 230, 255)); DrawClear = "DRAW";}} 3, dynamic icon public void Movecanvas (MouseEvent E) {// in two icons Drawing line segments while storeing these two icons and line segments of information canvas mcan = (canvas) E.GETSource (); int mtop = mcan.gety (), mleft = mcan.getx (), mwidth = mcan.getwidth ), ma Hight = mcan.getHeight (); mcan.setbounds (MLEFT E.GETX () - ClickX, MTOP E.GETY () - Clicky, MWidth, MHEIGHT); for (int i = 0; i Public void clickcanvas (MouseEvent E) {// Mouse Drag Icon is a change object location and object-related connection curdelcan = (canvas) E.GETSource (); clickx = E.GETX (); Clicky = E.Gety (); ((applet7) this.getParent ()). Repaint (); if ((applet7) this.getParent ()). DrawClear.equals (")) Return; if (((((((((applet7) this.getParent ()) .startobj == null && ((applet7) this.getParent ()). endobj == null) (Applet7) this.getParent ()). startobj = (canvas) E.Getsource (); else ing (((Applet7) THIS.GETPARENT ()). Startobj! = null && ((applet7) this.getParent ()). endobj == null && ((applet7) this.getParent ()). startobj! = (canvas) E.getsource ()) { ((applet7) this.getParent ()). Endobj = (canvas) E.GETSource (); vector v1 = new vector (); v1.addelement (new canvas [] {startobj, endobj}); v1.addelement (new Int [] {startobj.getx (), startobj.gety (), startobj.getwidth (), startobj.getHeight (), endobj.getx (), endobj.gety (), endobj.getWidth (), endobj.getHeight (); v.addelement (v1); (applet7) this.getParent ()). DrawClear = ""; ((Applet7) THIS.GETPARENT ()). Drawline.setBackground (NULL); (Applet7) this.getParent ()). startobj = null; ((applet7) this.getParent ()). endobj = null;}} V. Draw the PUBLIC VOID PAINT (GRAPHICS G) {// Draw line segment for (INT i = 0; i Public void Drawarrowhead (Graphics G, Canvas Startobj, Canvas Endobj) {// Draw Arrow Double XX = 0, YY = 0, XX1 = 0, YY1 = 0, XX2 = 0, YY2 = 0; MyWidth = MyHeight = (int) Math.Pow (Math.Pow (endobj.getwidth (), 2D) Math.Pow (endobj.getHeight (), 2D), 1/2D) / 2; xx = ENDX- (ENDX-STARTX ) * MyWidth / Math.Pow (endx-startx) * (endx-startx) (endy-starty), 1/2D); yy = endy- (endy- Starty) * MyHeight / (Math.Pow (Double) (Endx-Startx) (Double) (endy-starty), 1/2D)); XX1 = XX- 20 * Math.cos (Math.atan ((Double) YY-Starty / ((Double) XX-Startx) - Radians (30)); YY1 = YY-20 * Math.sin (Math.atan (Double) YY-STARTY) / (Double) XX-StartX) - Radians (30)); XX2 = XX-20 * Math.sin (Radians (60) -Math.atan ((Double) (YY- Starty)) / (XX-Startx))))))))))))))); yy2 = yy-20 * Math.cos (Radians (60) -math.atan ((Double) ((Double)) / ((Double) (XX-STARTX)))))))); if (startobj.getx () startobj.getwidth () / 2> endobj.getx () endobj.Getwidth () / 2) {xx1 = xx 20 * Math.cos (Math.atan ((Double) YY-Starty) / (Double) XX-Startx) - Radians (30)); yy1 = yy 20 * Math.sin (Math.atan Double) YY-STARTY) / (Double) XX-Startx) - Radians (30)); XX2 = XX 20 * Math.sin (Radians (60) -math.atan ((Double) (YY-Starty )) / (xx-startx))))))))); yy2 = yy 20 * Math.cos (Radians (60) -Math.atan ((Double) ((Double)) / ((Double) (xx-startx))))))));} g.fillpolygon (new int {{(int) xx2, (int) xx1, (int) xx}, new int [] {(int) YY2, (int) yy1, (int) YY}, 3);} PUBLIC DOUBLE RADIANS (INT De De De De Dou) {// This function is used to convert an angle into an arc RETURN ((double) degrees * math.pi / 180.0; 6. Test instructions Compile this applet7.java file, with AppletViewer View the applet on the main area has a line icon written with "Drawline" and several image icons, put the mouse on the image icon, the cursor becomes Hand shape, click, get the icon that needs to draw , Then move to the drawing area, right click on the mouse to place the selected icon to the corresponding position; when the icon placed in the main area exceeds two, click the Drawline line icon on the toolbar, make it discolored, then click The dynamic icon A and dynamic icon B in the main area will appear between A and B, according to the order, first clicked icon points to be clicked. Icon, here is a-> b, and after the line segment is drawn, the line-shaped icon restores the initial state, and you must click to connect (Note: An icon can connect to multiple icons simultaneously); Move the mouse to the dynamic icon of the main area, the cursor becomes the moving shape, click the mouse, get the delete object, press the Delete button on the keyboard, you can delete the icon just selected and It is connected to all line segments. Effect picture: If you don't understand, you can write: beyond_xiruo@21cn.com // ******************************* ***************************************** * All code * // ** *********************************************************** ****************** Import javax.swing. *; import java.awt. *; import java.awt.Image. *; import java.awt.event. * Import java.applet. *; import java.util. *; Public class applet7 extends applet {int count = 6; Vector v = new vector (); image [] img = new image [count]; int mywidth = 0, myHeight = 0; int widthcount = 0; boolean [] b = new Boolean [count]; int [] width = new int [count], w = new int [count], h = new int [count], wid = new int [count], hei = New Int [count], ww = new int [count]; int startX = 0, starty = 0, endx = 0, endy = 0; canvas startobj = null, endobj = null; string drawclear = " "; int clickx = 0, Clicky = 0, Movex = 0, Movey = 0; Canvas [] can = null; mycanvas [] Mycan = new mycanvas [count]; canvas curcan = null; canvas curdelcan = null; image curimg = NULL; JPANEL JP = New JPANel (NULL); Mycanvas Drawline = New Mycanvas ("Drawline"); Mediatracker Mt = New Mediatracker (this); Public void init () {img [0] = this.getimage (this.GETCODEBASE (), "1.gif"); img [1] = this.getiMage (this.GetcodeBase (), "2.gif"); IMG [2] = this.getimage (this.GetcodeBase (), "1.jpg"); img [3] = this.getimage (this.getcodebase (), "2.jpg"); img [4] = THIS .GetImage (this.getcodebase (), "3.jpg"); img [5] = this.getimage (this.getcodebase (), "4.jpg"); this.resize (800, 600); this.setlayout (null ); for (int i = 0; i public void clickdrawLine (MouseEvent e) {if (drawLine.getBackground () equals (new Color (230,230,255)).) {drawLine.setBackground (new Color (230,230,230)); startObj = null; endObj = null; drawclear = ""; } else {Drawline.setBackground (New Color (230, 230, 255)); DrawClear = "DRAW";}} public void Paint (Graphics G) {for (int i = 0; i Public void Drawarrowhead (graphics g, canvas startobj, canvas endobj) {double xx = 0, yy = 0, xx1 = 0, yy1 = 0, xx2 = 0, yy2 = 0; MyWidth = myHeight = (int) Math.Pow (Double) (Endobj.Getwidth (), 2D) Math.Pow (endobj.getHeight (), 2D), 1/2D) / 2; xx = endx- (endx-startx) * MyWidth / Math.Pow (endx-startx) * (endx-startx) (endy-starty), 1/2D); yy = endy- (endy-starty) * MyHeight /( (Double) ((Double) ((Double) ((Double) (Double) (Double) (Double) (Double) (Double) (Medy-Starty) * (Medy-Starty), 1/2D) .;xx1=xx-20*math. COS (Math.atan ((Double) YY-Starty / ((Double) XX-Startx) - Radians (30)); yy1 = yy-20 * math.sin (Math.atan ((Double) YY -starty) / ((Double) XX-Startx) - Radians (30)); XX2 = XX-20 * Math.sin (Radians (60) -math.atan ((Double) (YY-Starty) / (xx-startx)))))))); yy2 = yy-20 * Math.cos (Radians (60) -math.atan ((Double) ((Double)) / (Double) (xx- STARTX))))))); if (startobj.getx () startobj.getwidth () / 2> endobj.getx () endobj.Getwidth () / 2) {xx1 = xx 20 * Math.cos (Math.atan ((Double) YY-Starty / (Double) XX-Startx) - Radians (30)); YY1 = YY 20 * Math.sin (Math.atan (((Double) ) YY-startY) / (Double) XX-startX) - Radians (30)); xx2 = xx 20 * math.sin (Radians (60) -math.atan ((Double) ("YY-Starty) ) / (xx-startx)))))))); yy2 = yy 20 * Math.cos (Radians (60) -math.atan ((Double) ((Double)) / (Double) XX-startX)))))))));} g.fillpolygon (new int {{(int) xx2, (int) xx1, (int) xx}, new int [] {(int) yy2, (int) yy1, INT) YY}, 3);} Public Double Radians (int DetrEs) {return ((double) degRees * math.pi / 180.0;} // ***************************************** Dynamic icons Class Canvas extends Canvas {Image im = null; public canvas (Image im) {super (); this.setCursor (new Cursor (Cursor.MOVE_CURSOR)); this.im = im; this.addMouseListener (new MouseAdapter () {public void mousePressed (MouseEvent E) {Clickcanvas (E);}}); this.addKeylistener (new keyAdapter () {public void keypressed (keyevent e) {if (E.GetKeyCode () == Keyevent.vk_delete) Removecanvas (E);} }); this.addmouseMotionListener (new mousemotionadapter () {public void mousedragged (mouseevent e) {MoveCanvas (E);}}) Public void Movecanvas (MouseEvent E) {canvas mcan = (canvas) E.GETSource (); int mtop = mcan.gety (), mleft = mcan.getx (), mwidth = mcan.getWidth (), maHeiGHT = mcan.getHeight (); mcan.setbounds (Mleft E.GETX () - ClickX, MTOP E.GETY () - Clicky, MWidth, MWIGHT); For (int i = 0; i Public Void Removecanvas (KeyEvent E) {Try {canvas mcan = (canvas) E.GETSOURCE (); ((applet7) this.getParent ()). Remove (curdelcan); for (int i = v.size () - 1 ; i> = 0; I -) {Vector V1 = (Vector) V.Elementat (i); if ((canvas []) v1.Elementat (0)) [0] .equals (mcan) | (Canvas []) v1.Elementat (0)) [1] .Equals (mcan)) V.RemoveElementat (i); ((applet7) this.getParent ()). Validate (); ((Applet7). getParent ()). Repaint ();} catch (nullpointers) {return;}} public void clickcanvas (mouseevent e) {curdelcan = (canvas) E.GETSource (); clickx = E.GETX (); clicky = e (); ()). Repaint (); ((); ()). DrawClear.equals (")) Return; if ((((((((((((((applet7) THIS .GetParent ()). startobj == null& ((applet7) this.getParent ()). endobj == null) (Applet7) this.getParent ()). startobj = (canvas) E.getsource (); else ((applet7) this.getParent ()). Startobj! = null && ((applet7) this.getParent ()). endobj == null && ((applet7) this.getParent ()). STARTOBJ! = (canvas) E.getsource () This.getParent ()). endobj = (canvas) E.GETSource (); Vector v1 = new vector (); v1.addelement (new canvas [] {startobj, endobj}); v1.addelement (new int in "{startobj.getx (), startobj.gety (), startobj.getWidth (), startobj.getHEight (), endobj.getx (), endobj.gety (), endobj.getWidth (), endobj.getHeight (); v.addelement (v1); (applet7) this.getParent ()). DrawClear = ""; ((Applet7) THIS.GETPARENT ()). Drawline.setBackground (NULL); (Applet7) this.getParent ()). startobj = null; ((applet7) this.getParent ()). endobj = null;}} Public Void Paint (Graphics G) {g.drawimage (IM, 0, 0, this);}} // ******************************************************** **** Icon Toolbar Class Mycanvas Extends CANVAS {Image Im = Null; String Str = ""; Public Mycanvas (String S) {Super (); this.Str = S;} Public Mycanvas (Image Im) {Super (Image Im) {Super ); this.SetCursor (new cursor.hand_cursor); this.im = im; this.addmouselistener (new mouseadapter () {public void mousepressed (mouseEvent E) {ClickMycanvas (E);}}) Public void Clickmycanvas (MouseEvent E) {((applet7) this.getParent (). getParent ()). Drawline.setBackground (null); ((applet7) this.getParent (). getParent ()). Drawclear = "" ((applet7) this.getParent (). getParent ()). Startobj = null; ((applet7) this.getParent (). getParent ()). endparent ()). endparent ()). endobj = null; curcan = (canvas) E.GETSource (); curimg = (Image) im; Public void Paint (GRAPHICS G) {IF (str.equals (")) G.drawImage (IM, 0, 0, this); else {g.drawstring (STR, 20, GetHeight () / 2);}} }