一、题目要求
显示函数图像。编写程序,画出函数f(x)=x*x的图像。
二、代码示例
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
abstract class MyShape{
double zs;double zx;double ys;double yx;
public MyShape(){
zs=0;zx=0;ys=0;yx=0;
}
public MyShape(double a,double b,double c,double d) {
this.zs=a;this.zx=b;this.ys=c;this.yx=d;
}
abstract void draw(Graphics g);
}
class MyLine extends MyShape{
public MyLine(double a,double b,double c,double d) {
super(a,b,c,d);
}
void draw(Graphics g) {
Graphics2D g2=(Graphics2D )g;
Line2D line=new Line2D.Double(this.zs,this.zx,this.ys,this.yx);
g2.draw(line);
}
}
public class Main {
public static void main(String []args) {
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame Frame = new JFrame();
Frame.add(new DrawComponent());
Frame.pack();
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Frame.setVisible(true);
}
});
}
}
class DrawComponent extends JComponent
{
private static final int DEFAULT_WIDTH=600;
private static final int DEFAULT_HEIGHT=600;
@Override
public void paintComponent(Graphics g)
{
MyLine aLine1 = new MyLine(0, 300, 600, 300);
aLine1.draw(g);
MyLine aLine2 = new MyLine(300, 0, 300, 600);
aLine2.draw(g);
MyLine aLine3 = new MyLine(570, 280,580,270);
aLine3.draw(g);
MyLine aLine4 = new MyLine(570, 270,580,280);
aLine4.draw(g);
MyLine aLine5 = new MyLine(590,290, 600, 300);
aLine5.draw(g);
MyLine aLine6 = new MyLine(590,310, 600, 300);
aLine6.draw(g);
MyLine aLine7 = new MyLine(290,10, 300,0);
aLine7.draw(g);
MyLine aLine8 = new MyLine(300, 0, 310, 10);
aLine8.draw(g);
MyLine aLine9 = new MyLine(315, 10, 320,15);
aLine9.draw(g);
MyLine aLine10 = new MyLine(325, 10, 320,15);
aLine10.draw(g);
MyLine aLine11 = new MyLine(320,15, 320,25);
aLine11.draw(g);
fun(g);
}
public void fun(Graphics g) {
int []x = new int[600];
int []y = new int[600];
for(int i=0;i<600;i++) {
x[i]=i;
y[i]=300-(i-300)*(i-300);
}
Graphics2D g2d = (Graphics2D) g.create();
g2d.drawPolyline(x, y, 600);
}
public Dimension getPreferredSize()
{
return new Dimension(DEFAULT_WIDTH,DEFAULT_HEIGHT);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/103138.html