⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exercise11_17.java

📁 Introduction to java programming 一书中所有编程练习部分的源码
💻 JAVA
字号:
// Exercise11_17.java: Draw generic functions
import java.awt.*;
import javax.swing.*;

public class Exercise11_17 extends JFrame {
  public Exercise11_17() {
    getContentPane().setLayout(new GridLayout(1, 2, 5, 5));
    getContentPane().add(new DrawSine());
    getContentPane().add(new DrawCosine());
  }

  public static void main(String[] args) {
    Exercise11_17 frame = new Exercise11_17();
    frame.setSize(400, 400);
    frame.setTitle("Exercise11_17");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}

class DrawXSquare extends AbstractDrawFunction {
  /**Implement the fuction*/
  public double f(double x) {
    // scaleFactor for adjusting y coordinates
    double scaleFactor = 0.01;

    return scaleFactor * x * x;
  }
}

class DrawSine extends AbstractDrawFunction {
  public double f(double x) {
    return 50 * Math.sin((x / 100.0) * 2 * Math.PI);
  }
}

class DrawCosine extends AbstractDrawFunction {
  public double f(double x) {
    return 50 * Math.cos((x / 100.0) * 2 * Math.PI);
  }
}

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);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -