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

Python012

java中怎样绘制正弦函数图象?,第1张

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=1i<500i++)

{

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()

}

}

}

package math

import java.awt.BorderLayout

import java.awt.Dimension

import java.awt.GridLayout

import java.awt.Toolkit

import javax.swing.ButtonGroup

import javax.swing.JButton

import javax.swing.JComboBox

import javax.swing.JFrame

import javax.swing.JLabel

import javax.swing.JPanel

import javax.swing.JRadioButton

import javax.swing.JTextField

public class UI extends JFrame

{

MyPanel mp

JPanel pl = new JPanel()

JPanel pl1 = new JPanel(),

pl2 = new JPanel(),

pl3 = new JPanel(),

pl4 = new JPanel()

JRadioButton rb1,rb2

ButtonGroup bg = new ButtonGroup()

JTextField tf = new JTextField(16)

String[] s = {"y = sin(x)", "y = cos(x)", "y = tan(x)",

"y = pow(x, 2)", "y = pow(x, 3)", "y = log(x)",

"y = pow(2, x)", "y = sqrt(x)", "r = a(sita)"}

JComboBox cb

JButton bn1 = new JButton("变宽"),

bn2 = new JButton("变窄"),

bn3 = new JButton("拉长"),

bn4 = new JButton("压短"),

bn = new JButton("绘图"),

exit = new JButton("退出"),

bn5 = new JButton("左移"),

bn6 = new JButton("右移"),

bn7 = new JButton("上移"),

bn8 = new JButton("下移")

public UI()

{

mp = new MyPanel(this)

pl1.setLayout(new GridLayout(1, 2))

pl2.setLayout(new GridLayout(1, 2))

pl3.setLayout(new GridLayout(1, 2))

pl4.setLayout(new GridLayout(1, 2))

pl1.add(bn1)bn1.setEnabled(false)

pl1.add(bn2)bn2.setEnabled(false)

pl2.add(bn3)bn3.setEnabled(false)

pl2.add(bn4)bn4.setEnabled(false)

pl3.add(bn5)bn5.setEnabled(false)

pl3.add(bn6)bn6.setEnabled(false)

pl4.add(bn7)bn7.setEnabled(false)

pl4.add(bn8)bn8.setEnabled(false)

pl.setLayout(new GridLayout(20, 1))

rb1 = new JRadioButton("输入函数")

rb2 = new JRadioButton("选择已有函数")

rb2.setSelected(true)

tf.setEnabled(false)

bg.add(rb1)bg.add(rb2)

rb1.addActionListener(mp)

rb2.addActionListener(mp)

pl.add(rb1)

pl.add(tf)

pl.add(rb2)

cb = new JComboBox(s)

pl.add(cb)

pl.add(new JLabel())

pl.add(pl1)pl.add(pl2)

pl.add(pl3)pl.add(pl4)

pl.add(bn)

pl.add(exit)

bn1.addActionListener(mp)

bn2.addActionListener(mp)

bn3.addActionListener(mp)

bn4.addActionListener(mp)

bn5.addActionListener(mp)

bn6.addActionListener(mp)

bn7.addActionListener(mp)

bn8.addActionListener(mp)

bn.addActionListener(mp)

exit.addActionListener(mp)

this.setLayout(new BorderLayout())

this.add(mp, BorderLayout.CENTER)

this.add(pl, BorderLayout.EAST)

this.setTitle("平面直角坐标系画图小工具")

this.setSize(797, 600 + 37)

Dimension dn = Toolkit.getDefaultToolkit().getScreenSize()

this.setLocation((dn.width - 797) / 2, (dn.height - 637) / 2)

this.setVisible(true)

this.setDefaultCloseOperation(3)

}

public static void main(String[] args)

{

new UI()

}

}

package math

import java.awt.Color

import java.awt.Graphics

import java.awt.Graphics2D

import java.awt.Point

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import java.awt.event.MouseEvent

