I was working on this drawing program project, (more like, extending it) and I came upon a nasty bug. I added a JLabel on a JPanel, on the South of the BorderLayout, but not only it does not get added into the south of the JPanel, it also gets blurry after I paint something on the JPanel. What I do not understand is why these two things are happening.
Code of the Class:
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Painter extends JPanel{
private int pointCount = 0; // count the number of points
// array of 10000 java.awt.Point references
private Point[] points = new Point[10000];
JLabel myCount = new JLabel();
// set up GUI and register mouse event handler
public Painter() {
myCount.setText("Points so far: " + pointCount);
add(myCount, BorderLayout.SOUTH);
// handle frame mouse motion event
addMouseMotionListener(
new MouseMotionAdapter() { // anonymous inner class
public void mouseDragged(MouseEvent event) {
if (pointCount < points.length) {
points[pointCount] = event.getPoint(); // find the point
++pointCount; // increment number of points in the array
repaint();
myCount.setText("Points so far: " + pointCount);
} // end if
} // end of mouseDragged method
} // end anonymous inner class
); // end of addMouseMotionListener
}// end Painter constructor
// draw ovals in a 4-by-4 bounding box at specified locations on window
public void paintComponent(Graphics g) {
super.paintComponents(g); // clears drawing area
// draw all points in the array
for (int i = 0; i < pointCount; i++) {
g.fillOval(points[i].x, points[i].y, 12, 12);
} // end for loop
} // end method paintComponent
} // end of Painter class
First problem can be solved by calling setLayout(new BorderLayout); as the first line of your constructor
2nd problem can be solved by removing the 's' from paintComponents and just calling paintComponent