「java画正弦曲线」autocad画正弦曲线

博主:adminadmin 2022-11-25 12:21:06 65

本篇文章给大家谈谈java画正弦曲线,以及autocad画正弦曲线对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

用java语言绘制正弦曲线

很简单,程序写给你,给分:

---------------------------------------------

import java.awt.*;

import javax.swing.JFrame;

import java.util.Random;

import java.text.DecimalFormat;

public class SinDemo extends JFrame {

private double cx = 1, cy = 1;

private double toCx = 1, toCy = 1;

private Random rnd = new Random();

private DecimalFormat df = new DecimalFormat("0.00");

private SinDemo () {

super("Sin-Demo");

setSize(600, 600);

setLocationRelativeTo(null);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

run();

}

private int translateX (double x) {

return (int)(x*getWidth()/Math.PI/4 + getWidth()/2);

}

private int translateY (double y) {

return (int)(getHeight()/2 - y*getWidth()/Math.PI/4);

}

private double sin (double x) {

return (cy * Math.sin(cx * x));

}

@Override

public void paint (Graphics g) {

super.paint(g);

g.setColor(Color.BLUE);

g.drawString("y = " + df.format(cx) + " * sin( " + df.format(cy) + " * x)", 50, 50);

g.setColor(Color.LIGHT_GRAY);

g.drawLine(0, getHeight()/2, getWidth(), getHeight()/2);

g.drawLine(getWidth()/2, 0, getWidth()/2, getHeight());

g.setColor(Color.BLACK);

for (double i=-Math.PI/cx; iMath.PI/cx; i+=0.1)

g.drawLine(translateX(i), translateY(sin(i)),

translateX(i+0.1), translateY(sin(i+0.1)));

}

public void run () {

while (true) {

if (Math.abs(cx - toCx)  0.1) {

toCx = rnd.nextDouble()*2;

toCy = rnd.nextDouble()*2;

}

cx += (toCx - cx)/50;

cy += (toCy - cy)/50;

paint(getGraphics());

try {

Thread.sleep(80);

} catch (InterruptedException ie) {}

}

}

public static void main (String args[]) {

new SinDemo();

}

}

-------------------------------------------------

再给你一个截图:

JAVA画正弦曲线

import javax.swing.*;

import javax.swing.event.*;

import java.awt.*;

import java.awt.event.*;

public class SinCos extends JFrame{

    private final int WIDTH = 900, HEIGHT = 450;//窗口默认的宽度、高度

    private final int offsetX=15;//原点的距离窗口左边空白

    private final int offsetY=5; //原点的距离窗口顶边空白

    private final int stepX=200; //横向步进

    private final int stepY=200; //纵向步进

    private final Color colBack = Color.white;     //背景颜色

    private final Color colText = Color.blue;      //文字标签颜色

    private final Color colCros = Color.DARK_GRAY; //轴颜色

    private final Color colLine = Color.red;       //线颜色

    

    private JRadioButton rdSin=new JRadioButton("Sin");

    private JRadioButton rdCos=new JRadioButton("Cos");

    private JButton btnDraws = new JButton("画线");

    private JButton btnClear = new JButton("清空");

    private boolean bDraw = false;

    

