问题
I think that the reason why my paintComponent is not working because I placed the image as a JLabel in a JPanel. I want to use drawRect to draw a rectangle when I click using the MouseListener.
My image is a BufferedImage from JFileChooser
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode (JFileChooser.FILES_AND_DIRECTORIES);
if (fileChooser.showOpenDialog(panelPic) == JFileChooser.APPROVE_OPTION)
{
try
{
pic = ImageIO.read(fileChooser.getSelectedFile());
//reading the image of the selected file
} catch (IOException ioe)
{
JOptionPane.showMessageDialog(panelPic, "Error Reading File!");
}
System.out.println("getFileOrDirectory exited");
}
else if (fileChooser.showOpenDialog(panelPic) == JFileChooser.CANCEL_OPTION)
System.exit(1);
return pic;
then my picture would have go through this
pic = getFileOrDirectory();
picLabel = new JLabel (new ImageIcon(pic));
panelPic.add(picLabel);
System.out.println("picLabel added to panel");
repaint();
frame.validate();
panelPic is where I would like the picture to be seen. then this is my code for the paint that doesn't really worked -_- so I erased the drawImage method that isn't really working for me.
public void paint(Graphics g)
{
System.out.println("paint entered"); // just a checker if the program passed through this method
super.paint(g);
g.setColor(Color.BLACK);
g.drawRect(x-50, y-50, 100, 100);
frame.validate();
}
I would like to know how to fix this problem.
UPDATED!! This is the SSCCE of my code
public class TrialCode extends JPanel implements MouseListener, ActionListener
{ JFrame frame;
JPanel panelPic;
BufferedImage pic;
JButton getImg;
public TrialCode ()
{
frame = new JFrame("Java Project");
panelPic = new JPanel();
getImg = new JButton("Get Image");
frame.getContentPane().add(BorderLayout.CENTER, panelPic);
frame.setSize(900, 700);
frame.setVisible(true);
panelPic.addMouseListener(this);
getImg.addActionListener(this);
}
public void paint(Graphics g)
{
System.out.println("paintComponent entered"); // checker
super.paint(g);
g.setColor(Color.BLACK);
g.drawRect(x-50, y-50, 100, 100);
}
private BufferedImage getFileOrDirectory()
{
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode (JFileChooser.FILES_AND_DIRECTORIES);
if (fileChooser.showOpenDialog(panelPic) == JFileChooser.APPROVE_OPTION)
{
try
{
pic = ImageIO.read(fileChooser.getSelectedFile());
} catch (IOException ioe)
{
JOptionPane.showMessageDialog(panelPic, "Error Reading File!");
}
System.out.println("getFileOrDirectory exited");
}
else if (fileChooser.showOpenDialog(panelPic) == JFileChooser.CANCEL_OPTION)
System.exit(1);
return pic;
}
public void showImage()
{
pic = getFileOrDirectory();
picLabel = new JLabel (new ImageIcon(pic));
panelPic.add(picLabel);
repaint();
frame.validate();
}
public static void main (String [] args)
{
new TrialCode();
}
public void actionPerformed(ActionEvent event)
{
inputImage();
}
@Override
public void mouseClicked(MouseEvent e)
{
// TODO Auto-generated method stub
x = e.getX();
y = e.getY();
repaint();
inputName = JOptionPane.showInputDialog(frame, "Enter Name");
// when click the retangle should appear
}
来源:https://stackoverflow.com/questions/8344108/why-does-my-paintcomponent-doesnt-work