// Mr Greenstein
// March 17, 2014
// ShowImage.java
// This program shows:
//
// 1. Using a null layout
// 2. loading an image from a file and displays it to the screen.
// 3. implementing a JLabel component
import java.awt.*; // for classes Image, Graphics, Color
import java.awt.event.*; // for classes KeyListener, MouseListener
import javax.swing.*; // for class JFrame
////////////////////////////////////////////////////////
// 2. Add imports for classes File, IOExcaption, ImageIO
//
public class ShowImage
{
private JFrame frame;
private DrawingArea canvas; // JPanel to draw images
////////////////////////////////////////////////////////
// 2. Declare image object
////////////////////////////////////////////////////////
// 3. Declare JLabel object
private int xpos, ypos;
private boolean keyClear;
private int sizeX = 217, sizeY = 301; // Calvin
////////////////////////////////////////////////////////
// 2. Provide Filename
public ShowImage ( )
{
xpos = ypos = 220; // center the picture in the frame
keyClear = true;
}
public static void main (String[] args)
{
ShowImage si = new ShowImage();
si.Run();
}
public void Run( )
{
// Create the JFrame
frame = new JFrame("ShowImage");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
////////////////////////////////////////////////////////
// 1. Set the JFrame to a null layout
////////////////////////////////////////////////////////
// 3. Initialize JLabel and add to frame
// Create the JPanel canvas
canvas = new DrawingArea ( );
canvas.setBackground( Color.gray );
// Get the Image from a file
GetMyImage();
frame.getContentPane().add( canvas );
frame.addKeyListener(canvas);
////////////////////////////////////////////////////////
// 1. Set the location and size of the canvas
////////////////////////////////////////////////////////
// 3. Set the location and size of the label
frame.setBackground(Color.gray);
frame.setSize(800, 800);
frame.setLocation(300, 0);
frame.setResizable(false);
frame.setVisible(true);
}
public void GetMyImage()
{
///////////////////////////////////////////////////
// 2. Create a try-catch block for loading the image
}
// canvas
class DrawingArea extends JPanel implements MouseListener, KeyListener
{
public DrawingArea ( )
{
addMouseListener (this);
addKeyListener (this);
}
public void paintComponent ( Graphics g )
{
if (keyClear) super.paintComponent ( g ); // blank the canvas
///////////////////////////////////////////////////////////
// 2. Draw the image that was loaded
keyClear = false;
}
// Mouse methods
public void mousePressed ( MouseEvent e )
{
xpos = e.getX ( ) - 135;
ypos = e.getY ( ) - 90;
repaint ( );
}
public void mouseClicked ( MouseEvent e ) {}
public void mouseReleased ( MouseEvent e ) {}
public void mouseEntered ( MouseEvent e ) {}
public void mouseExited ( MouseEvent e ) {}
// Key methods
public void keyPressed (KeyEvent e)
{
int code = e.getKeyCode();
// pressing space or shift will clear the screen
if (code == 32 || code == 16)
{
keyClear = true;
repaint();
}
}
public void keyTyped (KeyEvent e) {}
public void keyReleased (KeyEvent e) {}
}
}
To Java Main Page