I have two ArrayList instances that contain some data and I want that data to be displayed as boxes or their data by using Swing.
The end goal is to display both array lists as one matrix of junctions and roads.
I have an object that contains both:
Map map = new Map(10);
System.out.println(map.calcShortestPath(map.getJunctions().get(4), map.getJunctions().get(0)));
System.out.println("\n Map #2");
ArrayList<Junction> junctions = new ArrayList<Junction>();
junctions.add(new Junction(0, 0));
junctions.add(new Junction(0, 3));
junctions.add(new Junction(4, 3));
junctions.add(new Junction(4, 0));
ArrayList<Road> roads = new ArrayList<Road>();
roads.add(new Road(junctions.get(0), junctions.get(1)));
roads.add(new Road(junctions.get(1), junctions.get(2)));
roads.add(new Road(junctions.get(2), junctions.get(3)));
roads.add(new Road(junctions.get(3), junctions.get(0)));
roads.add(new Road(junctions.get(0), junctions.get(2)));
map = new Map(junctions, roads);
The things I have tried so far:
Using JTable - didn't seem to be the correct choice for this.
Using JList - didn't seem to work as I tried to see one of the lists with this
code:
JList<Juncion> displayList = new JList<>(junctions.toArray(new String[0]));
JScrollPane scrollPane = new JScrollPane(displayList);
getContentPane().add(scrollPane);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
I think I'm getting closer to this by using the JList but I think I'm not doing this the correct way.
I thought it might be interesting to whip up an example of a graph display.
Here's the GUI I created.
The junctions are represented by squares, and the roads are represented by lines.
The first thing I did was create a Graph class to hold a List of junctions and a List of roads. I had to guess what the numbers of the Junction class represented. I assumed they were X and Y coordinates.
Once I created the Graph class (model class), writing the drawing panel and the paintComponent method was straightforward.
Here's the code. You would need to modify it to display more than one graph.
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class GraphDisplay implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new GraphDisplay());
}
private DrawingPanel drawingPanel;
private Graph graph;
private JFrame frame;
public GraphDisplay() {
this.graph = new Graph();
}
#Override
public void run() {
frame = new JFrame("Graph Display");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
drawingPanel = new DrawingPanel(graph);
frame.add(drawingPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public class DrawingPanel extends JPanel {
private static final long serialVersionUID = 1L;
private Graph graph;
private Tuple xTuple;
private Tuple yTuple;
public DrawingPanel(Graph graph) {
this.graph = graph;
this.xTuple = graph.getXRange();
this.yTuple = graph.getYRange();
this.setBackground(Color.WHITE);
this.setPreferredSize(new Dimension(400, 400));
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int xSpacing = getWidth() / (xTuple.getMaximum() -
xTuple.getMinimum() + 2);
int ySpacing = getHeight() / (yTuple.getMaximum() -
yTuple.getMinimum() + 2);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
g2d.setStroke(new BasicStroke(5f));
Font font = getFont().deriveFont(16f);
g2d.setFont(font);
List<Junction> junctions = graph.getJunctions();
for (int i = 0; i < junctions.size(); i++) {
Junction junction = junctions.get(i);
int x = (junction.getX() + 1) * xSpacing;
int y = (junction.getY() + 1) * ySpacing;
g.drawRect(x - 16, y - 16, 32, 32);
}
List<Road> roads = graph.getRoads();
for (int i = 0; i < roads.size(); i++) {
Road road = roads.get(i);
Junction origin = road.getOrigin();
Junction destination = road.getDestination();
int x1 = (origin.getX() + 1) * xSpacing;
int y1 = (origin.getY() + 1) * ySpacing;
int x2 = (destination.getX() + 1) * xSpacing;
int y2 = (destination.getY() + 1) * ySpacing;
g2d.drawLine(x1, y1, x2, y2);
}
}
}
public class Graph {
private final List<Junction> junctions;
private final List<Road> roads;
public Graph() {
junctions = new ArrayList<Junction>();
junctions.add(new Junction(0, 0));
junctions.add(new Junction(0, 3));
junctions.add(new Junction(4, 3));
junctions.add(new Junction(4, 0));
roads = new ArrayList<Road>();
roads.add(new Road(junctions.get(0), junctions.get(1)));
roads.add(new Road(junctions.get(1), junctions.get(2)));
roads.add(new Road(junctions.get(2), junctions.get(3)));
roads.add(new Road(junctions.get(3), junctions.get(0)));
roads.add(new Road(junctions.get(0), junctions.get(2)));
}
public List<Junction> getJunctions() {
return junctions;
}
public List<Road> getRoads() {
return roads;
}
public Tuple getXRange() {
int minimum = junctions.get(0).getX();
int maximum = minimum;
for (int i = 1; i < junctions.size(); i++) {
int x = junctions.get(i).getX();
minimum = Math.min(minimum, x);
maximum = Math.max(maximum, x);
}
return new Tuple(minimum, maximum);
}
public Tuple getYRange() {
int minimum = junctions.get(0).getY();
int maximum = minimum;
for (int i = 1; i < junctions.size(); i++) {
int y = junctions.get(i).getY();
minimum = Math.min(minimum, y);
maximum = Math.max(maximum, y);
}
return new Tuple(minimum, maximum);
}
}
public class Road {
private final Junction origin;
private final Junction destination;
public Road(Junction origin, Junction destination) {
this.origin = origin;
this.destination = destination;
}
public Junction getOrigin() {
return origin;
}
public Junction getDestination() {
return destination;
}
}
public class Junction {
private final int x;
private final int y;
public Junction(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
public class Tuple {
private final int minimum;
private final int maximum;
public Tuple(int minimum, int maximum) {
this.minimum = minimum;
this.maximum = maximum;
}
public int getMinimum() {
return minimum;
}
public int getMaximum() {
return maximum;
}
}
}
Related
I'm working on a really basic bar chart which has to display 6 values. The problem I'm running into is that when i put the bars on the screen once they stay on the screen, and i cannot get them off. I've tried using the remove, repaint and revalidate functions but these all do not work.
What do I have to do to remove the bars so they don't clog up?
My code:
import javax.swing.*;
import java.awt.*;
import java.util.Collections;
public class BarChart extends JPanel
{
private JLabel[] bars;
public BarChart(int[] data)
{
update(data);
}
public void update(int[] data)
{
this.setSize(190, 155);
this.setLayout(null);
int max = 0;
for (int i = 0; i < 6; i++) {if (data[i] > max) {max = data[i];}}
bars = new JLabel[6];
for (int i = 0; i < 6; i++)
{
bars[i] = new JLabel();
bars[i].setOpaque(true);
bars[i].setBackground(Color.RED);
int height = (max != 0) ? (data[i]*155)/max : 0;
System.out.printf("%d, %d, %d,... ", height, data[i], max);
this.add(bars[i]);
bars[i].setSize(25, height);
bars[i].setLocation((31*i)+5, 155-height);
}
System.out.println("");
}
}
For your current code, you would need to call removeAll(), then revalidate() and repaint() on the JPanel would "solve" your problem, but you have other unrelated problems:
You're setting a component's size when you should be producing a preferred size -- this is what layout managers generally work with
You're using null layouts, a very dangerous thing
You're using a "magic" number as a for loop ending condition -- a VERY dangerous thing to do. How do you know that the data array has 6 and only items within it. What harm is there in simply using the data array's length as you've likely done hundreds of times before?
Instead, consider using more flexible code that will adapt to any size of data you give it and that avoids null layouts. For example consider the following code that draws the bars within the JPanel's paintComponent method:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
#SuppressWarnings("serial")
public class TestBarChart extends JPanel {
private static final int[] INIT_DATA = { 1, 2, 4, 5, 6, 9 };
protected static final int MIN_DATA_lENGTH = 5;
protected static final int MAX_DATA_LENGTH = 9;
private static final int MAX_VALUE = 9;
private static final int PREF_W = 300;
private static final int PREF_H = 240;
private BarChart2 barChart2 = new BarChart2(INIT_DATA, MAX_VALUE, PREF_W, PREF_H);
public TestBarChart() {
barChart2.setBorder(BorderFactory.createLineBorder(Color.BLUE));
JPanel chartsPanel = new JPanel(new GridLayout(1, 0));
chartsPanel.setLayout(new GridLayout(1, 0));
chartsPanel.add(barChart2);
JButton resetDataBtn = new JButton(new AbstractAction("Reset Data") {
#Override
public void actionPerformed(ActionEvent e) {
int dataLength = (int) ((MAX_DATA_LENGTH - MIN_DATA_lENGTH) * Math.random()) + MIN_DATA_lENGTH;
int[] data = new int[dataLength];
for (int i = 0; i < data.length; i++) {
data[i] = (int) (MAX_VALUE * Math.random()) + 1;
}
barChart2.setData(data, MAX_VALUE);
}
});
JPanel btnPanel = new JPanel();
btnPanel.add(resetDataBtn);
setLayout(new BorderLayout());
add(chartsPanel);
add(btnPanel, BorderLayout.PAGE_END);
}
private static void createAndShowGui() {
TestBarChart mainPanel = new TestBarChart();
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
#SuppressWarnings("serial")
class BarChart2 extends JPanel {
private static final double BAR_WIDTH = 0.90;
private int prefW;
private int prefH;
private static final Color BAR_COLOR = Color.RED;
private int[] data;
private int maxValue;
public BarChart2(int[] data, int maxValue, int prefW, int prefH) {
setData(data, maxValue);
this.prefW = prefW;
this.prefH = prefH;
}
public final void setData(int[] data, int maxValue) {
this.data = data;
this.maxValue = maxValue;
repaint();
}
public int[] getData() {
return data;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(BAR_COLOR);
// simple algebraic calculations on where to place the bars
double denom = data.length + 1 - BAR_WIDTH;
int barWidth = (int) ((getWidth() * BAR_WIDTH) / denom);
for (int i = 0; i < data.length; i++) {
int x = (int) (getWidth() * (i + 1 - BAR_WIDTH) / denom);
int height = (int) (getHeight() * data[i] / (double) maxValue);
int y = (int) (getHeight() - height);
g.fillRect(x, y, barWidth, height);
}
}
#Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(prefW, prefH);
}
}
Note that the bar charts re-size to fill the GUI if you re-size the GUI. Note that the chart accomodates any number of data bars, all depending on the length of the data array passed into it.
You need to repaint the component.
getContentPane().validate();
getContentPane().repaint();
I'm a bit stuck trying to manipulate information stored in an ArrayList in the main class.
My ArrayList contains a "RandomTurtleA" object called "turtles"(more turtles will be added to the ArrayList later) which extends "DynamicTurtle" which extends "Turtle". If I can change the CartesianCoordinates for the "turtles" stored in the ArrayList in Class Turtle, I can then make sure they do not leave the visible JFrame.
I have setter methods to change the CartesianCoordinates, however cannot find out how to use them outside of the Main class in the wrapPosition method where the various if statements are to check if the turtles off screen.
Here is the Main Class: (The ArrayList)
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
class Lab7b
{
public static void main(String [ ] args)
{
int deltaTime = 50;
double currentX, currentY;
JFrame frame = new JFrame();
Canvas canvas = new Canvas();
frame.setTitle("Welcome to turtle land!");
frame.setSize(1000, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.add(canvas);
JLabel hello = new JLabel("Hello all Word!");
hello.setHorizontalAlignment(SwingConstants.CENTER);
frame.add(hello, BorderLayout.PAGE_END);
Canvas turtleCanvas = new Canvas();
frame.add(turtleCanvas, BorderLayout.CENTER);
ImageIcon supermanImage = new ImageIcon("superman.png");
JLabel supermanJLable = new JLabel(supermanImage);
frame.add(supermanJLable, BorderLayout.LINE_START);
ImageIcon hulkImage = new ImageIcon("hulk.png");
JLabel hulkJLable = new JLabel(hulkImage);
frame.add(hulkJLable, BorderLayout.LINE_END);
CartesianCoordinate randomInit = new CartesianCoordinate(400,300);
ArrayList<DynamicTurtle> turtles = new ArrayList<DynamicTurtle>();
turtles.add(new RandomTurtleA(turtleCanvas, 400, 300));
while(true)
{
for (int i = 0; i < turtles.size(); i++)
{
(turtles.get(i)).unDrawTurtle();
hello.setText("X: " + (turtles.get(i)).getPositionX() + " Y: " + (turtles.get(i)).getPositionY());
(turtles.get(i)).getPositionX() = currentX; // Problems here trying to access the current position of the turtle
(turtles.get(i)).getPositionX() = currentY; // " "
(turtles.get(i)).wrapPosition(1000, 600);
}
for (int i = 0; i < turtles.size(); i++)
{
(turtles.get(i)).update(1000);
}
for (int i = 0; i < turtles.size(); i++)
{
(turtles.get(i)).drawTurtle();
}
Utils.pause(deltaTime/2);
}
}
}
The Turtle Class: (containing the wrapPosition Method)
import java.util.ArrayList;
class Turtle
{
protected Canvas canvas; // private field reference to a canvas private
private CartesianCoordinate myLocation, oldLocation;
private boolean penDown = true;
private double Angle;
public Turtle kieranMullen;
public Turtle(Canvas canvas, CartesianCoordinate initLocation)
{
this.canvas = canvas;
this.myLocation = new CartesianCoordinate(0,0);
Angle = 0;
penDown = true;
myLocation = initLocation.copy();
}
public void wrapPositon(double maximumXPosition, double mamimumYPosition)
{
if(turtles.getPositionX() < 0) //if current x of turtle is less than 0
{
this.setX(1000); //set to 1000
}
if(currentX > 1000)
{
this.setX(0);
}
if(currentY < 0)
{
this.setY(600);
}
if(currentY > 600)
{
this.setX(0);
}
}
public double getPositionX()
{
double getPosX;
getPosX = myLocation.getX();
return getPosX;
}
public double getPositionY()
{
double getPosY;
getPosY = myLocation.getY();
return getPosY;
}
}
The CartesianCoordinate Class: (Containing the setter Methods)
class CartesianCoordinate
{
private double xPosition, yPosition, setterX, setterY;
public CartesianCoordinate(double x, double y)
{
this.xPosition = x;
this.yPosition = y;
}
public double getX()
{
return this.xPosition;
}
public double getY()
{
return this.yPosition;
}
public void setX(double setterX)
{
this.setterX = xPosition;
}
public void setY(double setterY)
{
this.setterY = yPosition;
}
}
I Would greatly appreciate any help given to me, thanks in advance. Regards, Hornsbeh.
Your Problem is, that you only have a getter for position x and y in your turtle class.
You have to add a setter for your location that is public in the Turtle class that in turn calls the functions of your Coordinates Object. Just like you already did with the get methods.
Setter:
class Turtle {
//...
public void setPositionX(final double x)
{
myLocation.setX(x);
}
The same for Y.
Then in your main class you can set X by using that method:
(turtles.get(i)).setPositionX(currentX);
I am trying to create a program that record mouse clicks, draw lines between those points and after some calculations display some circles through a button call. My problem is that I can display the lines or the circles, but not both.
I know there is something overlapping something else, but I am very new to Java and I don't know how to fix it. Here is the code:
package fempack;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import java.lang.Math;
public class MainFem extends JPanel {
final DrawPoints Npoints = new DrawPoints();
final DrawLines Nline = new DrawLines();
private static final long serialVersionUID = 1L;
private MouseHandler mouseHandler = new MouseHandler();
private Point p1 = new Point(100, 100);
public int Xpoint[][] = new int[500][30];
public int Ypoint[][] = new int[500][30];
private double Xmpoint[][] = new double[500][1000]; // [i γραμμή][συντεταγμένη Χ]
private double Ympoint[][] = new double[500][1000];
private double Vec[][][] = new double[500][2][500]; // [i γραμμή][0,1][0α 1β]
private double dist[] = new double[10000];
private boolean drawing;
private int c1;
private int c2;
public MainFem() {
this.addMouseListener(mouseHandler);
this.addMouseMotionListener(mouseHandler);
}
// -------------- Draw by clicking -----------------------
private class MouseHandler extends MouseAdapter {
#Override
public void mousePressed(MouseEvent e) {
if(SwingUtilities.isRightMouseButton(e)){
drawing=false;
c2++;
}
if(SwingUtilities.isLeftMouseButton(e)){
p1 = e.getPoint();
Xpoint[c1][c2] = p1.x;
Ypoint[c1][c2] = p1.y;
if (c1 > 3) {
for (int j = 0; j<c2+1; j++){
for (int i = 0; i<c1; i++){
if ((Math.abs(Xpoint[i][j]-Xpoint[c1][c2]) < 10) && (Math.abs(Ypoint[i][j]-Ypoint[c1][c2]) < 10)) {
Xpoint[c1][c2] = Xpoint[i][j];
Ypoint[c1][c2] = Ypoint[i][j];
System.out.println(Xpoint[i][j]);
}
}
}
}
if (drawing == true){
Nline.addLine(Xpoint[c1][c2], Ypoint[c1][c2], Xpoint[c1-1][c2], Ypoint[c1-1][c2]);
}
c1++;
drawing = true;
}
}
}
// ---------------- Create Mesh Points --------------------------
public void createmesh() {
int mdi = 0;
for (int j = 0; j<=c2; j++){
for (int i = 0; i<c1-1; i++){
// Υπολογισμός a και b συνιστωσών της εξίσωσης της γραμμής
Vec[i][0][mdi] = (Ypoint[i+1][j] - Ypoint[i][j])/(Xpoint[i+1][j] - Xpoint[i][j]);
Vec[i][1][mdi] = Ypoint[i][j] - Xpoint[i][j]*Vec[i][1][mdi];
// Υπολογισμός μέτρου διανύσματος
dist[mdi] = Math.sqrt(Math.pow(Xpoint[i][j] - Xpoint[i+1][j], 2) + Math.pow(Ypoint[i][j] - Ypoint[i+1][j], 2) );
// Υπολογισμός ενδιάμεσον σημείων
int nkom = 3;
double xa = Xpoint[i][j];
double ya = Ypoint[i][j];
for (int ii = 0; ii <nkom; ii++) {
double a = Vec[i][0][mdi];
double b = Vec[i][1][mdi];
Xmpoint[i][ii] = (-((2*a)*(b - ya) - 2*xa) + Math.sqrt(Math.abs(Math.pow(((2*a)*(b - ya) - 2*xa), 2) - 4*(1 + a*a)*(xa*xa + Math.pow((b - ya),2) - Math.pow(dist[mdi]/nkom,2)))))/(2 + 2*a*a);
Ympoint[i][ii] = a*Xmpoint[i][ii] + b;
double xm11 = Xmpoint[i][ii];
double ym11 = Ympoint[i][ii];
int xm1 = (int) xm11;
int ym1 = (int) ym11;
Npoints.addPoint(xm1, ym1);
System.out.println("i:" + ym11 + "...ii:" + ym1 );
xa = Xmpoint[i][ii];
ya = Ympoint[i][ii];
}
mdi++;
}
}
}
//------------------------- Display ---------------------------
private void display() {
JFrame f = new JFrame("LinePanel");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setPreferredSize(new Dimension(500, 600));
f.setLocationRelativeTo(null);
f.add(Npoints);
f.add(Nline);
JPanel buttonsPanel = new JPanel();
//-----------------Complete---------------
JButton dcomp = new JButton("Complete");
buttonsPanel.add(dcomp);
dcomp.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
createmesh();
}
});
//------------------Clean-------------------
JButton clearButton = new JButton("Clear");
buttonsPanel.setBorder(BorderFactory.createLineBorder(Color.black));
buttonsPanel.setPreferredSize(new Dimension(500, 100));
f.add(buttonsPanel, BorderLayout.SOUTH);
buttonsPanel.add(clearButton);
clearButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
Nline.clearLines();
}
});
f.pack();
f.setVisible(true);
f.add(this);
}
//---------------------------------------------------------
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new MainFem().display();
}
});
}
}
Class to draw lines:
package fempack;
import java.awt.Graphics;
import java.util.LinkedList;
import javax.swing.JPanel;
public class DrawLines extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
public DrawLines() {
}
private static class Line{
final int x1;
final int y1;
final int x2;
final int y2;
public Line(int x1, int y1, int x2, int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
}
private final LinkedList<Line> lines = new LinkedList<Line>();
public void addLine(int x1, int x2, int x3, int x4) {
lines.add(new Line(x1,x2,x3,x4));
repaint();
}
public void clearLines() {
lines.clear();
repaint();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Line line : lines) {
// System.out.println(line);
g.drawLine(line.x1, line.y1, line.x2, line.y2);
}
}
}
And class to draw Circles:
package fempack;
import java.awt.Graphics;
import javax.swing.JPanel;
import java.util.LinkedList;
public class DrawPoints extends JPanel {
private static final long serialVersionUID = 1L;
public DrawPoints() {
}
private static class Point {
final int x;
final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
private final LinkedList<Point> points = new LinkedList<Point>();
public void addPoint(int x, int y) {
points.add(new Point(x,y));
// System.out.println(x);
repaint();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Point point : points) {
// System.out.println(point);
g.drawOval(point.x, point.y, 5, 5);
}
}
}
Here is my solution to draw the points added in the createmesh(). I did not modify any handling of the points you got from the mouse position. I just tried to refactor and clean up your code.
Now DrawLine and DrawPoint are no more JPanel's they just hold the data you give them and draw to the Graphics-Object you pass to them in public void draw(Graphics g) method, which is called on paint in the DrawPanel-Class. DrawPanel inherits from JPanel and overrides void PaintComponent(Graphics g); to realize this (Thanks to MadProgrammer ;). Also DrawPanel handels the MouseEvents to put the mouse-positions into DrawLines- and DrawPoints-objects.
MainWindow inherits from JFrame and is the MainWindow. It creates all GUI-elements and calls the DrawPanel if necessary.
MainWindow:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainWindow extends JFrame {
private static final long serialVersionUID = 6755417048009930291L;
// begin gui components
JButton clearButton = null;
JButton dcomp = null;
JPanel buttonsPanel = null;
DrawPanel drawPanel = null;
// end gui components
public MainWindow(){
// Add ButtonPanel
add(getDrawPanel(), BorderLayout.CENTER);
add(getButtonPanel(), BorderLayout.SOUTH);
// Add Buttons
getButtonPanel().add(getDcomp());
getButtonPanel().add(getClearButton());
addMouseListener(getDrawPanel());
}
// begin getters and setters for gui components
private DrawPanel getDrawPanel(){
if(drawPanel == null){
drawPanel = new DrawPanel();
drawPanel.setVisible(true);
}
return drawPanel;
}
private JPanel getButtonPanel() {
if(buttonsPanel == null){
buttonsPanel = new JPanel();
buttonsPanel.setBorder(BorderFactory.createLineBorder(Color.black));
buttonsPanel.setPreferredSize(new Dimension(500, 100));
}
return buttonsPanel;
}
private JButton getClearButton() {
if(clearButton == null){
clearButton = new JButton("Clear");
clearButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
getDrawPanel().clearLines();
}
});
}
return clearButton;
}
private JButton getDcomp() {
if(dcomp == null){
dcomp = new JButton("Complete");
dcomp.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
getDrawPanel().createmesh();
getDrawPanel().repaint();
}
});
}
return dcomp;
}
// end begin getters and setters for gui components
//as always program entry-point
public static void main(String[] args) {
MainWindow wnd = new MainWindow();
wnd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
wnd.setPreferredSize(new Dimension(500, 600));
wnd.setLocationRelativeTo(null);
wnd.pack();
wnd.setVisible(true);
}
}
DrawPanel:
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class DrawPanel extends JPanel implements MouseListener {
private static final long serialVersionUID = -7726303639184194659L;
// begin members
private DrawLines dlines;
private DrawPoints dpoints;
// dont know what these members do
private Point p1 = new Point(100, 100);
public int Xpoint[][] = new int[500][30];
public int Ypoint[][] = new int[500][30];
private double Xmpoint[][] = new double[500][1000]; // [i
// γραμμή][συντεταγμένη
// Χ]
private double Ympoint[][] = new double[500][1000];
private double Vec[][][] = new double[500][2][500]; // [i γραμμή][0,1][0α
// 1β]
private double dist[] = new double[10000];
private boolean drawing;
private int c1;
private int c2;
// end members
public DrawPanel() {
dlines = new DrawLines();
dpoints = new DrawPoints();
addMouseListener(this);
}
// begin class logic
public void clearLines(){
dlines.clearLines();
repaint();
}
// dont know what this does
public void createmesh() {
int mdi = 0;
for (int j = 0; j <= c2; j++) {
for (int i = 0; i < c1 - 1; i++) {
// Υπολογισμός a και b συνιστωσών της εξίσωσης της γραμμής
Vec[i][0][mdi] = (Ypoint[i + 1][j] - Ypoint[i][j])
/ (Xpoint[i + 1][j] - Xpoint[i][j]);
Vec[i][1][mdi] = Ypoint[i][j] - Xpoint[i][j] * Vec[i][1][mdi];
// Υπολογισμός μέτρου διανύσματος
dist[mdi] = Math.sqrt(Math.pow(Xpoint[i][j] - Xpoint[i + 1][j],
2) + Math.pow(Ypoint[i][j] - Ypoint[i + 1][j], 2));
// Υπολογισμός ενδιάμεσον σημείων
int nkom = 3;
double xa = Xpoint[i][j];
double ya = Ypoint[i][j];
for (int ii = 0; ii < nkom; ii++) {
double a = Vec[i][0][mdi];
double b = Vec[i][1][mdi];
Xmpoint[i][ii] = (-((2 * a) * (b - ya) - 2 * xa) + Math
.sqrt(Math.abs(Math.pow(
((2 * a) * (b - ya) - 2 * xa), 2)
- 4
* (1 + a * a)
* (xa * xa + Math.pow((b - ya), 2) - Math
.pow(dist[mdi] / nkom, 2)))))
/ (2 + 2 * a * a);
Ympoint[i][ii] = a * Xmpoint[i][ii] + b;
double xm11 = Xmpoint[i][ii];
double ym11 = Ympoint[i][ii];
int xm1 = (int) xm11;
int ym1 = (int) ym11;
dpoints.addPoint(xm1, ym1);
System.out.println("i:" + ym11 + "...ii:" + ym1);
xa = Xmpoint[i][ii];
ya = Ympoint[i][ii];
}
mdi++;
}
}
}
// end class logic
// begin MouseListener implementation
#Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
drawing = false;
c2++;
}
if (SwingUtilities.isLeftMouseButton(e)) {
p1 = e.getPoint();
Xpoint[c1][c2] = p1.x;
Ypoint[c1][c2] = p1.y;
if (c1 > 3) {
for (int j = 0; j < c2 + 1; j++) {
for (int i = 0; i < c1; i++) {
if ((Math.abs(Xpoint[i][j] - Xpoint[c1][c2]) < 10)
&& (Math.abs(Ypoint[i][j] - Ypoint[c1][c2]) < 10)) {
Xpoint[c1][c2] = Xpoint[i][j];
Ypoint[c1][c2] = Ypoint[i][j];
System.out.println(Xpoint[i][j]);
}
}
}
}
if (drawing == true) {
dlines.addLine(Xpoint[c1][c2], Ypoint[c1][c2],
Xpoint[c1 - 1][c2], Ypoint[c1 - 1][c2]);
}
c1++;
drawing = true;
repaint();
}
}
#Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
// end MouseListener implementation
// beging PAINTING
#Override
protected void paintComponent(Graphics g) {
System.out.println("paintComponent");
super.paintComponent(g);
dlines.draw(g);
dpoints.draw(g);
}
// end PAINTING
}
DrawPoints:
import java.awt.Graphics;
import java.util.LinkedList;
public class DrawPoints {
public DrawPoints() {
}
private static class Point {
final int x;
final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
private final LinkedList<Point> points = new LinkedList<Point>();
public void addPoint(int x, int y) {
points.add(new Point(x, y));
}
public void draw(Graphics g){
for (Point point : points) {
g.drawOval(point.x, point.y, 5, 5);
}
}
}
DrawLines:
import java.awt.Graphics;
import java.util.LinkedList;
public class DrawLines{
public DrawLines() {
}
private static class Line {
final int x1;
final int y1;
final int x2;
final int y2;
public Line(int x1, int y1, int x2, int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
}
private final LinkedList<Line> lines = new LinkedList<Line>();
public void addLine(int x1, int x2, int x3, int x4) {
lines.add(new Line(x1, x2, x3, x4));
}
public void clearLines() {
lines.clear();
}
public void draw(Graphics g){
for (Line line : lines) {
g.drawLine(line.x1, line.y1, line.x2, line.y2);
}
}
}
I think its a good idea to handle things like this. You want to paint on a Panel or something so pass it to the object which hold your data and let them draw to their common canvas like I did with the void draw(Graphics g) method in DrawPoints- and DrawLines-class.
I hope this will help you.
Fell free to ask if something is unclear.
I have a window that dynamically updates the buffered image set on a JPanel using javax.swing.Timer
Everything works as expected but every time I invoke the dynamic update there seems to be another buffered image displayed below the currently updating one.
The image of the window before and after clicking the train button (which triggers the dynamic update) is given below.
Since the image below the dynamically updating image looks like the initial screen. I rechecked the following
Whether I'm adding two dynamic lattice objects to the same panel
Multiple calls of repaint()
Unwanted initialization of the dynamic lattice
I could not find any of these in my code. I cannot post the code since it is huge and whenever I'm creating a minimal set to reproduce the same behavior it is not there. So I'm sure I'm missing something or doing something on my project code which triggers this behavior. Any suggestions on how to debug this or why it is doing something like this?
Thank you
EDIT
SSCCE is give below. If executing, click the load button followed by the train button to get the error.
(MapScreen.java - Main Class)
package test;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import test.DisplayLattice;
import test.SelfOrganizingMap;
public class MapScreen extends JFrame {
private double NUM_ITERATIONS = 0.0;
private double ETA = 0.0;
private double SPREAD_FACTOR = 0.0;
private double RADIUS = 0.0;
private int WIDTH = 0;
private int HEIGHT = 0;
private SelfOrganizingMap SOM = null;
private Timer REFRESH_TIMER = null;
private JPanel pnlMap;
private JButton btnLoadParameters;
private JButton btnTrain;
private DisplayLattice displayScreen;
public MapScreen(double iterations, double learningRate, double spreadFactor, double radius, int option, int width, int height, int mapOption) {
NUM_ITERATIONS = iterations;
ETA = learningRate;
SPREAD_FACTOR = spreadFactor;
RADIUS = radius;
WIDTH = width;
HEIGHT = height;
setType(Type.UTILITY);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Map");
setSize(650, 800);
setLocation(150,150);
getContentPane().setLayout(null);
displayScreen = new DisplayLattice();
pnlMap = displayScreen;
pnlMap.setBounds(6, 130, 600, 600);
getContentPane().add(pnlMap);
btnLoadParameters = new JButton("Load Parameters");
btnLoadParameters.setFont(new Font("Tahoma", Font.PLAIN, 11));
btnLoadParameters.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
SOM = new SelfOrganizingMap(10000,0,0,13,displayScreen);
}
});
btnLoadParameters.setBounds(192, 46, 126, 23);
getContentPane().add(btnLoadParameters);
btnTrain = new JButton("Train");
btnTrain.setFont(new Font("Tahoma", Font.PLAIN, 11));
btnTrain.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
initialObjectSetUp();
}
});
btnTrain.setBounds(192, 72, 62, 23);
getContentPane().add(btnTrain);
}
private void initialObjectSetUp()
{
SOM.initTrainSOM(null, 100, 0.25);
REFRESH_TIMER = new Timer(100, SOM);
REFRESH_TIMER.start();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
MapScreen frame = new MapScreen(100,0.25,0.0,0.0,1,100,0,0);
frame.setVisible(true);
}
});
}
}
(SelfOrganizingMap.java)
package test;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SelfOrganizingMap implements ActionListener {
private Node[][] SOM = null;
private double[][] NORM_MAP = null; //holds the L2 norm of each vector in the SOM[][].
#SuppressWarnings("unused")
private int GRID_OPTION = 0;
private int INPUT_DIMENSION = 0;
private int NUMER_OF_ITERATIONS = 0;
private int CURRENT_ITERATION=0;
private int SOM_HORIZONTAL_LENGTH = 0;
private int SOM_VERTICAL_LENGTH = 0;
private double INITIAL_LEARNING_RATE = 0.0;
private double LEARNING_RATE = 0.0;
private double MAX_RADIUS = 0.0; //radius at first epoch (t = 0)
private double RADIUS = 0.0;
private double TIME_STEP = 0.0; //lambda of X(t) = t0 * exp(-t/lambda)
private String INPUT_SAMPLES = null;
private DisplayLattice DISPLAY_SCREEN = null;
public SelfOrganizingMap(int numberOfNodes, int depth, int grid, int inputDimensison, DisplayLattice screen)
{
INPUT_DIMENSION = inputDimensison;
if(grid == 0)
{
int side = (int)Math.sqrt(numberOfNodes);
SOM = new Node[side][side];
NORM_MAP = new double[side][side];
GRID_OPTION = grid;
MAX_RADIUS = side/2;
DISPLAY_SCREEN = screen;
}
RADIUS = MAX_RADIUS;
}
public void initTrainSOM(String input, int iterations, double learningRate)
{
NUMER_OF_ITERATIONS = iterations;
INITIAL_LEARNING_RATE = learningRate;
LEARNING_RATE = INITIAL_LEARNING_RATE;
TIME_STEP = NUMER_OF_ITERATIONS/Math.log(MAX_RADIUS);
INPUT_SAMPLES = input;
}
private void singleCompleteRun()
{
DISPLAY_SCREEN.render();
System.out.println(CURRENT_ITERATION);
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(CURRENT_ITERATION <= NUMER_OF_ITERATIONS)
{
singleCompleteRun();
CURRENT_ITERATION++;
}
}
}
(DisplayLattice.java)
package test;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.*;
#SuppressWarnings("serial")
public class DisplayLattice extends JPanel {
private BufferedImage img = new BufferedImage(500, 500, 1);
public void paintComponent(Graphics g) {
if (img == null)
super.paintComponents(g);
else
g.drawImage(img, 0, 0, this);
}
public void render() {
float cellWidth = 100;
float cellHeight = 100;
int imgW = img.getWidth();
int imgH = img.getHeight();
float r, g, b;
Graphics2D g2 = img.createGraphics();
g2.setBackground(Color.black);
g2.clearRect(0,0,imgW,imgH);
for (int x=0; x<100; x++) {
for (int y=0; y<100; y++) {
r = (float)Math.random();
g = (float)Math.random();
b = (float)Math.random();
g2.setColor(new Color(r,g,b));
g2.fillRect((int)(x*cellWidth), (int)(y*cellHeight),
(int)cellWidth+1, (int)cellHeight+1);
}
}
g2.setColor(Color.black);
g2.dispose();
repaint();
}
public BufferedImage getImage() {
if (img == null)
img = (BufferedImage)createImage(500, 500);
return img;
}
public void setImage(BufferedImage bimg) {
img = bimg;
}
}
(Node.java - Structure class for the SOM)
package test;
public class Node {
private int DIMENSION = 0;
private int POSITION_X = 0;
private int POSITION_Y = 0;
private double ACTIVATION_VALUE = 0.0;
public Node(int Dimensions, int x, int y)
{
DIMENSION = Dimensions;
setWeightVector();
POSITION_X = x;
POSITION_Y = y;
}
public int getX() {
return POSITION_X;
}
public int getY() {
return POSITION_Y;
}
public double getACTIVATION_VALUE() {
return ACTIVATION_VALUE;
}
public void setPOSITION_X(int x) {
POSITION_X = x;
}
public void setPOSITION_Y(int y) {
POSITION_Y = y;
}
public void setACTIVATION_VALUE(double y) {
ACTIVATION_VALUE= y;
}
private void setWeightVector()
{
double temp[] = new double[DIMENSION];
for(int i = 0; i<temp.length ; i++)
{
temp[i] = Math.random();
}
}
}
The problem is your DiaplyLattice class.
You overrode paintComponent but you invoke super.paintComponents(g). Notice the extra s you have at the end of paintComponents! This of course is unwanted and should be super.paintComponent(g);
I would have you method as follow:
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (img != null) {
g.drawImage(img, 0, 0, this);
}
}
Now, just as a good advice/tip to give, don't use null layout and rather use LayoutManager's and possibly use several level of nesting. It's always easier.
Also, you missed an important thing in SSCCE: the SHORT part. Meaning that you should remove anything unnecessary and have a single file to copy/paste.
I'm building a Tree traversal program which allows users to run BFS and DFS traversals, as well as add and remove nodes.
So far I've been able to update the adjacency matrix when someone clicks to add a new child node to a parent:
Then I click Add Node, which adds Z to parent A:
You can see that the adjacency matrix appends Z to A... but I'm expecting the node Z to appear on the left side tree as well.
I think the problem is the updated nodeList is not being sent to paintComponent() loop for painting... so it's not being shown.
Here is a SSCCE of my program:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import javax.swing.JFrame;
public class MainSSCCE extends JFrame {
static MainSSCCE run;
int amount, nodeWidth, nodeHeight;
GraphSSCCE g;
JFrame f;
public MainSSCCE(){
f = new JFrame("DTurcotte's Graph Traversals");
g = new GraphSSCCE(450, 300);
f.add(g);
f.setSize(g.getWidth(),g.getHeight());
f.setVisible(true);
nodeWidth = 25;
nodeHeight = 25;
f.setResizable(false);
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
public static void main(String[] args) {
run = new MainSSCCE();
}
}
Graph:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GraphSSCCE extends JPanel {
ArrayList<NodesSSCCE> nodeList;
public int[][] adjMatrix;
NodesSSCCE rootNode;
int size, height, width, x, y, counter;
Color color;
String message = "", run = "", nodeVal = "";
JButton AddButton;
JLabel label;
int nodeX = 180, nodeY = 100, nodeWidth, nodeHeight;
TextField child;
JComboBox parents;
public GraphSSCCE(int w, int h) {
height = h;
width = w;
nodeList = new ArrayList<NodesSSCCE>();
nodeWidth = 25;
nodeHeight = 25;
AddButton = new JButton("Add Node");
label = new JLabel("to parent");
label.setForeground(Color.WHITE);
child = new TextField(1);
parents = new JComboBox();
add(AddButton);
add(child);
add(label);
add(parents);
NodesSSCCE nA = new NodesSSCCE("A", nodeX, nodeY, nodeWidth, nodeHeight);
NodesSSCCE nB = new NodesSSCCE("B", nodeX, nodeY, nodeWidth, nodeHeight);
NodesSSCCE nC = new NodesSSCCE("C", nodeX, nodeY, nodeWidth, nodeHeight);
addNode(nA);
addNode(nB);
addNode(nC);
setRootNode(nA);
connectNode(nA, nB);
connectNode(nA, nC);
for (NodesSSCCE n : nodeList) {
parents.addItem(n.getValue());
}
//send in selected parent from combo box
AppendChildren ac = new AppendChildren(child);
this.child.addActionListener(ac);
this.AddButton.addActionListener(ac);
}
class AppendChildren implements ActionListener {
private TextField child;
public AppendChildren(TextField child) {
this.child = child;
}
public void actionPerformed(ActionEvent ae) {
String childName = child.getText();
NodesSSCCE newChild = new NodesSSCCE(childName, nodeX, nodeY, nodeWidth, nodeHeight);
appendNode(rootNode, newChild);
}
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
public void paintComponent(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, width, height);
g.setColor(rootNode.getColor());
g.fillRect(rootNode.getX(), rootNode.getY(), rootNode.getWidth(), rootNode.getHeight());
g.setColor(Color.WHITE);
g.drawString(rootNode.getValue(), rootNode.getX()+9, rootNode.getY()+16);
paintComponent(g, rootNode);
}
public void paintComponent(Graphics g, NodesSSCCE parentNode) {
ArrayList<NodesSSCCE> nodePrintList = new ArrayList<NodesSSCCE>();
if (nodeList.indexOf(parentNode)==nodeList.size()) {
System.out.println("\nend");
}
else {
nodePrintList = getChildren(parentNode);
int x = parentNode.getX()-50;
//PAINT TREE: THIS IS NOT PAINTING THE NEWLY APPENDED NODE
for (NodesSSCCE child : nodePrintList) {
g.setColor(child.getColor());
child.setX(x);
child.setY(parentNode.getY()+50);
g.fillRect(child.getX(), child.getY(), child.getWidth(), child.getHeight());
g.setColor(Color.WHITE);
g.drawString(child.getValue(), child.getX()+9, child.getY()+16);
x+=50;
paintComponent(g, child);
g.setColor(child.getColor());
g.drawLine(parentNode.getX()+10, parentNode.getY()+23, child.getX()+10, child.getY());
}
//PAINT ADJACENCY MATRIX
for (int i = 0; i < adjMatrix.length; i++) {
for (int j = 0; j < adjMatrix[i].length; j++) {
g.setColor(Color.white);
g.drawString("" + adjMatrix[i][j], (i*19)+350, (j*19)+100);
g.setColor(Color.gray);
g.drawString("" + nodeList.get(i).getValue(), (i*19)+350, 75);
}
g.drawString("" + nodeList.get(i).getValue(), 325, (i*19)+100);
}
}
}
public void setRootNode(NodesSSCCE n) {
rootNode = n;
}
public NodesSSCCE getRootNode() {
return rootNode;
}
public void addNode(NodesSSCCE n) {
nodeList.add(n);
}
public void appendNode(NodesSSCCE parent, NodesSSCCE child) {
//add new node X to nodeList
addNode(child);
int newSize = nodeList.size();
//make a new adj matrix of the new size...
int[][] adjMatrixCopy = new int[newSize][newSize];
int fromNode = nodeList.indexOf(parent);
int toNode = nodeList.indexOf(child);
//copy adjMatrix data to new matrix...
for (int i = 0; i < adjMatrix.length; i++) {
for (int j = 0; j < adjMatrix[i].length; j++) {
adjMatrixCopy[i][j] = adjMatrix[i][j];
}
}
adjMatrixCopy[fromNode][toNode] = 1;
adjMatrix = adjMatrixCopy;
repaint();
}
public void connectNode(NodesSSCCE from, NodesSSCCE to)
{
if(adjMatrix == null) {
size = nodeList.size();
adjMatrix = new int[size][size];
}
int fromNode = nodeList.indexOf(from);
int toNode = nodeList.indexOf(to);
adjMatrix[fromNode][toNode] = 1;
}
public ArrayList<NodesSSCCE> getChildren (NodesSSCCE n) {
ArrayList<NodesSSCCE> childrenList;
childrenList = new ArrayList<NodesSSCCE>();
int index = nodeList.indexOf(n);
int col = 0;
while (col < size) {
if (adjMatrix[index][col] == 1) {
childrenList.add(nodeList.get(col));
}
col++;
}
return childrenList;
}
}
Nodes class:
import java.awt.Color;
import java.awt.Graphics;
public class NodesSSCCE {
Boolean visited;
String val;
Color color;
int xLoc, yLoc, width, height;
public NodesSSCCE(String s, int x, int y, int w, int h) {
visited = false;
val=s;
xLoc = x;
yLoc = y;
width = w;
height = h;
color = Color.gray;
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
public int getX() {
return xLoc;
}
public int getY() {
return yLoc;
}
public void setX(int x) {
xLoc = x;
}
public void setY(int y) {
yLoc = y;
}
public void visited(Boolean v) {
visited = v;
}
public String getValue() {
return val;
}
public void setValue(String value) {
val = value;
}
public void setColor (Color c) {
color = c;
}
public Color getColor () {
return color;
}
}
Nope, ignore my comment.
You forget to update size correctly.
In the GraphSSCCE, go to method getChildren(NodesSSCCE) and change the loop condition from col < size to col < nodeList.size().
Being able to debug your own programs can save a lot of time.