Hey everyone, okay so im pretty new to java, i normally use c but now im learning java...
What i need to do is open and select an image then display it the panel but am not sure how to do this..
this is what i have so far:
import java.io.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.awt.print.*; import javax.swing.*; import javax.swing.filechooser.*; import javax.imageio.*; public class assignment1 extends JPanel implements ActionListener { JLabel label = new JLabel(); JMenuItem openItem, quitItem; BufferedImage image = null; public static void main(String[] args) { new assignment1(); } public void actionPerformed(ActionEvent event) { JMenuItem source = (JMenuItem)event.getSource(); if (source == openItem){ JFileChooser chooser = new JFileChooser(); chooser.setAcceptAllFileFilterUsed(false); chooser.setFileFilter(new ImageFilter()); int retVal = chooser.showOpenDialog(this); if (retVal == JFileChooser.APPROVE_OPTION) { File myFile = chooser.getSelectedFile(); try { image = ImageIO.read(myFile); } catch( IOException e ){ e.printStackTrace(); } } } else if (source == quitItem){ System.out.println("Quitting...."); System.exit(0); } } public assignment1(){ JFrame frame = new JFrame("make window"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400,200); //JPanel panel = new JPanel(); //add menu bar. JMenuBar menuBar = new JMenuBar(); frame.setJMenuBar(menuBar); //FILE MENU !! JMenu fileMenu = new JMenu("File"); menuBar.add(fileMenu); //OPEN ITEM TOOL openItem = new JMenuItem("Open"); openItem.addActionListener(this); fileMenu.add(openItem); //CLOSE ITEM TOOL quitItem = new JMenuItem("Quit Item"); quitItem.addActionListener(this); fileMenu.add(quitItem); frame.setVisible(true); ImageDemo panel = new ImageDemo(); frame.setContentPane(panel); // JPanel panel = new JPanel(); // panel.setVisible(true); } public void paintComponent( Graphics g ){ if( image != null ){ Graphics2D g2 = (Graphics2D)g; System.out.println("Calling paint"); g2.drawImage( image, 0, 0, null ); } } }