Gilad Mitrani Gilad Mitrani - 4 years ago 123
Java Question

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. 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

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:


  • MainPanel
    which holds some stuff

  • BlackPanel
    which is used to paint on.



My Class has an
ArrayList
- points as a class member.

All Point objects are made using
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.

EDIT: Solution by @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.

Then I called
repaint()
after loading the new point array - works like magic.

Answer Source

You have to override the paintComponent(Graphics) method of PanelBlack.

getGraphics() is not reliable to do your paintings, paintComponent(Graphics) is where you are supposed to customize the paintings.

Painting in Swing

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download