【JAVA语言程序设计基础篇】--图形-- 使用抽象方法绘制函数图形
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了【JAVA语言程序设计基础篇】--图形-- 使用抽象方法绘制函数图形,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2627字,纯文字阅读大概需要4分钟。
内容图文
一个很好的运用抽象类的例子
<span style="font-size:14px;">package chapter15_编程练习题; import java.awt.*; import javax.swing.*; @SuppressWarnings("serial") public class Exercise15_13 extends JFrame { public Exercise15_13() { setLayout(new GridLayout(1, 2,10,10)); add(new DrawSine()); add(new DrawCosine()); } public static void main(String[] args) { Exercise15_13 frame = new Exercise15_13(); frame.setSize(400, 400); frame.setTitle("Exercise15_13"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); // Center the frame frame.setVisible(true); } } @SuppressWarnings("serial") class DrawXSquare extends AbstractDrawFunction {//y=x平方 /**Implement the fuction*/ public double f(double x) { // scaleFactor for adjusting y coordinates double scaleFactor = 0.01; return scaleFactor * x * x; } } @SuppressWarnings("serial") class DrawSine extends AbstractDrawFunction {//sin public double f(double x) { return 50 * Math.sin((x / 100.0) * 2 * Math.PI); } } @SuppressWarnings("serial") class DrawCosine extends AbstractDrawFunction {//cos public double f(double x) { return 50 * Math.cos((x / 100.0) * 2 * Math.PI); } } @SuppressWarnings("serial")//抽象类 abstract class AbstractDrawFunction extends JPanel { /**Polygon to hold the points*/ private Polygon p = new Polygon(); final int TO_X_AXIS = 125; final int TO_Y_AXIS = 125; final int END_OF_X_AXIS = 275; final int END_OF_Y_AXIS = 200; /**Default constructor*/ protected AbstractDrawFunction() {//构造函数 drawFunction(); setBackground(Color.white); } /**Draw the function*/ public abstract double f(double x); /**Obtain points for x coordinates 100, 101, ..., 300*/ public void drawFunction() {//绘制曲线图形点 for (int x = -100; x <= 100; x++) { p.addPoint(x + TO_Y_AXIS, TO_X_AXIS - (int)f(x)); } } /**Paint the function diagram*/ protected void paintComponent(Graphics g) { super.paintComponent(g); // Draw x axis g.drawLine(10, TO_X_AXIS, END_OF_X_AXIS, TO_X_AXIS); // Draw y axis g.drawLine(TO_Y_AXIS, 30, TO_Y_AXIS, END_OF_Y_AXIS); // Draw arrows on x axis g.drawLine(END_OF_X_AXIS, TO_X_AXIS, END_OF_X_AXIS - 20, TO_X_AXIS - 10); g.drawLine(END_OF_X_AXIS, TO_X_AXIS, END_OF_X_AXIS - 20, TO_X_AXIS + 10); // Draw arrows on y axis g.drawLine(TO_Y_AXIS, 30, TO_Y_AXIS - 10, 50); g.drawLine(TO_Y_AXIS, 30, TO_Y_AXIS + 10, 50); // Draw x, y g.drawString("X", END_OF_X_AXIS - 20, TO_X_AXIS - 30); g.drawString("Y", TO_Y_AXIS + 20, 40); // Draw a polygon line by connecting the points in the polygon g.drawPolyline(p.xpoints, p.ypoints, p.npoints); } }</span>
原文:http://blog.csdn.net/qq_24653023/article/details/52192370
内容总结
以上是互联网集市为您收集整理的【JAVA语言程序设计基础篇】--图形-- 使用抽象方法绘制函数图形全部内容,希望文章能够帮你解决【JAVA语言程序设计基础篇】--图形-- 使用抽象方法绘制函数图形所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。