PICTURES and BUTTONS
This demo program shows how to do all the following:
(1) place AWT Button controls on the the programs form window
(2) respond to Button clicks - the action method
(3) display an image on the programs form window
(4) display an image in a pop-up window
\************************************************************************/
import java.awt.*;
import javax.swing.*;
Button b1 = new Button("Show Picture");
Button b2 = new Button("Pop-up Picture");
Font big = new Font("Arial",0,36);
void setup()
{
size(600,400); //
change size of standard window
b1.setFont(big); // change font on
the Button b1
b2.setFont(big); // change font on
Button b2
add(b1);
// display Button b1
add(b2);
// display Button b2
}
boolean action(java.awt.Event evt, Object src)
{
Object source = evt.target;
// this is the Button that was clicked
if(source==b1)
//
shows a web image on the form window
{
PImage pic =
loadImage("https://www.google.com/images/srpr/logo11w.png");
image(pic,100,100,200,240);
repaint();
//
updates the visible window
}
if(source==b2)
{
// This works if the picture file is
stored on the local hard disk
// in the Documents folder. This
should work on Linux, Windows and Mac OS.
// Of course the picture can have a
different name than "picture.jpg"
// If you prefer a "hard-coded path", try
the following:
// new
PictureFrame(file:c:/folder/picture.jpg"); // on Windows
machines
// new
PictureFrame("file:///users/username/folder/picture.jpg"); // on
Mac OS
// new
PictureFrame("file:///home/username/folder/picture.jpg"); //
on
Ubuntu Linux
PictureFrame pf2 = new
PictureFrame("file:"+System.getProperty("user.home")+"/Documents/picture.jpg");
pf2.setBounds(150,150,500,500);
//
For more information on various System properties, try this link:
// http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html
}
return true;
}
class PictureFrame extends JFrame // a class to create a
popup
{
//
window showing a picture
JLabel
pic;
//
can be a URL or a disk path
public PictureFrame(String name)
{
try
{
setSize(400,400);
setVisible(true);
Icon ii = new ImageIcon(new
java.net.URL(name));
pic = new
JLabel("",ii,JLabel.LEFT);
pic.setSize(300,400);
this.getContentPane().add(pic);
}
catch(Exception ex)
{ }
}
}