import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SinFrame extends JFrame {
public SinFrame() {
Container container = this.getContentPane();
container.setLayout(new BorderLayout());
SinPanel sinPanel = new SinPanel();
container.add(sinPanel, BorderLayout.CENTER);
this.setTitle("Show Sin Fuction!");
this.setSize(new Dimension(400, 400));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
this.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
this.setVisible(true);
}
public static void main(String[] args) {
new SinFrame();
}
}
class SinPanel extends JPanel {
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);
}
}
class Location {
public int x;
public int y;
public Location(int x, int y) {
this.x = x;
this.y = y;
}
}
分享到:
相关推荐
java图形处理及鼠标事件处理, 资源描述 资源描述 资源描述
Java图形化界面 计算器 用的是栈 比较简单
这是个用java写的正弦函数,希望对一些朋友可以有帮助哦
6.了解JAVA语言的图形用户界面JAVA SWING的设计知识。 7.运用JAVA SWING编写简单计算器程序,并实现其功能。 四、 实现功能 1.键入数字时,按下相应的数字键,如果按错可用(DEL)键消去一次数值,再重新输入正确...
1、编写一个记事本程序 要求: 用图形用户界面实现。 能实现编辑、保存、另存为、查找替换等功能。 提示:使用文件输入输出流。 2、编写一个计算器程序 要求: 界面模拟Windows中的计算器程序。 实现基本数学运算、...
函数绘图图形化多项式、指数函数、正弦函数和随机游走的图形用户界面。 写于 2014 年 12 月。 随机游走 正弦 指数 抛物线
Java课程设计题目 一、课程设计总体要求: 本次课程设计是对前面学过的所有面向对象的编程思想以及编程方法的一个总结、回 顾和实践,因此,开始设计前学生一定要先回顾以前所学的内容,明确本次作业设计所要用 到的技术...
7. 根据正弦曲线求出各点的深度,这个方法并不难写,但是在实现的时候,由于把顶点的顺序计算的有一点问题,画出来的图形在移动时不是按照正弦曲线的规律,使得波动的规律看起来极不自然,后来把顶点的计算改正后,...
他们必须建模,设计和实现一个程序,该程序用图形表示加,减,乘,除和乘幂运算所给出的函数,以及除正弦,余弦,正切,余切,割线和割线外的三角函数平方根函数。 该程序必须使用图形用户界面,可以在其中指定...
图形模型--------------------------------------------------------------------------------------13 可视化模型-----------------------------------------------------------------------------------15 3.2 ...