码迷,mamicode.com
首页 > 编程语言 > 详细

【JAVA语言程序设计基础篇】--图形-- 使用抽象方法绘制函数图形

时间:2016-08-12 20:10:08      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:

一个很好的运用抽象类的例子


<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>


技术分享

【JAVA语言程序设计基础篇】--图形-- 使用抽象方法绘制函数图形

标签:

原文地址:http://blog.csdn.net/qq_24653023/article/details/52192370

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!