绘制正弦曲线:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.lang.*;
public class sinx {
public static void main(String[] args) {
DrawFrame frame = new DrawFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class DrawFrame extends JFrame {
public DrawFrame() {
//设置标题,窗口大小
setTitle("sinx");
setSize(WIDTH, HEIGHT);
DrawPanel panel = new DrawPanel();
Container contentPane = getContentPane();
contentPane.add(panel);
}
public static final int WIDTH = 400;
public static final int HEIGHT = 400;
}
class DrawPanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
int x,y;
double a;
//画正弦曲线
//Graphics g=getGraphics();
for(x=0;x{
a=Math.sin(x*Math.PI/180);
y=(int)(80+40*a);
g2.drawString("*",x,y);
}
}
}
写得比较简单哈。
package OnlineUserCount;
import java.awt.*;
import javax.swing.*;
public class Sin extends JPanel{
private double x;
private double y;
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
super.paintComponent(g);
g.setColor(Color.WHITE);//设置面板背景色
g.fillRect(0, 0, 400, 300);//填充面板
g.setColor(Color.RED);//设置画线的颜色
for(x=0;x
{
y=Math.sin(x*Math. PI/180);//转化为弧度,1度=π/180弧度
y=(100+80*y);//便于在屏幕上显示
//g.drawString(".",(int)x,(int)y);//用这种方式也可以
g.drawLine((int)x, (int)y, (int)x,(int) y);//画点
}
}
public static void main(String []args){
Sin s= new Sin();
JFrame j=new JFrame();
j.setTitle("一个周期的正弦曲线");
j.add(s);
j.setSize(400, 300);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setVisible(true);
}
}
//效果截图
以上是使用Java设计并实现一个应用程序绘制以下函数的图像:的详细内容。更多信息请关注PHP中文网其他相关文章!