終於用java畫出數據圖了!!
我先定義一個物件讓幫我讀檔 把數據存到矩陣中,
在用awt套件的Graphics2D畫圖,
畫數據圖的時候是以每一個點去畫一個1x1的矩形,
因為他沒有point的圖形可以用 所以用矩形代替,
畫的時候要呼叫物件讀取數據,
然後會用到try{__}catch (IOException e) {__},
我目前還不太懂這東西。。。因為eclipse幫忙除錯就用了它 哈哈哈
用java畫數據圖好麻煩。。。。。。。。
附上程式碼
----------------------------------------------------------------------------------------------
package factorial;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.awt.geom.*;
public class Awt01 extends Frame {
static Awt01 frm = new Awt01();
static Graphics2D g2;
public static void main(String[] args) throws IOException {
Awt01 aa = new Awt01();
aa.bbb();
}
public double[] bbb() throws IOException {
frm.setTitle("Awt01");
frm.setSize(500, 600);
frm.setVisible(true);
FileReader fr = new FileReader("test.txt");
BufferedReader br = new BufferedReader(fr);
String line, tempstring;
String[] tempArray = new String[2];
ArrayList<String> myList = new ArrayList<String>();
int i = 0;
while ((line = br.readLine()) != null) {
// br.readLine()是讀取txt檔的每一行資料,把讀到的資料存到line
// 再將line丟給tempstring去儲存
tempstring = line;
// tempstring.split("\\s") 會依照空白鍵來切割,有幾個空白就切幾次
tempArray = tempstring.split("\\s");
// 按照順序.一行一行的儲存到動態陣列裡面
for (i = 0; i < tempArray.length; i++) {
myList.add(tempArray[i]);
}
}
// tempArray[]是String,一維矩陣
/*
* 轉換成double的二維矩陣 int k = myList.size() / 2; int count = 0; double[][]
* trans_array = new double[k][2]; for (int x = 0; x < myList.size() /
* 2; x++) { for (int y = 0; y < 2; y++) { trans_array[x][y] =
* Double.parseDouble((String) myList.get(count)); count++; //
* 一個index來決定myList讀取值的位置 } }
**/
// 轉換成double的一維矩陣
int k = myList.size();
int count = 0;
double[] trans_array = new double[k];
for (int x = 0; x < myList.size(); x++) {
trans_array[x] = Double.parseDouble((String) myList.get(count));
count++;
}
System.out.println(trans_array[3]);
return trans_array;
}
public void paint(Graphics g) {
Awt01 aa = new Awt01();
Graphics2D g2 = (Graphics2D) g;
Line2D linex = new Line2D.Double(0, frm.getHeight() * 5 / 6, frm.getWidth(), frm.getHeight() * 5 / 6);
Line2D liney = new Line2D.Double(frm.getWidth() / 6, 0, frm.getWidth() / 6, frm.getHeight());
g2.draw(linex);
g2.draw(liney);
try {
double[] wtf = aa.bbb();
int x1, y1;
double[] wtf2 = new double[3200];
System.out.println(wtf[50]+frm.getWidth() / 6);
for (int t = 0; t <= 1599; t++) {
y1 = t * 2;
x1 = t * 2 + 1;
wtf2[x1] = wtf[x1]* 24+frm.getWidth() / 6;
wtf2[y1] = wtf[y1]* -180+frm.getHeight() * 5 / 6;
Rectangle2D.Double r1 = new Rectangle2D.Double(wtf2[x1], wtf2[y1], 1, 1);
g2.draw(r1);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
沒有留言:
張貼留言