change background color on mouse event
Hi there,
This seems like a really dumb question, which is why i'm so frustrated at the moment, can anybody explain why, when the mouse enters the an imagePanel, it doesn't change colour?
Any help is appreciated,
Thanks
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
publicclass GridBagLayoutDemo{
publicstaticvoid main(String[] args){
create();
}
publicstaticvoid create(){
JFrame frame =new JFrame();
JLabel titleLabel =new JLabel("Title: ");
JLabel storyLabel =new JLabel("Story: ");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel =new JPanel();
frame.add(panel);
panel.setLayout(new BorderLayout());
JPanel titlePanel =new JPanel();
JPanel mainImagePanel =new JPanel();
titlePanel.setBackground(Color.RED);
titlePanel.setLayout(new GridLayout(2,0));
panel.add(titlePanel,BorderLayout.PAGE_START);
panel.add(mainImagePanel);
mainImagePanel.setLayout(new GridLayout(2,5));
imagePanel imagePanel0 =new imagePanel();
imagePanel imagePanel1 =new imagePanel();
imagePanel imagePanel2 =new imagePanel();
imagePanel imagePanel3 =new imagePanel();
imagePanel imagePanel4 =new imagePanel();
imagePanel imagePanel5 =new imagePanel();
imagePanel imagePanel6 =new imagePanel();
imagePanel imagePanel7 =new imagePanel();
imagePanel imagePanel8 =new imagePanel();
imagePanel imagePanel9 =new imagePanel();
mainImagePanel.add(imagePanel0);
mainImagePanel.add(imagePanel1);
mainImagePanel.add(imagePanel2);
mainImagePanel.add(imagePanel3);
mainImagePanel.add(imagePanel4);
mainImagePanel.add(imagePanel5);
mainImagePanel.add(imagePanel6);
mainImagePanel.add(imagePanel7);
mainImagePanel.add(imagePanel8);
mainImagePanel.add(imagePanel9);
titlePanel.add(titleLabel);
titlePanel.add(storyLabel);
mainImagePanel.setSize(550, 275);
int height = (mainImagePanel.getHeight() + titlePanel.getHeight());
int width = (mainImagePanel.getWidth());
System.out.println(height);
System.out.println(width);
imagePanel0.setToolTipText("One");
imagePanel1.setToolTipText("Two");
imagePanel2.setToolTipText("Three");
imagePanel3.setToolTipText("Four");
imagePanel4.setToolTipText("Five");
imagePanel5.setToolTipText("Six");
imagePanel6.setToolTipText("Seven");
imagePanel7.setToolTipText("Eight");
imagePanel8.setToolTipText("Nine");
imagePanel9.setToolTipText("Ten");
frame.setSize(width, height);
frame.show();
}
}
class imagePanelextends JPanelimplements MouseListener
{
publicvoid paint(Graphics g)
{
Image image = Toolkit.getDefaultToolkit().getImage("/home/University Work/Java/src/Picture.jpg");
this.setSize(image.getWidth(this) + 10,image.getHeight(this) + 10);
g.drawImage(image,5,5,this);
addMouseListener(this);
}
publicvoid mousePressed(MouseEvent e)
{
System.out.println("Pressed");
}
publicvoid mouseReleased(MouseEvent e)
{
System.out.println("Released");
}
publicvoid mouseEntered(MouseEvent e)
{
this.setBackground(Color.BLUE);
}
publicvoid mouseExited(MouseEvent e)
{
System.out.println("Exited");
}
publicvoid mouseClicked(MouseEvent e)
{
System.out.println("Clicked");
}
}

