Hi there, first time posting in here, what I am having an issue with is the JButtons only showing when the mouse hovers over them. I have created a grid in which the user can click and desired square and add a shape to it. However, once the user clicks and adds a shape the whole panel goes blank until the mouse is moved over it. I can't seem to fathom where I am going wrong. Any help will be greatly appreciated. N.B. The code below is a bit untidy as I want to get the main functionality outlined by the specification working first and ill tidy it up afterwards. Many thanks.
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.black);
Graphics2D g2d = (Graphics2D) g.create();
for (Rectangle r : grid) {
g2d.drawRect((int) r.getX(), (int) r.getY(), (int) r.getWidth(),
(int) r.getHeight());
}
if (selectedRectangle != null) {
g2d.setColor(Color.RED);
g2d.drawRect((int) selectedRectangle.getX(),
(int) selectedRectangle.getY(),
(int) selectedRectangle.getWidth(),
(int) selectedRectangle.getHeight());
}
for (IGizmo giz : gizmoList) {
if (giz instanceof CircleModel) {
g2d.setColor(Color.BLUE);
g2d.fillOval(giz.getX(), giz.getY(), giz.getWidth(),
giz.getHeight());
System.out.println("X: " + giz.getX() + ", Y: " + giz.getY()
+ ", Width: " + giz.getWidth() + ", Height: "
+ giz.getHeight());
}
if (giz instanceof Square) {
g2d.setColor(Color.RED);
g2d.fillRect(giz.getX(), giz.getY(), giz.getWidth(),
giz.getHeight());
System.out.println("X: " + giz.getX() + ", Y: " + giz.getY()
+ ", Width: " + giz.getWidth() + ", Height: "
+ giz.getHeight());
}
g2d.dispose();
}
public void rectangleClicked(Point mousePosition) {
int rectanglenNumber = 1;
for (Rectangle rect : grid) {
if (mousePosition.getX() < (rect.getX() + 20)
&& mousePosition.getX() > rect.getX()
&& mousePosition.getY() < (rect.getY() + 20)
&& mousePosition.getY() > rect.getY()) {
System.out.println("RECT " + rectanglenNumber + " CLICKED");
Rectangle rectToDraw = new Rectangle((int) rect.getX(),
(int) rect.getY(), (int) rect.getHeight(),
(int) rect.getWidth());
if (!selectedRectangle.equals(rectToDraw)) {
selectedRectangle = rectToDraw;
} else {
selectedRectangle = new Rectangle();
}
}
rectanglenNumber++;
}
super.repaint();
}
public void update(Observable o, Object arg) {
if (o instanceof Model) {
gizmoList = (List<IGizmo>) arg;
}
if (o instanceof RunMode) {
this.ball = (Ball) arg;
gizmoList.add(ball);
this.a = gm.getAbsorber();
gizmoList.add(a);
}
super.repaint();
}