import java.awt.event.MouseMotionListener

import java.awt.geom.Ellipse2D

import java.awt.geom.Line2D

import javax.swing.JOptionPane

import javax.swing.JPanel

public class MyPanel extends JPanel implements ActionListener,MouseMotionListener

{

UI ui

int flag

double h_times

int w_times

int dx

int dy

String str

Point pt = new Point(0, 0)

void init()

{

flag = -1

h_times = Math.PI / 100

w_times = 100

dx = 300

dy = 300

}

public MyPanel(UI ui)

{

this.addMouseMotionListener(this)

init()

this.ui = ui

}

public void paintComponent(Graphics g)

{

super.paintComponent(g)

Graphics2D g2 = (Graphics2D)g

drawCoordinate(g2)

Line2D line

g2.setColor(Color.BLUE)

g2.drawString("(" + (pt.x - 300) + ", " + (300 - pt.y) + ")", pt.x + 20, pt.y + 20)

switch(flag)

{

case 0:

g2.drawString("y = Asin(Bx + C) + D", 105, 60)

for(double i = 0i <600i += 0.01)

{

line = new Line2D.Double(i, dy - Math.sin(getReal_X(i)) * w_times, i + 1, dy - Math.sin(getReal_X(i + 1)) * w_times)

g2.draw(line)

}

break

case 1:

g2.drawString("y = Acos(Bx + C) + D", 105, 60)

for(double i = 0i <600i += 0.01)

{

line = new Line2D.Double(i, dy - Math.cos(getReal_X(i)) * w_times, i + 1, dy - Math.cos(getReal_X(i + 1)) * w_times)

g2.draw(line)

}

break

case 2:

g2.drawString("y = Atan(Bx + C) + D", 105, 60)

for(double i = 0i <600i += 0.01)

{

line = new Line2D.Double(i, dy - Math.tan(getReal_X(i)) * w_times, i + 1, dy - Math.tan(getReal_X(i + 1)) * w_times)

g2.draw(line)

}

break

case 3:

g2.drawString("y = Apow(Bx + C, 2) + D", 105, 60)

for(double i = 0i <600i += 0.01)

{

line = new Line2D.Double(i, dy - Math.pow(getReal_X(i), 2) * w_times, i + 1, dy - Math.pow(getReal_X(i + 1), 2) * w_times)

g2.draw(line)

}

break

case 4:

g2.drawString("y = Apow(Bx + C, 3) + D", 105, 60)

for(double i = 0i <600i += 0.01)

{

line = new Line2D.Double(i, dy - Math.pow(getReal_X(i), 3) * w_times, i + 1, dy - Math.pow(getReal_X(i + 1), 3) * w_times)

g2.draw(line)

}

break

case 5:

g2.drawString("y = Alog(Bx + C) + D", 105, 60)

for(double i = 0i <600i += 0.01)

{

line = new Line2D.Double(i, dy - Math.log(getReal_X(i)) * w_times, i + 1, dy - Math.log(getReal_X(i + 1)) * w_times)

g2.draw(line)

}

break

case 6:

g2.drawString("y = Apow(2, Bx + C) + D", 105, 60)

for(double i = 0i <600i += 0.01)

{

line = new Line2D.Double(i, dy - Math.pow(2, getReal_X(i)) * w_times, i + 1, dy - Math.pow(2, getReal_X(i + 1)) * w_times)

g2.draw(line)

}

break

case 7:

g2.drawString("y = Asqrt(Bx + C) + D", 105, 60)

for(double i = 0i <600i += 0.01)

{

line = new Line2D.Double(i, dy - Math.sqrt(getReal_X(i)) * w_times, i + 1, dy - Math.sqrt(getReal_X(i + 1)) * w_times)

g2.draw(line)

}

break

case 8:

g2.drawString("y = a(sita)", 105, 60)

for(double i = 0i <600i += 0.01)

{

line = new Line2D.Double(getReal_X(i) * Math.cos(getReal_X(i)), dy - getReal_X(i) * Math.sin(getReal_X(i)) * w_times, getReal_X(i) * Math.cos(getReal_X(i + 1)), dy - getReal_X(i) * Math.sin(getReal_X(i + 1)) * w_times)

g2.draw(line)

}

break

}

if(flag != -1)

{

g2.drawString("A = " + w_times, 105, 90)

g2.drawString("B= " + h_times, 105, 120)

g2.drawString("C= " + (300 - dx), 105, 150)

g2.drawString("D= " + (300 - dy), 105, 180)

}

}

private double getReal_X(double x)

{

return (x - dx) * h_times

}

private void drawCoordinate(Graphics2D g2)

{

int len = 20

Line2D line

for(int i = 0i <= 600 / leni++)

{

g2.setColor(Color.PINK.darker())

if(i == 300 / len)

g2.setColor(Color.RED)

else

line = new Line2D.Double(0, i * len, 600, i * len)

g2.draw(line)

line = new Line2D.Double(i * len, 0, i * len, 600)

g2.draw(line)

}

drawPoint(g2, 300, 300)

}

private void drawPoint(Graphics2D g2, double x, double y)

{

g2.setColor(Color.YELLOW)

Ellipse2D circle = new Ellipse2D.Double(x - 2, y - 2, 4, 4)

g2.fill(circle)

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource() == ui.rb1)

{

ui.tf.setEnabled(true)

ui.cb.setEnabled(false)

flag = -1

}

if(e.getSource() == ui.rb2)

{

ui.tf.setEnabled(false)

ui.cb.setEnabled(true)

}

if(e.getSource() == ui.bn1)

{

h_times /= 1.1

}

if(e.getSource() == ui.bn2)

{

h_times *= 1.1

}

if(e.getSource() == ui.bn3)

{

// ui.bn4.setEnabled(true)

w_times += 10

// if(w_times >= 300)

// ui.bn3.setEnabled(false)

}

if(e.getSource() == ui.bn4)

{

// ui.bn3.setEnabled(true)

w_times -= 10

// if(w_times <= 0)

// ui.bn4.setEnabled(false)

}

if(e.getSource() == ui.bn5)

{

dx -= 10

}

if(e.getSource() == ui.bn6)

{

dx += 10

}

if(e.getSource() == ui.bn7)

{

// ui.bn8.setEnabled(true)

dy -= 10

// if(dy <= 0)

// ui.bn7.setEnabled(false)

}

if(e.getSource() == ui.bn8)

{

// ui.bn7.setEnabled(true)

dy += 10

// if(dy >= 600)

// ui.bn8.setEnabled(false)

}

if(e.getSource() == ui.bn)

{

if(ui.tf.isEnabled() == true)

{

str = ui.tf.getText()

if(str == null || str.length() == 0)

{

ui.bn1.setEnabled(false)

ui.bn2.setEnabled(false)

ui.bn3.setEnabled(false)

ui.bn4.setEnabled(false)

ui.bn5.setEnabled(false)

ui.bn6.setEnabled(false)

ui.bn7.setEnabled(false)

ui.bn8.setEnabled(false)

JOptionPane.showMessageDialog(this, "请输入函数表达式 !")

return

}

}else flag = -2

ui.bn1.setEnabled(true)

ui.bn2.setEnabled(true)

ui.bn3.setEnabled(true)

ui.bn4.setEnabled(true)

ui.bn5.setEnabled(true)

ui.bn6.setEnabled(true)

ui.bn7.setEnabled(true)

ui.bn8.setEnabled(true)

init()

if(ui.cb.isEnabled() == true)

{

flag = ui.cb.getSelectedIndex()

}

}

if(e.getSource() == ui.exit)

System.exit(0)

repaint()

}

public void mouseDragged(MouseEvent arg0)

{

}

public void mouseMoved(MouseEvent e)

{

pt = e.getPoint()

repaint()

}

}