大家看看我写的这道作业题。
程序实现画一个函数曲线
函数 y = a + b*x + c*x*x + d*x*x*x + e*x*x*x*x;
其中 a, b, c, d, e 为该函数的系数。 大家可以通过按键来动态改变设置各系数,从而改变图形。比如按下 a 键,系数a 以 0。1 为单位增加, shift + a, 则是 0。1 为单位减少。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
public class DrawMyFunction extends Canvas
{
static final Color BG_COLOR = Color.WHITE;
static final Color FG_COLOR = Color.BLACK;
double a = -2.2,
b =-2.3,
c = 1,
d = 1.5,
e = 0.1;
final double SCHRITTE = 0.1;
DrawMyFunction()
{
setBackground(BG_COLOR);
addKeyListener(new MyKeyListener());
setFocusable(true);
requestFocus();
}
public void paint(Graphics g)
{
int width = getSize().width;
int xRaster = width/18;
width = xRaster*18;
int height = getSize().height;
int yRaster = height/18;
height = yRaster*18;
setForeground(FG_COLOR);
// draw x-axis
g.drawLine(0, height/2, width, height/2);
for(int ix = xRaster; ix
g.drawLine(ix, height/2, ix, height/2-5);
g.drawString("0", width/2-10, height/2+15);
g.drawString("2", width-xRaster-5, height/2+15);
g.drawString("x", width-10, height/2-5);
g.drawString("-2", xRaster-5, height/2+15);
// draw y-axis
g.drawLine(width/2, 0, width/2, height);
for(int iy = yRaster; iy
g.drawLine(width/2, iy, width/2+5, iy);
g.drawString("4", width/2-10, yRaster+5);
g.drawString("-4", width/2-20, height-yRaster+5);
g.drawString("y", width/2+10, yRaster+5);
// draw koeffiziente
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
String sExpression = "a=" + nf.format(a) +
" b=" + nf.format(b) +
" c=" + nf.format(c) +
" d=" + nf.format(d) +
" e=" + nf.format(e);
g.drawString( sExpression, 5, height-5);
// draw the function
double x_step = 4./(8*2*xRaster-1);
double y_step = 8./(8*2*yRaster-1);
double x, y;
int x0, y0, x1, y1;
x0 = xRaster;
x = -2;
y = a + b*x + c*x*x + d*x*x*x + e*x*x*x*x;
y0 = (y>=0)?(height/2-(int)(y/y_step+0.5)):(height/2+(int)(Math.abs(y)/y_step+0.5));
x += x_step;
for( ;x<=2; x += x_step)
{
x1 = x0+1;
y = a + b*x + c*x*x + d*x*x*x + e*x*x*x*x;
y1 = (y>=0)?(height/2-(int)(y/y_step+0.5)):(height/2+(int)(Math.abs(y)/y_step+0.5));
g.drawLine(x0, y0,x1, y1);
x0 = x1;
y0 = y1;
}
}
public static void main(String [] args)
{
JFrame window = new JFrame("Draw my function");
window.setSize(560, 400);
window.setLocation(100,100);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(new DrawMyFunction());
window.setVisible(true);
}
//-----------------------------------------------
class MyKeyListener extends KeyAdapter
{
public void keyPressed(KeyEvent ke)
{
if(ke.getKeyChar()=='A')
a = a - SCHRITTE;
if(ke.getKeyChar() == 'a')
a = a + SCHRITTE;
if(ke.getKeyChar()=='B')
b = b - SCHRITTE;
if(ke.getKeyChar() == 'b')
b = b + SCHRITTE;
if(ke.getKeyChar()=='C')
c = c - SCHRITTE;
if(ke.getKeyChar() == 'c')
c = c + SCHRITTE;
if(ke.getKeyChar()=='D')
d = d - SCHRITTE;
if(ke.getKeyChar() == 'd')
d = d + SCHRITTE;
if(ke.getKeyChar()=='E')
e = e - SCHRITTE;
if(ke.getKeyChar() == 'e')
e = e + SCHRITTE;
repaint();
}
}
}