JAVA编程题目
绘制函数曲线:在Applet画面中实现y=a*x2+b*x+c函数曲线的绘制;(1)通过Applet参数传递a,b,c值;(2)函数的x变化范围为-10到10。请会的朋友...
绘制函数曲线:在Applet画面中实现y=a*x2+b*x+c 函数曲线的绘制;(1) 通过Applet参数传递a,b,c值;(2) 函数的x变化范围为-10到10。 请会的朋友帮我下,这个太难了,我实在不会。谢谢!
展开
1个回答
2013-08-21
展开全部
import java.awt.*;
import java.applet.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;
public class HeLing extends Applet implements ActionListener{
private int a,b,c,thick;
Image img;
int x0,y0;
int m=x0-20;
int k=0;
static Color draw=Color.green;
Button color= new Button("color");
public void init(){
a=Integer.parseInt(getParameter("a"));
b=Integer.parseInt(getParameter("b"));
c=Integer.parseInt(getParameter("c"));
thick=Integer.parseInt(getParameter("thick"));
x0=-b/(2*a);
img=createImage(20,35);
Graphics gimg=img.getGraphics();
gimg.drawOval(5,5,10,10);
gimg.drawLine(10,15,10,30);
gimg.drawLine(10,20,4,25);
gimg.drawLine(10,20,16,25);
gimg.drawLine(10,30,3,35);
gimg.drawLine(10,30,17,35);
add(color);
color.addActionListener(this);
} public void actionPerformed(ActionEvent e){
draw=JColorChooser.showDialog(HeLing.this,"曲线的颜色",draw);
}
public void paint(Graphics g){
Graphics2D g2d=(Graphics2D)g;
int offx=200;
int offy=200; g2d.setPaint(Color.blue);
g2d.setStroke(new BasicStroke(2));
g2d.draw(new Line2D.Float(offx-100,offy+0,offx+100,offy+0));
g2d.draw(new Line2D.Float(offx+0,offy-100,offx+0,offy+100));
g2d.draw(new Line2D.Float(offx+96,offy-5,offx+100,offy+0));
g2d.draw(new Line2D.Float(offx+96,offy+5,offx+100,offy+0));
g2d.draw(new Line2D.Float(offx-4,offy-95,offx+0,offy-100));
g2d.draw(new Line2D.Float(offx+4,offy-95,offx+0,offy-100));
g2d.drawString("x",offx+105,offy);
g2d.drawString("y",offx+0,offy-105);
GeneralPath polly=new GeneralPath();
int x=-40;
int x1=5*x; int y1=a*x*x+b*x+c;
int x2,y2;
polly.moveTo(offx+x1,offy-y1); for(;x<=40;x++)
{ x2=x*5;
y2=a*x*x+b*x+c;
polly.lineTo(offx+x2,offy-y2);
}
g2d.setPaint(draw); g2d.setStroke(new BasicStroke(thick)); g2d.draw(polly); int n= a*m*m + b*m+ c;
g.drawImage(img,offx+5*m-10,offy-n-15,this);
m++;
k++;
try{ Thread.sleep(120);
} catch (InterruptedException e) { }
repaint();
if(k%30==0) m=x0-20; }
}
import java.applet.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;
public class HeLing extends Applet implements ActionListener{
private int a,b,c,thick;
Image img;
int x0,y0;
int m=x0-20;
int k=0;
static Color draw=Color.green;
Button color= new Button("color");
public void init(){
a=Integer.parseInt(getParameter("a"));
b=Integer.parseInt(getParameter("b"));
c=Integer.parseInt(getParameter("c"));
thick=Integer.parseInt(getParameter("thick"));
x0=-b/(2*a);
img=createImage(20,35);
Graphics gimg=img.getGraphics();
gimg.drawOval(5,5,10,10);
gimg.drawLine(10,15,10,30);
gimg.drawLine(10,20,4,25);
gimg.drawLine(10,20,16,25);
gimg.drawLine(10,30,3,35);
gimg.drawLine(10,30,17,35);
add(color);
color.addActionListener(this);
} public void actionPerformed(ActionEvent e){
draw=JColorChooser.showDialog(HeLing.this,"曲线的颜色",draw);
}
public void paint(Graphics g){
Graphics2D g2d=(Graphics2D)g;
int offx=200;
int offy=200; g2d.setPaint(Color.blue);
g2d.setStroke(new BasicStroke(2));
g2d.draw(new Line2D.Float(offx-100,offy+0,offx+100,offy+0));
g2d.draw(new Line2D.Float(offx+0,offy-100,offx+0,offy+100));
g2d.draw(new Line2D.Float(offx+96,offy-5,offx+100,offy+0));
g2d.draw(new Line2D.Float(offx+96,offy+5,offx+100,offy+0));
g2d.draw(new Line2D.Float(offx-4,offy-95,offx+0,offy-100));
g2d.draw(new Line2D.Float(offx+4,offy-95,offx+0,offy-100));
g2d.drawString("x",offx+105,offy);
g2d.drawString("y",offx+0,offy-105);
GeneralPath polly=new GeneralPath();
int x=-40;
int x1=5*x; int y1=a*x*x+b*x+c;
int x2,y2;
polly.moveTo(offx+x1,offy-y1); for(;x<=40;x++)
{ x2=x*5;
y2=a*x*x+b*x+c;
polly.lineTo(offx+x2,offy-y2);
}
g2d.setPaint(draw); g2d.setStroke(new BasicStroke(thick)); g2d.draw(polly); int n= a*m*m + b*m+ c;
g.drawImage(img,offx+5*m-10,offy-n-15,this);
m++;
k++;
try{ Thread.sleep(120);
} catch (InterruptedException e) { }
repaint();
if(k%30==0) m=x0-20; }
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |