[英]Draw points defined in file in Java
我正在嘗試學習Java的某些圖形方面。 我制作了一個列出這些點的文本文件,文件的第一行確定了多少點。 我需要將點放在文本文件中並將其繪制在畫布上。 我看過幾種資源,但我只是不明白所有事情如何協同工作。 我什至找不到一個提供所有代碼並解釋每個部分的位置以及如何調用它的教程。 基本上,我對JPanels,JFrames和整個過程感到困惑。 以下是我到目前為止編寫的代碼以及該文件的外觀的屏幕快照。
碼:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.Graphics.*;
import java.util.*;
import java.io.*;
public class drawPoints extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
this.setBackground(Color.WHITE);
g.setColor(Color.BLUE);
try{
FileInputStream fstream = new FileInputStream("Desktop/Assign2Test1.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
int i = 1;
while ((strLine = br.readLine()) != null){
final Point[] points = new Point[Integer.parseInt(br.readLine())];
final String[] split = strLine.split("\u0009");
points[i++] = new Point(Integer.parseInt(split[0]), Integer.parseInt(split[1]));
}
in.close();
}catch (Exception e){ System.err.println("Error: " + e.getMessage());
}
}
import javax.swing.*;
public class mainDrawPoint
{
public static void main(String args[])
{
JFrame f = new JFrame("Draw Points Application");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
drawPoints dP = new drawPoints();
f.add(dP);
f.setSize(500,500);
f.setVisible(true);
}
}
代碼要做的就是將值放入數組中。
x和y坐標由制表符分隔。 任何幫助,將不勝感激!
您應該考慮以下內容:
據我所知,第一個數字是計數。 如果確實如此,則不需要Strings的LinkedList。 構建一個Point數組,例如:
final Point[] points = new Point[Integer.parseInt(br.readLine())];
從那里開始,使用具有strLine變量的循環系統,使用該String並計算出如何解析它,例如:
int i = 0; // Put this outside of the while loop.
//While loop condition check here
final String[] split = strLine.split("\u0009"); // Unicode character for tab.
points[i++] = new Point(Integer.parseInt(split[0]), Integer.parseInt(split[1])); // Assuming length 2 of split.
至於渲染點,請創建一個新類,該類擴展了JPanel。 在該類中,添加以下代碼並填寫TODO:
@Override
public void paintComponent(final Graphics g){
//TODO: Paint what you want here, automatically double-buffered
}
現在,當您創建一個新的Panel類並將其添加到JFrame時,每當您從JFrame調用repaint()時,它將在Panel類的paintComponent()方法中呈現代碼。 如果您還有其他疑問,請隨時提問。
編輯:示例代碼:
import javax.swing.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class Test {
private static final String FILE = "Desktop/Assign2Test1.txt";
private static Point[] points;
public static void main(final String[] args){
try{
final BufferedReader br = new BufferedReader(new FileReader(new File(FILE)));
points = new Point[Integer.parseInt(br.readLine())];
int i = 0;
int xMax = 0;
int yMax = 0;
while(br.ready()){
final String[] split = br.readLine().split("\u0009");
final int x = Integer.parseInt(split[0]);
final int y = Integer.parseInt(split[1]);
xMax = Math.max(x, xMax);
yMax = Math.max(y, yMax);
points[i++] = new Point(x, y);
}
final JFrame frame = new JFrame("Point Data Rendering");
final Panel panel = new Panel();
panel.setPreferredSize(new Dimension(xMax + 10, yMax + 10));
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
frame.repaint();
} catch (final Exception e){
e.printStackTrace();
}
}
public static class Panel extends JPanel {
@Override
public void paintComponent(final Graphics g){
g.setColor(Color.RED);
for(final Point p : points){
g.fillRect((int) p.getX(), (int) p.getY(), 2, 2);
}
}
}
}
使用panel.getGraphics()
方法從元素中獲取Graphics對象的實例。 稍后使用graphics對象使用graphics.drawLine()
方法繪制線條。
您可以使用正則表達式string.split("\\t")
分割行的內容。 這將為您提供一個包含2個元素的數組(因為您說每行包含2個數字,並且它們之間使用制表符)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.