public void PaintFunction ( Graphics g )
{
Font textFont = new Font ( "Serif", Font.BOLD, 30 );
g.setFont ( textFont );
g.setColor ( Color.black );
g.fillRect ( xcorner - 2, ycorner - 2, 804, 504 );
g.setColor ( Color.white );
g.fillRect ( xcorner + 2, ycorner + 2, 796, 496 );
g.setColor ( Color.black );
g.fillRect ( xcorner + 398, ycorner, 4, 500 );
g.fillRect ( xcorner, ycorner + 248, 800, 4 );
for ( int i = -7; i <= 7; i++ )
{
g.drawString ( "" + i, xcorner + 400 + 50 * i - 8, ycorner + 250 + 30 );
g.fillRect ( xcorner + 400 + 50 * i - 2, ycorner + 250 - 8, 4, 16 );
}
for ( int i = -4; i <= 4; i++ )
{
g.drawString ( "" + i, xcorner + 400 - 36, ycorner + 250 - 50 * i + 10 );
g.fillRect ( xcorner + 400 - 8, ycorner + 250 - 50 * i - 2, 16, 4 );
}
double y, prevy, prevx;
prevx = -8.0;
prevy = a * prevx * prevx * prevx + b * prevx * prevx + c * prevx + d;
for ( double x = -8.0; x <= 8.0; x += 0.1 )
{
y = a * x * x * x + b * x * x + c * x + d;
for ( int i = -1; i <= 1; i++ )
g.drawLine ( xcorner + (int)( 50 * x + 400 ), ycorner + (int)( -50 * y + 250 ) + i,
xcorner + (int)( 50 * prevx + 400 ), ycorner + (int)( -50 * prevy + 250 ) + i );
prevx = x;
prevy = y;
}
g.setColor ( Color.white );
g.fillRect ( 0, 0, xcorner - 2, 650 );
g.fillRect ( 0, 0, 900, ycorner - 2 );
g.fillRect ( 0, ycorner + 502, 900, 150 );
g.fillRect ( xcorner + 802, 0, 100, 600 );
g.setColor ( Color.black );
g.drawString ( "y = " + Format.right( a, 4, 1 ) + "x^3 + " + Format.right( b, 4, 1 ) +
"x^2 + " + Format.right( c, 4, 1 ) + "x + " + Format.right( d, 4, 1 ), 200, 565 );
}
Back to Lesson 27 Examples
Back to Java Main Page