不知道算不算 难题
请教
大家看看我写的这道作业题。
程序实现画一个函数曲线
函数 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<width; ix += 2*xRaster) 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<height; iy += 2*yRaster) 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(); } } }
我用我上面说的方法写了一个画:y = sin(x)图象的程序,只要改一下函数,就可以画任何函数图象了。 大家可以参考一下:
import java.awt.*; import javax.swing.*; import java.awt.geom.*; import java.util.*;
class MyPanel extends JPanel { ArrayList lines = new ArrayList(); double x = 20.0 - 200.0; double y = 50*Math.sin(Math.toRadians(x)) + 100.0; double px, py; MyPanel() { lines.ensureCapacity(720); for(int i = 0; i < 720; i++) { px = x + 1; py = 50*Math.sin(Math.toRadians(px)) + 100.0; lines.add(new Line2D.Double(x, y, px, py)); x = px; y = py; } } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.BLUE); int size = lines.size(); for(int i = 0; i < size; i++) g2.draw((Line2D)lines.get(i)); } }
class PaintFrame extends JFrame { MyPanel panel = new MyPanel(); PaintFrame() { setTitle("PaintFrame"); setSize(410, 240); setResizable(false);
getContentPane().add(panel); } }
public class Paint { public static void main(String[] args) { PaintFrame jellen = new PaintFrame(); jellen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jellen.show(); } }