As said similarly by others, always use JLabel
to display images. Thus, it is easy to add / remove them when necessary, instead of drawing. Moreover, in your code you override paint(...)
, for Swing
we prefer to override the paintComponent(...)
JComponent
corresponding JComponent
if the specified component has one.
Try this code here, I separated the Controller part, you can get an idea of ββhow to do this:
import java.awt.BorderLayout; import java.awt.Container; import java.awt.EventQueue; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.Icon; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.WindowConstants; public class New2 extends JFrame { private static String SHOW_ACTION = "show"; private static String HIDE_ACTION = "hide"; public New2(String filename) { setTitle("MyWindow"); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setSize(800, 600); Container container = getContentPane(); container.setLayout(new BorderLayout()); container.add(createControls(), BorderLayout.CENTER); } private JPanel createControls() { JButton showButton = new JButton("Show"); showButton.setActionCommand(SHOW_ACTION); JButton hideButton = new JButton("Hide"); hideButton.setActionCommand(HIDE_ACTION); JLabel imageLabel = new JLabel(); New2Controller n2c = new New2Controller(showButton , hideButton, imageLabel); showButton.addActionListener(n2c); hideButton.addActionListener(n2c); JPanel panel = new JPanel(); panel.setLayout(new FlowLayout(FlowLayout.CENTER)); panel.add(imageLabel); panel.add(showButton); panel.add(hideButton); return panel; } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { New2 frame = new New2("/img/image.jpg"); frame.setVisible(true); } }); } } class New2Controller implements ActionListener { private JButton showButton; private JButton hideButton; private JLabel imageLabel; private static String SHOW_ACTION = "show"; private static String HIDE_ACTION = "hide"; private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon"); public New2Controller(JButton show, JButton hide, JLabel label) { showButton = show; hideButton = hide; imageLabel = label; } public void actionPerformed(ActionEvent event) { String actionCommand = event.getActionCommand(); if (SHOW_ACTION.equals(actionCommand)) { SwingUtilities.invokeLater(new Runnable() { public void run() { imageLabel.setIcon(infoIcon); } }); } else if (HIDE_ACTION.equals(actionCommand)) { imageLabel.setIcon(null); } } }
This code shows how you read using ImageIO
and the URL
,
import java.awt.BorderLayout; import java.awt.Container; import java.awt.EventQueue; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; import javax.swing.WindowConstants; import javax.imageio.ImageIO; public class New2 extends JFrame { private static String SHOW_ACTION = "show"; private static String HIDE_ACTION = "hide"; public New2(String filename) { setTitle("MyWindow"); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setSize(800, 600); Container container = getContentPane(); container.setLayout(new BorderLayout()); container.add(createControls(), BorderLayout.CENTER); } private JPanel createControls() { JButton showButton = new JButton("Show"); showButton.setActionCommand(SHOW_ACTION); JButton hideButton = new JButton("Hide"); hideButton.setActionCommand(HIDE_ACTION); JLabel imageLabel = new JLabel(); New2Controller n2c = new New2Controller(showButton , hideButton, imageLabel); showButton.addActionListener(n2c); hideButton.addActionListener(n2c); JPanel panel = new JPanel(); panel.setLayout(new FlowLayout(FlowLayout.CENTER)); panel.add(imageLabel); panel.add(showButton); panel.add(hideButton); return panel; } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { New2 frame = new New2("/img/image.jpg"); frame.setVisible(true); } }); } } class New2Controller implements ActionListener { private JButton showButton; private JButton hideButton; private JLabel imageLabel; private Image image; private ImageIcon imageIcon; private static String SHOW_ACTION = "show"; private static String HIDE_ACTION = "hide"; public New2Controller(JButton show, JButton hide, JLabel label) { showButton = show; hideButton = hide; imageLabel = label; try { image = ImageIO.read(getClass().getResource("/img/caIcon.png")); } catch(Exception e) { e.printStackTrace(); } imageIcon = new ImageIcon(image); } public void actionPerformed(ActionEvent event) { String actionCommand = event.getActionCommand(); if (SHOW_ACTION.equals(actionCommand)) { SwingUtilities.invokeLater(new Runnable() { public void run() { imageLabel.setIcon(imageIcon ); } }); } else if (HIDE_ACTION.equals(actionCommand)) { imageLabel.setIcon(null); } } }
Also, when you use BorderLayout
, never use NORTH
, EAST
, WEST
and SOUTH
for BorderLayout. They have been replaced by PAGE_START
, LINE_START
, LINE_END
and PAGE_END
respectively.
The BorderLayout object has five areas. These areas are defined by BorderLayout constants:
- PAGE_START
- PAGE_END
- LINESTART
- LINE_END
- CENTER
Note to the version: before the release of JDK 1.4, the preferred names for different areas were different: from compass points (for example, BorderLayout.NORTH for the upper area) to longer versions of the constants that we use in our examples. The constants used by our examples are preferred because they are standard and allow programs to adapt to languages ββthat have different orientations.
Directory structure:
Your Project | | classes src | | img *.class(or package Folder)
Now use getClass().getResource("/img/star.png");