大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
public class Test3 {
成都创新互联主营富蕴网站建设的网络公司,主营网站建设方案,app软件定制开发,富蕴h5小程序开发搭建,富蕴网站营销推广欢迎富蕴等地区企业咨询
public static void main(String[] args) {
TriFunc tri = new TriFunc();
// 生成一块25×100的画布
Canvas canvas = new Canvas(25, 100);
// 画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;
}
}
来自CSDN 楼主可以去看看。。。。我是蜗牛。。。嘿嘿
代码如下,只是时间仓促有些简陋,没有坐标轴,而且大小比例问题也没有调好。不过功能实现了。嘎嘎,新手上路,腾云驾雾。
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Bbso extends JPanel{
int x,y,x1,y1,m=100;
double d;
public Bbso() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(100,100,500,300);
f.setTitle("画曲线");
f.setVisible(true);
f.getContentPane().add(this);
}
public static void main(String arg[]) {
new Bbso();
}
public void paint(Graphics g) {
super.paintComponent(g);
x1=0;
y1=0;
for(x=-250;x250;x++) {
d=-0.2045*x*x+100.41*x-6736.8; //这里填写公式
y=(int)d;
g.drawLine(x1,y1+m,x,y+m);
x1=x;
y1=y;
}
}
}
ListString list1=new ArrayListString();
ListString list2=new ArrayListString();
ListString list3=new ArrayListString(); //用来接收笛卡尔积
list1.add("str1");
list1.add("str2");
list1.add("str3");
list2.add("aaa");
list2.add("bbb");
for(String value1 : list1)
{
for(String value2 : list2)
{
String temp=value1+","+value2;
list3.add(temp);
}
}