栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java | PTA练习:显示函数图像

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java | PTA练习:显示函数图像

一、题目要求

显示函数图像。编写程序,画出函数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);
   }
}



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/361718.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号