public class Test3 {
成都創(chuàng)新互聯(lián)主營(yíng)富蘊(yùn)網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,app軟件定制開(kāi)發(fā),富蘊(yùn)h5小程序開(kāi)發(fā)搭建,富蘊(yùn)網(wǎng)站營(yíng)銷推廣歡迎富蘊(yùn)等地區(qū)企業(yè)咨詢
public static void main(String[] args) {
TriFunc tri = new TriFunc();
// 生成一塊25×100的畫(huà)布
Canvas canvas = new Canvas(25, 100);
// 畫(huà)sin曲線,周期為2
tri.drawSin(canvas, 2.0);
canvas.printCanvas();
System.out.println();
canvas.reset();
// 畫(huà)cos曲線,周期為2
tri.drawCos(canvas, 2.0);
canvas.printCanvas();
}
}
class TriFunc {
/**
* 畫(huà)sin曲線
* @param canvas 畫(huà)布
* @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++) {
// 將數(shù)組索引映射為橫坐標(biāo)值
double k = (i - canvas.getWidth() / 2) * xRatio;
// 將sin值映射為數(shù)組索引
int h = yMulti - (int)Math.round(Math.sin(k) * yMulti);
chars[h][i] = Canvas.FILL_CHAR;
}
}
/**
* 畫(huà)cos曲線
* @param canvas 畫(huà)布
* @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 = ' ';
/**
* 構(gòu)建一塊畫(huà)布
* @param height
* @param width
*/
public Canvas(int height, int width) {
// 由于需要畫(huà)坐標(biāo)軸,所以得采用奇數(shù)
this.height = height % 2 == 0 ? height + 1 : height;
this.width = width % 2 == 0 ? width + 1 : width;
init();
}
/**
* 初始化畫(huà)布
*/
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();
}
/**
* 添加坐標(biāo)軸
*/
private void addAxis() {
// 添加橫坐標(biāo)
int y = height / 2;
for(int x = 0; x width; x++) {
canvas[y][x] = '-';
}
// 添加縱坐標(biāo)
int xx = width / 2;
for(int yy = 0; yy height; yy++) {
canvas[yy][xx] = '|';
}
// 添加原點(diǎn)
canvas[y][xx] = '+';
}
/**
* 輸出畫(huà)布
*/
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();
}
}
/**
* 清空畫(huà)布
*/
public void reset() {
init();
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
public char[][] getCanvas() {
return canvas;
}
}
來(lái)自CSDN 樓主可以去看看。。。。我是蝸牛。。。嘿嘿
代碼如下,只是時(shí)間倉(cāng)促有些簡(jiǎn)陋,沒(méi)有坐標(biāo)軸,而且大小比例問(wèn)題也沒(méi)有調(diào)好。不過(guò)功能實(shí)現(xiàn)了。嘎嘎,新手上路,騰云駕霧。
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("畫(huà)曲線");
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; //這里填寫(xiě)公式
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();???//用來(lái)接收笛卡爾積
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);
}
}