我试图找出是否可以从Graphics Abstract Class中初始化对象。我知道您无法使用以下对象定义其中之一:
Graphics canvas = new Graphics();
但是,我在Stack Overflow上阅读了有关此内容的其他文章,但对于您将用来执行此操作的技术感到非常困惑。
到目前为止,这是我的代码:
package main.pulsebeat02.Graphing;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.io.File;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import main.pulsebeat02.CSV_Reading.LoadtoArray.Import.Read_Text;
import main.pulsebeat02.CSV_Reading.LoadtoArray.Import.Read_Title;
public class Graph_GUI {
private final static int WIDTH = 1200; // Width for Window
private final static int HEIGHT = 800; // Height for Window
private static int MarkerLinesX = 0;
private static int MarkerLinesY = 0;
static Graphics2D canvasOriginal = null; // Canvas
static Graphics canvas = (Graphics) canvasOriginal;
private static JPanel panel = new JPanel(new GridBagLayout(), true);
private static JFrame window = new JFrame("CSV Graph");
public static void DefineWindow() {
window.setSize(new Dimension(WIDTH, HEIGHT));
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// canvas.translate(WIDTH/2, HEIGHT/2);
public Graph_GUI() { // GUI
panel.paint(canvas);
window.add(panel);
}
public static double[][] getValues(File file) { // Get Values of CSV File
return Read_Text.getDoublesCSV(file);
}
public static String[] getTitles(File file) { // Get Titles of CSV File
return Read_Title.getTitles(file);
}
@Deprecated
public static void drawPointCoords(Graphics canvas, int xCoord, int yCoord, int XArc, int YArc) { // Deprecated
canvas.drawOval(XArc, YArc, xCoord, yCoord);
}
public static void drawPoint(Graphics canvas, Point point) { // Draws Point
Ellipse2D.Double shape = new Ellipse2D.Double(point.x, point.y, point.Ysize, point.Xsize);
((Graphics2D) canvas).draw(shape);
// canvas.drawOval(point.x, point.y, point.Ysize, point.Xsize);
}
@Deprecated
public static void drawLineCoords(Graphics canvas, int x1, int y1, int x2, int y2) { // Deprecated
canvas.drawLine(x1, y1, x2, y2);
}
public static void drawLinePoints(Graphics canvas, Point point, Point point2) { // Draws Line
((Graphics2D) canvas).draw(new Line2D.Double(point.x, point.y, point2.x, point2.y));
}
public static void drawGraph(File file) { // Draw a Graph
double[][] values = getValues(file);
// String [] titles = getTitles(file);
ArrayList<Point> Points = new ArrayList<Point>(); // Define ArrayList of Points
for (int i = 0; i < values.length - 1; i++) {
for (int z = 0; z < values[0].length - 1; z++) {
// double [] currentPoint = {values[i][z], values[i + 1][z]};
Point currentPoint = new Point(values[i][z], values[i][z + 1], 5, 5); // Make current point
Points.add(currentPoint); // Add it to ArrayList
// drawPointCoords(canvas, (int)currentPoint[0], (int)currentPoint[1], 5, 5);
// Better Alternative is to use Point Class
if (Points.size() >= 2) {
drawLinePoints(canvas, Points.get(Points.indexOf(currentPoint) - 1), currentPoint);
}
}
}
for (int c = 0; c < Points.size(); c++) {
drawPoint(canvas, Points.get(c));
}
}
public static void drawXAxis(Graphics Oldcanvas, double scale, Point Xpoint, Point Xpoint1) {
// Make Cartesian plane here.
Graphics2D canvas = (Graphics2D) Oldcanvas;
canvas.draw(new Line2D.Double(Xpoint.x, Xpoint.y, Xpoint1.x, Xpoint1.y));
double length = Math
.sqrt((Xpoint1.x - Xpoint.x) * (Xpoint1.x - Xpoint.x) + (Xpoint1.y - Xpoint.y) * (Xpoint.y - Xpoint.y));
MarkerLinesX = (int) (length / scale);
}
public static void drawYAxis(Graphics Oldcanvas, double scale, Point Ypoint, Point Ypoint1) {
// Make Cartesian plane here.
Graphics2D canvas = (Graphics2D) Oldcanvas;
canvas.draw(new Line2D.Double(Ypoint.x, Ypoint.y, Ypoint1.x, Ypoint1.y));
double length = Math
.sqrt((Ypoint1.x - Ypoint.x) * (Ypoint1.x - Ypoint.x) + (Ypoint1.y - Ypoint.y) * (Ypoint.y - Ypoint.y));
MarkerLinesY = (int) (length / scale);
}
public static void drawLinesWithScale(Graphics canvas, int MarkerLength) {
}
public static void main(String[] args, File file) {
Graph_GUI graph = new Graph_GUI();
graph.DefineWindow();
graph.drawGraph(file);
}
}
我在这里的目标是使用值数组(x和y)使用point类创建它们的列表,并将它们绘制在canvas对象上。 (画布定义为“图形对象”)。然后,我将 canvas 放入JPanel并将其放入JFrame。但这给了我一个空指针异常,因为我没有初始化。我该如何解决这个问题或避免这个问题?