JAVA,正弦曲线的绘制,使用Graphics类,绘制正弦曲线。急需!!! 5
2个回答
2010-12-21
展开全部
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JFrame;
import javax.swing.Timer;
public class SinWave extends JFrame {
private static final int NUM = 1000;
private static final double TWOPI = 2 * 3.14;
private static final int STEP = 10;
private int[] xPoints = new int[NUM];
private int[] yPoints = new int[NUM];
private int[] yCopyPoints = new int[STEP];
private int width, height;
private Timer timer;
private boolean move;
public SinWave(boolean move) {
this.move = move;
setSize(500, 400);
this.setBackground(Color.white);
Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
width = getWidth();
height = getHeight();
this.setLocation((scrSize.width - width) / 2,
(scrSize.height - height) / 2);
for (int i = 0; i < NUM; i++) {
xPoints[i] = i * width / NUM;
yPoints[i] = (int) (height / 2 * (1 - Math.sin(TWOPI * i / NUM)));
}
setVisible(true);
timer = new Timer(200, new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < STEP; i++)
yCopyPoints[i] = yPoints[i];
for (int i = 0; i < NUM - STEP; i++)
yPoints[i] = yPoints[i + STEP];
for (int i = 0; i < STEP; i++)
yPoints[i + NUM - STEP] = yCopyPoints[i];
repaint();
}
});
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent ce) {
width = getWidth();
height = getHeight();
for (int i = 0; i < NUM; i++) {
xPoints[i] = i * width / NUM;
yPoints[i] = (int) (height / 2 * (1 - Math.sin(TWOPI * i
/ NUM)));
}
}
});
timer.start();
if (!move) {
timer.stop();
}
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.red);
g.drawLine(0, height / 2, width, height / 2);
g.setColor(Color.green);
g.drawPolyline(xPoints, yPoints, NUM);
}
public static void main(String[] args) {
SinWave sw = new SinWave(true); // 曲线是否运动
sw.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JFrame;
import javax.swing.Timer;
public class SinWave extends JFrame {
private static final int NUM = 1000;
private static final double TWOPI = 2 * 3.14;
private static final int STEP = 10;
private int[] xPoints = new int[NUM];
private int[] yPoints = new int[NUM];
private int[] yCopyPoints = new int[STEP];
private int width, height;
private Timer timer;
private boolean move;
public SinWave(boolean move) {
this.move = move;
setSize(500, 400);
this.setBackground(Color.white);
Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
width = getWidth();
height = getHeight();
this.setLocation((scrSize.width - width) / 2,
(scrSize.height - height) / 2);
for (int i = 0; i < NUM; i++) {
xPoints[i] = i * width / NUM;
yPoints[i] = (int) (height / 2 * (1 - Math.sin(TWOPI * i / NUM)));
}
setVisible(true);
timer = new Timer(200, new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < STEP; i++)
yCopyPoints[i] = yPoints[i];
for (int i = 0; i < NUM - STEP; i++)
yPoints[i] = yPoints[i + STEP];
for (int i = 0; i < STEP; i++)
yPoints[i + NUM - STEP] = yCopyPoints[i];
repaint();
}
});
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent ce) {
width = getWidth();
height = getHeight();
for (int i = 0; i < NUM; i++) {
xPoints[i] = i * width / NUM;
yPoints[i] = (int) (height / 2 * (1 - Math.sin(TWOPI * i
/ NUM)));
}
}
});
timer.start();
if (!move) {
timer.stop();
}
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.red);
g.drawLine(0, height / 2, width, height / 2);
g.setColor(Color.green);
g.drawPolyline(xPoints, yPoints, NUM);
}
public static void main(String[] args) {
SinWave sw = new SinWave(true); // 曲线是否运动
sw.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
展开全部
很简单,程序写给你,给分:
---------------------------------------------
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; i<Math.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();
}
}
-------------------------------------------------
再给你一个截图:
---------------------------------------------
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; i<Math.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();
}
}
-------------------------------------------------
再给你一个截图:
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询