1. 程式人生 > >【JAVA語言程式設計基礎篇】--圖形-- 使用抽象方法繪製函式圖形

【JAVA語言程式設計基礎篇】--圖形-- 使用抽象方法繪製函式圖形

一個很好的運用抽象類的例子

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