sinpanel.java

来自「jfreechart 柱状图 均值图 和值图 实例」· Java 代码 · 共 50 行

JAVA
50
字号
package zyyt.youTian;

import java.awt.*;
import javax.swing.*;



public class SinPanel extends JPanel {
  /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Dimension panelSize = this.getSize();
    
    Location center = new Location(panelSize.width/2, panelSize.height/2);
    int radius = (int)((Math.min(panelSize.width, panelSize.height)/2)*0.9);

    //确定每个点的坐标
    int[] x = new int[2*radius+1];
    int[] y = new int[2*radius+1];
    for(int i = 0; i < 2*radius+1; i++) {
      x[i] = center.x - radius + i;
      double y1 = Math.sin(((double)(-radius + i) / radius) * 4 * Math.PI);//这个很重要,sin()里面必须为double值
      int y2 = (int)(y1 * 100);
      y[i] = center.y - y2;
    }

    g.setColor(Color.black);
    //画坐标轴
    g.drawLine(center.x - radius, center.y, center.x + radius, center.y);
    g.drawLine(center.x, center.y - radius, center.x, center.y + radius);
    g.drawLine(center.x + radius, center.y, center.x + radius - 10, center.y - 7);
    g.drawLine(center.x + radius, center.y, center.x + radius - 10, center.y + 7);
    g.drawLine(center.x, center.y - radius, center.x - 7, center.y - radius + 10);
    g.drawLine(center.x, center.y - radius, center.x + 7, center.y - radius + 10);

    g.drawPolyline(x, y, 2*radius+1);

    g.setColor(Color.red);
    g.setFont(new Font("ScanSerif", Font.BOLD, 12));
    g.drawString("X", center.x + radius, center.y - 10);
    g.drawString("Y", center.x + 10, center.y - radius);
  }
}


⌨️ 快捷键说明

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