The method of drawing a straight line using the key line is actually curved. It should be because this point is too big, so it looks like a straight line. My classmates didn't demonstrate this straight line. Among them, the paintcomponent (Graphics line) method is very important. It seems that the swing component inherits an interface paint, so this is not a defined function, but an interface implementation.
The Code is as follows:
Program for drawing:
Linepanel. Java
Import java. AWT .*;
Import java. AWT. event .*;
Import java. Lang. Math .*;
Import javax. Swing .*;
Public class linepanel extends jpanel implements mouselistener, mousemotionlistener
{
Private point point1 = NULL, point2 = NULL;
Private int flag = 0;
Public linepanel ()
{
Super ();
Addmouselistener (this );
Addmousemotionlistener (this );
Setbackground (color. White );
Setpreferredsize (New Dimensions (800,600 ));
Repaint ();
}
Public void paintcomponent (Graphics line ){
// Super. paintcomponent (line );
Line. setcolor (color. White );
Line. drawrect (0, 0, getwidth (), getheight ());
// Super ();
Paintline (line );
}
Public void paintline (Graphics line)
{
Super. paintcomponents (line );
Line. setcolor (color. Red );
If (point1! = NULL) & (point2! = NULL ))
{String str1, str2;
Str1 = point1.getx () + "," + point1.gety ();
Line. drawstring (str1, point1.x, point1.y );
Str2 = point2.getx () + "," + point2.gety ();
Line. drawstring (str2, point2.x, point2.y );
// Line. drawline (point1.x, point1.y, point2.x, point2.y );
DDA (line, point1, point2 );
}
} // Paint ends
// DDA
Public void DDA (Graphics line, point point1, point point2)
{
Int X1, Y1, X2, Y2;
Int dx, Dy, epsl, K;
Float X, Y, xincres, yincres;
X1 = point1.x;
Y1 = point1.y;
X2 = point2.x;
Y2 = point2.y;
DX = x2-x1;
DY = y2-y1;
X = x1;
Y = Y1;
If (math. Abs (dx)> math. Abs (dy ))
Epsl = math. Abs (dx );
Else
Epsl = math. Abs (dy );
Xincret = (float) dx/(float) epsl;
Yincret = (float) dy/(float) epsl;
For (k = 0; k <= epsl; k ++)
{
Line. filloval (INT) (x + 0.5), (INT) (Y + 0.5), 3 );
X = x + xincret;
Y = Y + yincret;
}
} // DDA ends
// Implement the listener Interface
Public void mousepressed (mouseevent E)
{
}
Public void mousedragged (mouseevent event ){}
Public void mouseclicked (mouseevent event)
{Flag ++;
If (flag % 2 = 0 ){
Point1 = event. getpoint ();
Repaint ();
}
Else
Point2 = event. getpoint ();
// Repaint ();
}
Public void mousereleased (mouseevent event)
{
Point2 = event. getpoint ();
Repaint ();
}
Public void mouseentered (mouseevent event ){}
Public void mouseexited (mouseevent event ){}
Public void mousemoved (mouseevent event)
{
Point2 = event. getpoint ();
// Repaint ();
}
}
A driver,
Line. Java
Import javax. Swing .*;
Public class line
{
Public line ()
{
}
Public static void main (string ARGs [])
{
Jframe frame = new jframe ("software 0804 Deng Chao linear program ");
Frame. setdefaclocloseoperation (jframe. exit_on_close );
Linepanel Panel = new linepanel ();
Frame. getcontentpane (). Add (panel );
Frame. Pack ();
Frame. setvisible (true );
}
}