java 怎么画一个曲线? java 怎么画一个曲线? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.applet.Applet;import java.awt.*;import java.awt.event.*;public class Polylines extends Applet { private static Color[] colors = { Color.white, Color.black, Color.blue, Color.red, Color.yellow, Color.orange, Color.cyan, Color.pink, Color.magenta, Color.green }; public void init() { Button button = new Button("repaint"); add(button); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { Polylines.this.repaint(); } }); } public void paint(Graphics g) { int arraySize = ((int)(Math.random()*100)); int[] xPoints = new int[arraySize]; int[] yPoints = new int[arraySize]; for(int i=0; i < xPoints.length; ++i) { xPoints[i] = ((int)(Math.random()*200)); yPoints[i] = ((int)(Math.random()*200)); } g.setColor(colors[(int)(Math.random()*10)]); g.drawPolyline(xPoints, yPoints, xPoints.length); showStatus(arraySize + " points"); } } 原来帮 CSDN 上一个人写的,呵呵public class Test { public static void main(String[] args) { TriFunc tri = new TriFunc(); // 生成一块25×100的画布 Canvas canvas = new Canvas(25, 120); // 画sin曲线,周期为2 tri.drawSin(canvas, 2.0); canvas.printCanvas(); System.out.println(); canvas.reset(); // 画cos曲线,周期为2 tri.drawCos(canvas, 2.0); canvas.printCanvas(); }}class TriFunc { /** * 画sin曲线 * @param canvas 画布 * @param period 曲线周期 */ public void drawSin(Canvas canvas, double period) { char[][] chars = canvas.getCanvas(); // x 轴的比率 double xRatio = (2 * period * Math.PI) / (canvas.getWidth() - 1); // y 轴的放大倍率 int yMulti = (canvas.getHeight() - 1) / 2; for(int i = 0; i < canvas.getWidth(); i++) { // 将数组索引映射为横坐标值 double k = (i - canvas.getWidth() / 2) * xRatio; // 将sin值映射为数组索引 int h = yMulti - (int)Math.round(Math.sin(k) * yMulti); chars[h][i] = Canvas.FILL_CHAR; } } /** * 画cos曲线 * @param canvas 画布 * @param period 曲线周期 */ public void drawCos(Canvas canvas, double period) { char[][] chars = canvas.getCanvas(); double xRatio = (2 * period * Math.PI) / (canvas.getWidth() - 1); int yMulti = (canvas.getHeight() - 1) / 2; for(int i = 0; i < canvas.getWidth(); i++) { double k = (i - canvas.getWidth() / 2) * xRatio; int h = yMulti - (int)Math.round(Math.cos(k) * yMulti); chars[h][i] = Canvas.FILL_CHAR; } }}class Canvas { private int height; private int width; private char[][] canvas; // 填充字符 public static char FILL_CHAR = '+'; // 空白字符 public static char BLANK_CHAR = ' '; /** * 构建一块画布 * @param height * @param width */ public Canvas(int height, int width) { // 由于需要画坐标轴,所以得采用奇数 this.height = height % 2 == 0 ? height + 1 : height; this.width = width % 2 == 0 ? width + 1 : width; init(); } /** * 初始化画布 */ private void init() { this.canvas = new char[height][width]; for(int i = 0; i < height; i++) { for(int j = 0; j < width; j++) { canvas[i][j] = BLANK_CHAR; } } addAxis(); } /** * 添加坐标轴 */ private void addAxis() { // 添加横坐标 int y = height / 2; for(int x = 0; x < width; x++) { canvas[y][x] = '-'; } // 添加纵坐标 int xx = width / 2; for(int yy = 0; yy < height; yy++) { canvas[yy][xx] = '|'; } // 添加原点 canvas[y][xx] = '+'; } /** * 输出画布 */ public void printCanvas() { for(int i = 0; i < height; i++) { for(int j = 0; j < width; j++) { System.out.print(canvas[i][j]); } System.out.println(); } } /** * 清空画布 */ public void reset() { init(); } public int getHeight() { return height; } public int getWidth() { return width; } public char[][] getCanvas() { return canvas; } } 长见识了!我还不知道原来java还有这功能! 3楼的吧问题说的太专业了。尤其我这样的数学白痴根本不懂什么Sin Cos 之类的 贝塞尔曲线的算法代码:http://www.math.ucla.edu/~baker/java/hoefer/Beziersrc.htm我是不懂。 要在Applet中实现,而且过程很烦琐 用java 画几个函数曲线public class Test { public static void main(String[] args) { TriFunc tri = new TriFunc(); // 生成一块25×100的画布 Canvas canvas = new Canvas(25, 120); // 画sin曲线,周期为2 tri.drawSin(canvas, 2.0); canvas.printCanvas(); System.out.println(); canvas.reset(); // 画cos曲线,周期为2 tri.drawCos(canvas, 2.0); canvas.printCanvas(); }}class TriFunc { /** * 画sin曲线 * @param canvas 画布 * @param period 曲线周期 */ public void drawSin(Canvas canvas, double period) { char[][] chars = canvas.getCanvas(); // x 轴的比率 double xRatio = (2 * period * Math.PI) / (canvas.getWidth() - 1); // y 轴的放大倍率 int yMulti = (canvas.getHeight() - 1) / 2; for(int i = 0; i < canvas.getWidth(); i++) { // 将数组索引映射为横坐标值 double k = (i - canvas.getWidth() / 2) * xRatio; // 将sin值映射为数组索引 int h = yMulti - (int)Math.round(Math.sin(k) * yMulti); chars[h][i] = Canvas.FILL_CHAR; } } /** * 画cos曲线 * @param canvas 画布 * @param period 曲线周期 */ public void drawCos(Canvas canvas, double period) { char[][] chars = canvas.getCanvas(); double xRatio = (2 * period * Math.PI) / (canvas.getWidth() - 1); int yMulti = (canvas.getHeight() - 1) / 2; for(int i = 0; i < canvas.getWidth(); i++) { double k = (i - canvas.getWidth() / 2) * xRatio; int h = yMulti - (int)Math.round(Math.cos(k) * yMulti); chars[h][i] = Canvas.FILL_CHAR; } }}class Canvas { private int height; private int width; private char[][] canvas; // 填充字符 public static char FILL_CHAR = '+'; // 空白字符 public static char BLANK_CHAR = ' '; /** * 构建一块画布 * @param height * @param width */ public Canvas(int height, int width) { // 由于需要画坐标轴,所以得采用奇数 this.height = height % 2 == 0 ? height + 1 : height; this.width = width % 2 == 0 ? width + 1 : width; init(); } /** * 初始化画布 */ private void init() { this.canvas = new char[height][width]; for(int i = 0; i < height; i++) { for(int j = 0; j < width; j++) { canvas[i][j] = BLANK_CHAR; } } addAxis(); } /** * 添加坐标轴 */ private void addAxis() { // 添加横坐标 int y = height / 2; for(int x = 0; x < width; x++) { canvas[y][x] = '-'; } // 添加纵坐标 int xx = width / 2; for(int yy = 0; yy < height; yy++) { canvas[yy][xx] = '|'; } // 添加原点 canvas[y][xx] = '+'; } /** * 输出画布 */ public void printCanvas() { for(int i = 0; i < height; i++) { for(int j = 0; j < width; j++) { System.out.print(canvas[i][j]); } System.out.println(); } } /** * 清空画布 */ public void reset() { init(); } public int getHeight() { return height; } public int getWidth() { return width; } public char[][] getCanvas() { return canvas; } } //获取指定的进程名System.Diagnostics.Process[] prProcesses = System.Diagnostics.Process.GetProcessesByName(Application.ProductName);//如果获取的进程名多于一个(本身也有一个嘛~)if (prProcesses.Length > 1){ MessageBox.Show("已启动过了一个实例!");//这句话可以换成弹出原窗口的代码。 //退出 return; } import java.awt.*;import javax.swing.*;public class MyDrawing extends JApplet{ public void init() { DrawPanel p=new DrawPanel(); setLayout(new BorderLayout()); add(p,BorderLayout.CENTER); } public static void main(String[] args) { JFrame frame=new JFrame(); frame.setTitle("我的曲线"); MyDrawing dr=new MyDrawing(); frame.add(dr); dr.init(); dr.start(); frame.setSize(350,200); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}class DrawPanel extends JPanel{ public void paintComponent(Graphics g) { super.paintComponents(g); int x=getWidth()/3; int y=getHeight()/5; int x1=10,x2=30,x3=10,x4=120;//设置4个点的横坐标x int y1=40,y2=80,y3=120,y4=10;//设置4个点的纵坐标y //将四个点连起来 g.drawLine(x1,y1,x2,y2); g.drawLine(x2, y2, x3, y3); g.drawLine(x3, y3, x4, y4); }} 楼主可以研究一下Arc2D这个类,是画弧线的 自己做不如用jfreechart,既方便快速,又美观。 import java.applet.Applet;import java.awt.*;import java.awt.event.*;public class Polylines extends Applet { private static Color[] colors = { Color.white, Color.black, Color.blue, Color.red, Color.yellow, Color.orange, Color.cyan, Color.pink, Color.magenta, Color.green }; public void init() { Button button = new Button("repaint"); add(button); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { Polylines.this.repaint(); } }); } public void paint(Graphics g) { int arraySize = ((int)(Math.random()*100)); int[] xPoints = new int[arraySize]; int[] yPoints = new int[arraySize]; for(int i=0; i < xPoints.length; ++i) { xPoints[i] = ((int)(Math.random()*200)); yPoints[i] = ((int)(Math.random()*200)); } g.setColor(colors[(int)(Math.random()*10)]); g.drawPolyline(xPoints, yPoints, xPoints.length); showStatus(arraySize + " points"); } } 这个应该才是正解。如果使用画点的方法,画sin cos还行。如果画tan和ctg,就会在PI/2的左右有明显的断点。 不就是graphics2D 想画什么画什么 急求解决java万年历问题 一道关于protected修饰符的面试题 请问下JAVA可以读取RS485串口协议的数据吗? 如何实现窗口间的参数传递 做文字动画的时候,如何让图像不残留阿?急!急! 请教一道笔试题 请教:如何下载Wrox出版社书中的代码? 什么方法可以获取JRadioButton的值?? 我用Java写的Applet为什么不能显示中文? 有哪个函数能把字符串转化成 int? JFileChooser获得的路径问题 为什么yahoo.cn邮箱登录出错后跳转到英文界面?
import java.awt.*;
import java.awt.event.*;public class Polylines extends Applet {
private static Color[] colors = {
Color.white, Color.black, Color.blue, Color.red,
Color.yellow, Color.orange, Color.cyan, Color.pink,
Color.magenta, Color.green }; public void init() {
Button button = new Button("repaint");
add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Polylines.this.repaint();
}
});
}
public void paint(Graphics g) {
int arraySize = ((int)(Math.random()*100));
int[] xPoints = new int[arraySize];
int[] yPoints = new int[arraySize];
for(int i=0; i < xPoints.length; ++i) {
xPoints[i] = ((int)(Math.random()*200));
yPoints[i] = ((int)(Math.random()*200));
}
g.setColor(colors[(int)(Math.random()*10)]);
g.drawPolyline(xPoints, yPoints, xPoints.length);
showStatus(arraySize + " points");
}
}
public static void main(String[] args) {
TriFunc tri = new TriFunc();
// 生成一块25×100的画布
Canvas canvas = new Canvas(25, 120); // 画sin曲线,周期为2
tri.drawSin(canvas, 2.0);
canvas.printCanvas();
System.out.println();
canvas.reset();
// 画cos曲线,周期为2
tri.drawCos(canvas, 2.0);
canvas.printCanvas();
}
}class TriFunc { /**
* 画sin曲线
* @param canvas 画布
* @param period 曲线周期
*/
public void drawSin(Canvas canvas, double period) {
char[][] chars = canvas.getCanvas();
// x 轴的比率
double xRatio = (2 * period * Math.PI) / (canvas.getWidth() - 1);
// y 轴的放大倍率
int yMulti = (canvas.getHeight() - 1) / 2;
for(int i = 0; i < canvas.getWidth(); i++) {
// 将数组索引映射为横坐标值
double k = (i - canvas.getWidth() / 2) * xRatio;
// 将sin值映射为数组索引
int h = yMulti - (int)Math.round(Math.sin(k) * yMulti);
chars[h][i] = Canvas.FILL_CHAR;
}
}
/**
* 画cos曲线
* @param canvas 画布
* @param period 曲线周期
*/
public void drawCos(Canvas canvas, double period) {
char[][] chars = canvas.getCanvas();
double xRatio = (2 * period * Math.PI) / (canvas.getWidth() - 1);
int yMulti = (canvas.getHeight() - 1) / 2;
for(int i = 0; i < canvas.getWidth(); i++) {
double k = (i - canvas.getWidth() / 2) * xRatio;
int h = yMulti - (int)Math.round(Math.cos(k) * yMulti);
chars[h][i] = Canvas.FILL_CHAR;
}
}
}
class Canvas {
private int height;
private int width;
private char[][] canvas;
// 填充字符
public static char FILL_CHAR = '+';
// 空白字符
public static char BLANK_CHAR = ' ';
/**
* 构建一块画布
* @param height
* @param width
*/
public Canvas(int height, int width) {
// 由于需要画坐标轴,所以得采用奇数
this.height = height % 2 == 0 ? height + 1 : height;
this.width = width % 2 == 0 ? width + 1 : width;
init();
}
/**
* 初始化画布
*/
private void init() {
this.canvas = new char[height][width];
for(int i = 0; i < height; i++) {
for(int j = 0; j < width; j++) {
canvas[i][j] = BLANK_CHAR;
}
}
addAxis();
}
/**
* 添加坐标轴
*/
private void addAxis() {
// 添加横坐标
int y = height / 2;
for(int x = 0; x < width; x++) {
canvas[y][x] = '-';
}
// 添加纵坐标
int xx = width / 2;
for(int yy = 0; yy < height; yy++) {
canvas[yy][xx] = '|';
}
// 添加原点
canvas[y][xx] = '+';
}
/**
* 输出画布
*/
public void printCanvas() {
for(int i = 0; i < height; i++) {
for(int j = 0; j < width; j++) {
System.out.print(canvas[i][j]);
}
System.out.println();
}
}
/**
* 清空画布
*/
public void reset() {
init();
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
} public char[][] getCanvas() {
return canvas;
}
}
http://www.math.ucla.edu/~baker/java/hoefer/Beziersrc.htm我是不懂。
public class Test {
public static void main(String[] args) {
TriFunc tri = new TriFunc();
// 生成一块25×100的画布
Canvas canvas = new Canvas(25, 120); // 画sin曲线,周期为2
tri.drawSin(canvas, 2.0);
canvas.printCanvas();
System.out.println();
canvas.reset();
// 画cos曲线,周期为2
tri.drawCos(canvas, 2.0);
canvas.printCanvas();
}
}class TriFunc { /**
* 画sin曲线
* @param canvas 画布
* @param period 曲线周期
*/
public void drawSin(Canvas canvas, double period) {
char[][] chars = canvas.getCanvas();
// x 轴的比率
double xRatio = (2 * period * Math.PI) / (canvas.getWidth() - 1);
// y 轴的放大倍率
int yMulti = (canvas.getHeight() - 1) / 2;
for(int i = 0; i < canvas.getWidth(); i++) {
// 将数组索引映射为横坐标值
double k = (i - canvas.getWidth() / 2) * xRatio;
// 将sin值映射为数组索引
int h = yMulti - (int)Math.round(Math.sin(k) * yMulti);
chars[h][i] = Canvas.FILL_CHAR;
}
}
/**
* 画cos曲线
* @param canvas 画布
* @param period 曲线周期
*/
public void drawCos(Canvas canvas, double period) {
char[][] chars = canvas.getCanvas();
double xRatio = (2 * period * Math.PI) / (canvas.getWidth() - 1);
int yMulti = (canvas.getHeight() - 1) / 2;
for(int i = 0; i < canvas.getWidth(); i++) {
double k = (i - canvas.getWidth() / 2) * xRatio;
int h = yMulti - (int)Math.round(Math.cos(k) * yMulti);
chars[h][i] = Canvas.FILL_CHAR;
}
}
}
class Canvas {
private int height;
private int width;
private char[][] canvas;
// 填充字符
public static char FILL_CHAR = '+';
// 空白字符
public static char BLANK_CHAR = ' ';
/**
* 构建一块画布
* @param height
* @param width
*/
public Canvas(int height, int width) {
// 由于需要画坐标轴,所以得采用奇数
this.height = height % 2 == 0 ? height + 1 : height;
this.width = width % 2 == 0 ? width + 1 : width;
init();
}
/**
* 初始化画布
*/
private void init() {
this.canvas = new char[height][width];
for(int i = 0; i < height; i++) {
for(int j = 0; j < width; j++) {
canvas[i][j] = BLANK_CHAR;
}
}
addAxis();
}
/**
* 添加坐标轴
*/
private void addAxis() {
// 添加横坐标
int y = height / 2;
for(int x = 0; x < width; x++) {
canvas[y][x] = '-';
}
// 添加纵坐标
int xx = width / 2;
for(int yy = 0; yy < height; yy++) {
canvas[yy][xx] = '|';
}
// 添加原点
canvas[y][xx] = '+';
}
/**
* 输出画布
*/
public void printCanvas() {
for(int i = 0; i < height; i++) {
for(int j = 0; j < width; j++) {
System.out.print(canvas[i][j]);
}
System.out.println();
}
}
/**
* 清空画布
*/
public void reset() {
init();
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
} public char[][] getCanvas() {
return canvas;
}
}
System.Diagnostics.Process[] prProcesses =
System.Diagnostics.Process.GetProcessesByName(Application.ProductName);
//如果获取的进程名多于一个(本身也有一个嘛~)
if (prProcesses.Length > 1)
{
MessageBox.Show("已启动过了一个实例!");//这句话可以换成弹出原窗口的代码。
//退出
return;
}
import java.awt.*;
import javax.swing.*;public class MyDrawing extends JApplet
{
public void init()
{
DrawPanel p=new DrawPanel();
setLayout(new BorderLayout());
add(p,BorderLayout.CENTER);
} public static void main(String[] args) {
JFrame frame=new JFrame();
frame.setTitle("我的曲线");
MyDrawing dr=new MyDrawing();
frame.add(dr);
dr.init();
dr.start();
frame.setSize(350,200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}}class DrawPanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponents(g);
int x=getWidth()/3;
int y=getHeight()/5;
int x1=10,x2=30,x3=10,x4=120;//设置4个点的横坐标x
int y1=40,y2=80,y3=120,y4=10;//设置4个点的纵坐标y
//将四个点连起来
g.drawLine(x1,y1,x2,y2);
g.drawLine(x2, y2, x3, y3);
g.drawLine(x3, y3, x4, y4); }
}
import java.awt.*;
import java.awt.event.*;public class Polylines extends Applet {
private static Color[] colors = {
Color.white, Color.black, Color.blue, Color.red,
Color.yellow, Color.orange, Color.cyan, Color.pink,
Color.magenta, Color.green }; public void init() {
Button button = new Button("repaint");
add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Polylines.this.repaint();
}
});
}
public void paint(Graphics g) {
int arraySize = ((int)(Math.random()*100));
int[] xPoints = new int[arraySize];
int[] yPoints = new int[arraySize];
for(int i=0; i < xPoints.length; ++i) {
xPoints[i] = ((int)(Math.random()*200));
yPoints[i] = ((int)(Math.random()*200));
}
g.setColor(colors[(int)(Math.random()*10)]);
g.drawPolyline(xPoints, yPoints, xPoints.length);
showStatus(arraySize + " points");
}
}
这个应该才是正解。如果使用画点的方法,画sin cos还行。
如果画tan和ctg,就会在PI/2的左右有明显的断点。