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:
- Import java. AWT. color;
- Import java. AWT. container;
- Import java. AWT. graphics;
- Import javax. Swing. jframe;
- Public class testdrawline {
- Public static void main (string [] ARGs ){
- New drawline ();
- }
- }
- Class drawline extends jframe {
- Point start;
- Point end;
- Container P;
- Public drawline (){
- P = getcontentpane ();
- Setbounds (200,200,800,400 );
- Setvisible (true );
- P. setbackground (color. Black );
- Setlayout (null );
- // Paintzb (this. getgraphics ());
- // Paintsin (this. getgraphics ());
- Paintcomponents (this. getgraphics ());
- Setresizable (false );
- This. setdefaclocloseoperation (jframe. exit_on_close );
- }
- // Public void paint (Graphics g ){
- // G. drawline (80, 80,720, 80 );
- // Paintsin (this. getgraphics ());
- //}
- Public void paintcomponents (Graphics GG ){
- GG. drawline (10,100,200,400 );
- Final graphics G = GG;
- Start = new point (0, 80 );
- End = new point (0, 80 );
- G. setcolor (color. Red );
- Runnable run = new runnable (){
- Point temp = NULL;
- Int x = 0;
- Public void run (){
- Int d = 1;
- While (true ){
- Try {
- Temp = new point (x, 80 + (INT) (40 * Math. Sin (math. Pi * (x-80)/30 )));
- G. drawline (start. X, start. Y, end. X, end. y );
- // G. drawline (80, 80,720, 80 );
- G. drawline (790-start.x, start. Y + 120,790 -end.x, end. Y + 120 );
- G. drawline (start. X, start. Y + 240, end. X, end. Y + 240 );
- Start = end;
- End = temp;
- Thread. Sleep (10 );
- } Catch (interruptedexception e ){
- E. printstacktrace ();
- }
- If (x> 800 | x <0 ){
- D =-D;
- Color c = G. getcolor ();
- If (C = color. Red ){
- G. setcolor (color. Blue );
- } Else {
- G. setcolor (color. Red );
- }
- }
- X + = D;
- }
- }
- };
- New thread (run). Start ();
- }
- Class Point {
- Int X, Y;
- Public point (INT _ x, int _ y ){
- X = _ x;
- Y = _ y;
- }
- }
- }