How to use graphics related classes in Java

Source: Internet
Author: User

There are many problems:

1. How to accurately draw some curves? All the parameters in graphics are integers, Because pixels are inseparable, but this is precisely because the image is not smooth. How can this problem be solved?

2. Just like the performance in the task manager and the online graphics, how can I display them dynamically and repeatedly erase and redraw images?

3. How to Achieve re-painting, especially in dynamic drawing?

4. All curves are straight lines from the calculus point of view, so can Java be competent to draw a curve by drawing a straight line? How can I develop table-level processing and teach complex and timely drawing programs?

The code for the above problems encountered when drawing a sine compound curve is as follows:

Code:
  1. Import java. AWT. color;
  2. Import java. AWT. container;
  3. Import java. AWT. graphics;
  4. Import javax. Swing. jframe;
  5. Public class testdrawline {
  6. Public static void main (string [] ARGs ){
  7. New drawline ();
  8. }
  9. }
  10. Class drawline extends jframe {
  11. Point start;
  12. Point end;
  13. Container P;
  14. Public drawline (){
  15. P = getcontentpane ();
  16. Setbounds (200,200,800,400 );
  17. Setvisible (true );
  18. P. setbackground (color. Black );
  19. Setlayout (null );
  20. // Paintzb (this. getgraphics ());
  21. // Paintsin (this. getgraphics ());
  22. Paintcomponents (this. getgraphics ());
  23. Setresizable (false );
  24. This. setdefaclocloseoperation (jframe. exit_on_close );
  25. }
  26. // Public void paint (Graphics g ){
  27. // G. drawline (80, 80,720, 80 );
  28. // Paintsin (this. getgraphics ());
  29. //}
  30. Public void paintcomponents (Graphics GG ){
  31. GG. drawline (10,100,200,400 );
  32. Final graphics G = GG;
  33. Start = new point (0, 80 );
  34. End = new point (0, 80 );
  35. G. setcolor (color. Red );
  36. Runnable run = new runnable (){
  37. Point temp = NULL;
  38. Int x = 0;
  39. Public void run (){
  40. Int d = 1;
  41. While (true ){
  42. Try {
  43. Temp = new point (x, 80 + (INT) (40 * Math. Sin (math. Pi * (x-80)/30 )));
  44. G. drawline (start. X, start. Y, end. X, end. y );
  45. // G. drawline (80, 80,720, 80 );
  46. G. drawline (790-start.x, start. Y + 120,790 -end.x, end. Y + 120 );
  47. G. drawline (start. X, start. Y + 240, end. X, end. Y + 240 );
  48. Start = end;
  49. End = temp;
  50. Thread. Sleep (10 );
  51. } Catch (interruptedexception e ){
  52. E. printstacktrace ();
  53. }
  54. If (x> 800 | x <0 ){
  55. D =-D;
  56. Color c = G. getcolor ();
  57. If (C = color. Red ){
  58. G. setcolor (color. Blue );
  59. } Else {
  60. G. setcolor (color. Red );
  61. }
  62. }
  63. X + = D;
  64. }
  65. }
  66. };
  67. New thread (run). Start ();
  68. }
  69. Class Point {
  70. Int X, Y;
  71. Public point (INT _ x, int _ y ){
  72. X = _ x;
  73. Y = _ y;
  74. }
  75. }
  76. }

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.