[Java Paint] drawing function image

Source: Internet
Author: User

Using the graphics class to draw any explicit function image, just modify the F () function in the code, and adjust the Timesx and Timesy parameters to zoom in or out of the image in the direction. You need to redefine the coordinate system.

1  Packagetest;2 3 Importjavax.swing.*;4 ImportJava.awt.Graphics;5 6  Public classDrawfunctionextendsJFrame {7     Static DoubleTimesx = ten, Timesy = 10;8     DoubleFDoublex) {9         returnMath.sin (x)/Math.pow (1.1,-X);//function ExpressionTen     } One     intx0, y0; A     Static intW = 600, H =; -     Static DoubleL =-W/2, R = W/2; - Graphics G; the      Public voidSetorigin (intXinty) { -          This. x0 =x; -          This. y0 =y; -         //Show coordinate axis +DrawLine (-W/2, 0, W/2, 0); -DrawLine (0,-H/2, 0, H/2); +DrawString ("X", W/2-30,-20); ADrawString ("Y", -20, H/2-20); at          for(inti = 1; I <= 10; i + +) { -Draw (W/2-i-6, i); -Draw (W/2-i-6,-i); -         } -          for(inti = 1; I <= 10; i + +) { -Draw (-I, H/2-i); inDraw (I, H/2-i); -         } to     } +      Publicdrawfunction () { -AddNewNewpanel ()); the     } *      Public Static voidMain (string[] args) { $Drawfunction frame =Newdrawfunction ();Panax NotoginsengFrame.settitle ("Drawfunction"); - frame.setsize (W, H); theFrame.setlocationrelativeto (NULL); + frame.setdefaultcloseoperation (jframe.exit_on_close); AFrame.setvisible (true); theFrame.setresizable (false); +     } -      Public classcoordinate2d { $         intx, y; $          PublicCoordinate2d (intXinty) { -              This. x =x; -              This. y =y; the         } -          Public intGetpixelpointx () {Wuyi             returnX0 +x; the         } -          Public intGetpixelpointy () { Wu             returnY0-y; -         } About     } $     classNewpanelextendsJPanel { -         protected voidpaintcomponent (Graphics g) { -             Super. paintcomponent (g); -G =G; ASetorigin (W/2, H/2); +             //in the following, draw what want draw! the              for(inti =-W/2; I <= W/2; i + +) { - Draw (I, work (i)); $             } the             /* the for (int i = 0; i <; i + +) { the int x = (int) (Math.random () * 400-200); the int y = (int) (Math.random () * 400-200); - drawstring ("haha", x, y); in             } the             */ the         } About     } the     intWorkintx) { the         //timesx = 0.01; the         //Timesy = +; +         return(int) (F (X/TIMESX) *timesy); -     } the      Public voidDrawintXinty) {Bayi         intX =Newcoordinate2d (x, y). Getpixelpointx (); the         intY =Newcoordinate2d (x, y). Getpixelpointy (); the g.drawline (x, y, x, y); -     } -      Public voidDrawrec (intX1,intY1,intX2,inty2) { the         intDX = x1 < x2? 1:-1; the         intdy = y1 < y2? 1:-1; the          for(inti = x1; I! = x2 + dx; i + =dx) { the              for(intj = y1; J! = y2 + dy; J + =dy) { - Draw (I, j); the             } the         } the     }94      Public voidDrawLine (intX1,intY1,intX2,inty2) { the         intDX = x1 < x2? 1:-1; the         if(X1 = =x2) Drawrec (x1, y1, x2, y2); the         Else {98             DoubleD = (Double) (y2-y1)/(X2-x1); About              for(inti = x1; I! = x2 + dx; i + =dx) { -Draw (I, (int) (Y1 + (i-x1) *d));101             }102         }103     }104      Public voidDrawString (String s,intXinty) { the         intX =Newcoordinate2d (x, y). Getpixelpointx ();106         intY =Newcoordinate2d (x, y). Getpixelpointy ();107 g.drawstring (S, X, Y);108     }109}

[Java Paint] drawing function image

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.