O'Reilly logo

Java™ After Hours: 10 Projects You'll Never Do at Work by Steven Holzner

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Handling Menu Selections

The actionPerformed method handles menu selections, setting the appropriate drawing flags as needed. This method handles the File menu's Open item by using the ImageIO class to open an image file:

public void actionPerformed(ActionEvent e)
{
    if(e.getSource() == openMenuItem){
        try{
					dialog.setMode(FileDialog.LOAD);
					dialog.setVisible(true);
					if(dialog.getFile() != ""){
					File inputFile = new File(dialog.getDirectory() +
					dialog.getFile());
					bufferedImage = ImageIO.read(inputFile);
					if(bufferedImage != null){
					image = createImage(bufferedImage.getWidth(),
					bufferedImage.getHeight());
					Graphics2D g2d = (Graphics2D)
					image.getGraphics();
					g2d.drawImage(bufferedImage, null, 0, 0);
					imageWidth = bufferedImage.getWidth();
					imageHeight = bufferedImage.getHeight(); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required