I am trying to learn some of the graphics side of Java. I made a text file that lists the points and the first line in the file determines how many points there are. I need to take the points in the text file and plot them on a canvas. I have looked at several resources and I just do not understand how everything works together. I cannot even find a tutorial that gives all of the code and explains where each part goes and how it is called. Basically, I am confused by JPanels, JFrames, and basically the whole process. Below is the code that I have written so far and a screen shot of what the file looks like.
Code:
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);
}
}
All the code is doing is putting the values in an array.
The x and y coordinates are separated by a tab. Any help would be appreciated!
Something you should consider is the following:
From what I can tell, the first number in there is the count. If that is indeed it, you don't need the LinkedList for the Strings. Build a Point array, for example:
final Point[] points = new Point[Integer.parseInt(br.readLine())];
From there, using that looping system you have with the strLine variable, use that String and work out how to parse it, for example:
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.
As for rendering the points, create a new class, one that extends JPanel. In that class, add the following code and fill in the TODO:
@Override
public void paintComponent(final Graphics g){
//TODO: Paint what you want here, automatically double-buffered
}
Now, when you create a new Panel class and add it to a JFrame, whenever you call for a repaint() from the JFrame it will render the code in the paintComponent() method of the Panel class. If you have any other questions, feel free to ask.
Edit: Example code:
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);
}
}
}
}
obtain the instance of the Graphics object from the element by using the panel.getGraphics()
method. later use the graphics object to draw lines using the graphics.drawLine()
method .
you can split the content of a line using a regex string.split("\\t")
. this will give you an array with 2 elements (since you say that each line contains 2 numbers with a tab in between).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.