    public void paint(Graphics g){

        super.paint(g);   //让父类先处理

        int w=getWidth(); //窗口的宽度

        int h=getHeight();//窗口的高度

        

        g.setColor(colBack);    //底色用白色

        g.fillRect(0, 0, w, h);    //填充整个窗口

        rdSin.repaint();

        rdCos.repaint();

        btnDraws.repaint();

        btnClear.repaint();

        if(!bDraw){

            return ;

        }

        

        int mid_y=(h-offsetY)/2+10;//半高

        g.setFont(g.getFont().deriveFont(10f));//字体大小

        g.setColor(colCros);        //横线和竖线,用深灰色

        g.drawLine(0, offsetY+mid_y, w, mid_y+offsetY);        //横线中线

        g.drawLine(offsetX, offsetY, offsetX, h);    //竖线中线

        

        g.setColor(colText); //刻度用蓝色

        int maxX=(int)Math.round( (w-offsetX) / stepX);//计算一下横向最大刻度

        for(int i=0;i=maxX;i++){

            g.drawLine(offsetX+stepX*i, offsetY+mid_y-5, offsetX+stepX*i, offsetY+mid_y+5);//横线刻度,90步进,方便后面画线的计算

            g.drawString(String.valueOf(90*i), offsetX+stepX*i-5, offsetY+mid_y+10+5); //刻度是90度

        }

        

        int maxY=(int)Math.round( mid_y / stepY);//计算一下纵向最大刻度

        for(int i=1;i=maxY;i++){

            g.drawLine(offsetX-5, offsetY+mid_y-stepY*i, offsetX+5, offsetY+mid_y-stepY*i);//竖线正刻度,100步进

            g.drawString(String.valueOf(stepY*i), offsetX+10, offsetY+mid_y-stepY*i+5);

            g.drawLine(offsetX-5, offsetY+mid_y+stepY*i, offsetX+5, offsetY+mid_y+stepY*i);//竖线负刻度,100步进

            g.drawString(String.valueOf(-stepY*i), offsetX+10, offsetY+mid_y+stepY*i+5);

        }

        

        g.setColor(colLine);   //曲线用红色

        int x1, y1, x_=-1,y_=0;

        for(int i=0; i=w; i++){ //从0度到窗口宽度,开始画Sin()点

            x1=((Double)(offsetX+i/90.0*stepX) ).intValue();

            if(rdSin.isSelected()){

                y1=offsetY+((Double)( mid_y+stepY*Math.sin( Math.toRadians(i) )) ).intValue();

            }else{

                y1=offsetY+((Double)( mid_y+stepY*Math.cos( Math.toRadians(i) )) ).intValue();

            }

            if(x_==-1){

                x_=x1;y_=y1;

            }

            g.drawLine(x_, y_, x1, y1);

            x_=x1;y_=y1;

        }

    }

    public SinCos(){

        super("测试Graphics+Sin/Cos");

        this.setSize(WIDTH, HEIGHT);

        this.setLayout(null);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        

        ButtonGroup group = new ButtonGroup();

        group.add(rdSin);

        group.add(rdCos);

        this.getContentPane().add(rdSin);

        this.getContentPane().add(rdCos);

        this.getContentPane().add(btnDraws);

        this.getContentPane().add(btnClear);

        this.setVisible(true);

        this.doLayout();

        btnDraws.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){

                bDraw=true; SinCos.this.repaint();

            }

        });

        btnClear.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){

                bDraw=false;SinCos.this.repaint();

            }

        });

        rdSin.setSelected(true);

    }

    public void doLayout(){

        super.doLayout();

        rdSin.setBounds(10, 15, 50, 20);

        rdCos.setBounds(rdSin.getX()+rdSin.getWidth()+5, 15, 50, 20);

        btnDraws.setBounds(rdCos.getX()+rdCos.getWidth()+5, 12, 70, 25);

        btnClear.setBounds(btnDraws.getX()+btnDraws.getWidth()+3, 12, 70, 25);

    }

    

    public static void main(String[] args){

        EventQueue.invokeLater(new Runnable()  {

            public void run()   {

                new SinCos();

            }

        });

    }

}

java中怎样绘制正弦函数图象?

import java.awt.*;

import java.applet.*;

import java.math.*;

import java.awt.event.*;

public class dffg extends Applet implements ActionListener

{

Button bb,bn;

TextField tt;

int aa;

public void init()

{

bb=new Button("画图");

bn=new Button("清除重画");

tt=new TextField(5);

add(bb);

add(tt);

add(bn);

bb.addActionListener(this);

bn.addActionListener(this);

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==bb)

{

int aa=Integer.parseInt(tt.getText());

Graphics g=getGraphics();

for(int i=1;i500;i++)

{

int x=i;

int y=(int)(Math.sin(aa*x*3.14/181)*100+150);

g.drawString("s",x,y);

}

}

if(e.getSource()==bn)

{

repaint();

}

}

}

java编程绘制正弦曲线是什么?

写得比较简单哈。

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=360;x+=0.1)//一个周期

{

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编程绘制正弦曲线。

写得比较简单哈。

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=360;x+=0.1)//一个周期

{

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画正弦曲线的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于autocad画正弦曲线、java画正弦曲线的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-25,除非注明,否则均为首码项目网原创文章,转载请注明出处。