[英]Paint Points from array to JPanel
I am trying to write a simple GUI application that allows the user to click on a panel, BlackPanel
in this situation, to paint a dot. 我正在尝试编写一个简单的GUI应用程序,该应用程序允许用户单击面板(在这种情况下为
BlackPanel
绘制点。 than save those dots to a file. 而不是将这些点保存到文件中。 And the ability to load dots from file and display them.
以及从文件加载点并显示它们的能力。
I have successfully made all the functions and methods needed to save and load an ArrayList<Point>
from file and have made sure that the points loaded contain the coordinates that are needed to be re-painted 我已经成功完成了从文件保存和加载
ArrayList<Point>
所需的所有功能和方法,并确保加载的点包含需要重新绘制的坐标
My problem is that I am unable to create a method that paints all the dots from an array 我的问题是我无法创建一种绘制数组中所有点的方法
My main class is a JFrame
which has two JPanel
s added to it: 我的主类是一个
JFrame
,其中添加了两个JPanel
:
MainPanel
which holds some stuff MainPanel
BlackPanel
which is used to paint on. BlackPanel
。 My Class has an ArrayList
- points as a class member. 我的班级有一个
ArrayList
指向班级成员。
All Point objects are made using java.awt.Point
. 所有Point对象都是使用
java.awt.Point
制成的。
private void paintPoint(Graphics g, Point p) {
g.setColor(Color.white);
g.fillOval(p.x, p.y, 5, 5);
} // this one is used to create a single dot called by MouseClicked event - works
private void paintPoints(Graphics g, ArrayList<Point> points) {
g.setColor(Color.white);
for (Point point : points) {
g.fillOval(point.x, point.y, 5, 5);
}
} // this one is called by LoadPointsDialog() which in turn is called by a button action
private void loadPointsDialog() {
FileDialog fd = new FileDialog(this, "Open XML file", FileDialog.LOAD);
fd.setDirectory("C:\\");
fd.setFile("*.xml");
fd.setFilenameFilter((File dir, String name) -> name.endsWith(".xml"));
fd.setVisible(true);
String folder = fd.getDirectory();
String fileName = fd.getFile();
ArrayList<Point> aux;
try { // irrelevent for this question. works.
aux = XMLio.read(folder+fileName);
} catch (IOException e) {
System.err.println("Error! Failed reading from file");
return;
}
ItemClear.doClick(); // clears points class member and calls BlackPanel.updateUI();
points.addAll(aux); // works 100% i checked to see if the points exist.
paintPoints(PanelBlack.getGraphics(), points);
}
private void ItemOpenActionPerformed(java.awt.event.ActionEvent evt) {
loadPointsDialog();
}
private void PanelBlackMouseClicked(java.awt.event.MouseEvent evt) {
Point p = evt.getPoint();
paintPoint(PanelBlack.getGraphics(), p);
savePoint(p); // points.add(p);
}
private void ItemClearActionPerformed(java.awt.event.ActionEvent evt) {
PanelBlack.updateUI();
points.clear();
}
i am using NetBeans designer tool to create this JFrame. 我正在使用NetBeans设计器工具创建此JFrame。
EDIT: Solution by @Berger 编辑:@Berger解决方案
I have created a new nested class : 我创建了一个新的嵌套类:
public class PaintPanel extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.white);
for (Point point : points) {
g.fillOval(point.x, point.y, 5, 5);
}
}
}
and defined PanelBlack
to be an object of this class. 并将
PanelBlack
定义为PanelBlack
的对象。
Then I called repaint()
after loading the new point array - works like magic. 然后,在加载新的点数组之后,我调用了
repaint()
-就像魔术一样工作。
You have to override the paintComponent(Graphics)
method of PanelBlack
. 您必须重写
PanelBlack
的paintComponent(Graphics)
方法。
getGraphics()
is not reliable to do your paintings, paintComponent(Graphics)
is where you are supposed to customize the paintings. getGraphics()
不能可靠地进行绘画,应该在paintComponent(Graphics)
中自定义绘画。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.