Reputation: 1439
After searching for more than 3 hours for similar problem and didn't find any clue i hope i will get my rescue hear. I'm trying to write a problem that stores in queue up to 5 lines that are draw inside JPanel using paintComponent() method. Also i want to update JLabel text to count size of queue. I'm trying to update the JLabel by setText method inside Jpanel.paintComponent() method. The problem that i have encountered and can't overcome on them are: 1. When paintComponent runs the line: label.setText("... "+ lineQueue.size()); inside DrawPanel class i get an exception means that label is equal to null. How does it possible if DrawPanel constructor initialized it? 2. In order to overcome first problem i put if (label != null) than label.setText("... " + lineQueue.size()) but label text isn't updated no matter what happen to JFrame. Can someone explain me what is the problem? It makes me crazy :(
My Code (4 files):
public class Point
{
private int x;
private int y;
public Point (int x, int y)
{
this.x = x;
this.y = y;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;
public class MyLine
{
private Point p1;
private Point p2;
private Color color;
public MyLine (Point point1 ,Point point2, Color color)
{
p1 = point1;
p2 = point2;
this.color = color;
}
public void drawLine (Graphics g)
{
g.setColor(color);
g.drawLine (p1.getX(),p1.getY(),p2.getX(),p2.getY());
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.util.*;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.util.Queue;
public class DrawPanel extends JPanel
{
private LinkedList<MyLine> lineQueue;
private JLabel label;
public DrawPanel(JLabel label)
{
label = this.label;
lineQueue = new LinkedList <MyLine>();
}
public void paintComponent (Graphics g)
{
super.paintComponent(g);
Random rand = new Random();
Point p1 = new Point (rand.nextInt(400),rand.nextInt(400));
Point p2 = new Point (rand.nextInt(400),rand.nextInt(400));
Color color = new Color (rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
if (lineQueue.size() >= 5)
lineQueue.remove();
lineQueue.add(new MyLine (p1,p2,color));
ListIterator<MyLine> iterator = lineQueue.listIterator(0);
while (iterator.hasNext() == true)
iterator.next().drawLine(g);
if (label!=null)
{
label.setText("... "+ lineQueue.size());
}
}
}
import java.awt.Color;
import javax.swing.*;
import java.awt.BorderLayout;
public class TestDrawLine
{
public static void main(String[] args)
{
JFrame frame = new JFrame ();
frame.setLayout(new BorderLayout());
frame.setSize(400,350);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel();
DrawPanel drawPanel = new DrawPanel(label);
drawPanel.setBackground(Color.BLACK);
frame.add (drawPanel,BorderLayout.CENTER);
JPanel southPanel = new JPanel();
southPanel.add (label);
frame.add (southPanel,BorderLayout.SOUTH);
frame.setVisible(true);
}
}
Upvotes: 1
Views: 173
Reputation: 691913
label = this.label;
This initializes the argument with the instance variable. You need to do the exact inverse:
this.label = label;
which initializes the instance variable with the argument.
In order to overcome first problem i put if (label != null) than label.setText("... " + lineQueue.size())
That can't possibly fix the issue. All this does is that it doesn't do anything anymore instead of causing an exception. The label is still null, and you're just not doing anything with it anymore.
That said: the paintComponent()
method is not supposed to modify the state of your component (or of anything else). It's only supposed to paint the component. That method will be called many times, whenever swing decides that the component needs to be repainted.
Upvotes: 2