Introduction

Image control is superclass for all image classes representing graphical images.

Class declaration

Following is the declaration for java.awt.Image class:

  1. public abstract class Image
  2. extends Object

Field

Following are the fields for java.awt.Image class:

  • protected float accelerationPriority — Priority for accelerating this image.

  • static int SCALE_AREA_AVERAGING — Use the Area Averaging image scaling algorithm.

  • static int SCALE_DEFAULT — Use the default image-scaling algorithm.

  • static int SCALE_FAST — Choose an image-scaling algorithm that gives higher priority to scaling speed than smoothness of the scaled image.

  • static int SCALE_REPLICATE — Use the image scaling algorithm embodied in the ReplicateScaleFilter class.

  • static int SCALE_SMOOTH — Choose an image-scaling algorithm that gives higher priority to image smoothness than scaling speed.

  • static Object UndefinedProperty — The UndefinedProperty object should be returned whenever a property which was not defined for a particular image is fetched.

Class constructors

S.N.Constructor & Description
1 Image()

Class methods

S.N.Method & Description
1 void flush() Flushes all reconstructable resources being used by this Image object.
2 float getAccelerationPriority() Returns the current value of the acceleration priority hint.
3 ImageCapabilities getCapabilities(GraphicsConfiguration gc) Returns an ImageCapabilities object which can be inquired as to the capabilities of this Image on the specified GraphicsConfiguration.
4 abstract Graphics getGraphics() Creates a graphics context for drawing to an off-screen image.
5 abstract int getHeight(ImageObserver observer) Determines the height of the image.
6 abstract Object getProperty(String name, ImageObserver observer) Gets a property of this image by name.
7 Image getScaledInstance(int width, int height, int hints) Creates a scaled version of this image.
8 abstract ImageProducer getSource() Gets the object that produces the pixels for the image.
9 abstract int getWidth(ImageObserver observer) Determines the width of the image.
10 void setAccelerationPriority(float priority) Sets a hint for this image about how important acceleration is.

Methods inherited

This class inherits methods from the following classes:

  • java.lang.Object

Image Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui > AwtControlDemo.java

  1. package com.tutorialspoint.gui;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. public class AwtControlDemo {
  7.  
  8. private Frame mainFrame;
  9. private Label headerLabel;
  10. private Label statusLabel;
  11. private Panel controlPanel;
  12.  
  13. public AwtControlDemo(){
  14. prepareGUI();
  15. }
  16.  
  17. public static void main(String[] args){
  18. AwtControlDemo awtControlDemo = new AwtControlDemo();
  19. awtControlDemo.showImageDemo();
  20. }
  21.  
  22. private void prepareGUI(){
  23. mainFrame = new Frame("Java AWT Examples");
  24. mainFrame.setSize(400,400);
  25. mainFrame.setLayout(new GridLayout(3, 1));
  26. mainFrame.addWindowListener(new WindowAdapter() {
  27. public void windowClosing(WindowEvent windowEvent){
  28. System.exit(0);
  29. }
  30. });
  31. headerLabel = new Label();
  32. headerLabel.setAlignment(Label.CENTER);
  33. statusLabel = new Label();
  34. statusLabel.setAlignment(Label.CENTER);
  35. statusLabel.setSize(350,100);
  36.  
  37. controlPanel = new Panel();
  38. controlPanel.setLayout(new FlowLayout());
  39.  
  40. mainFrame.add(headerLabel);
  41. mainFrame.add(controlPanel);
  42. mainFrame.add(statusLabel);
  43. mainFrame.setVisible(true);
  44. }
  45.  
  46. private void showImageDemo(){
  47. headerLabel.setText("Control in action: Image");
  48.  
  49. controlPanel.add(new ImageComponent("resources/java.jpg"));
  50. mainFrame.setVisible(true);
  51. }
  52.  
  53. class ImageComponent extends Component {
  54.  
  55. BufferedImage img;
  56.  
  57. public void paint(Graphics g) {
  58. g.drawImage(img, 0, 0, null);
  59. }
  60.  
  61. public ImageComponent(String path) {
  62. try {
  63. img = ImageIO.read(new File(path));
  64. } catch (IOException e) {
  65. e.printStackTrace();
  66. }
  67. }
  68.  
  69. public Dimension getPreferredSize() {
  70. if (img == null) {
  71. return new Dimension(100,100);
  72. } else {
  73. return new Dimension(img.getWidth(), img.getHeight());
  74. }
  75. }
  76. }
  77. }

Compile the program using command prompt. Go to D:/ > AWT and type the following command.

  1. D:\AWT>javac com\tutorialspoint\gui\AwtControlDemo.java

If no error comes that means compilation is successful. Run the program using following command.

  1. D:\AWT>java com.tutorialspoint.gui.AwtControlDemo

Verify the following output

awt_image.jpg