Tuesday, June 3, 2014

Java Swing enlarge image

Enlarge images in java with mouse clicked on image, and return to previous state when clicked on blank space. Maybe there is some more elegant solution, but I did this with java swing timer.

ImageEnlarge class:


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;

/**
*
* @author Anak1n
*/

public class ImageEnlarge extends JPanel implements ActionListener,
  MouseListener {

 private ImageIcon imageIcon;
 private Image image;

 private int width;
 private int heigth;
 private int imageHeigth;
 private int imageWidth;

 boolean mouseClickedInImageArea = false;

 Rectangle boundsOfImage;

 public ImageEnlarge() {
  
  imageHeigth = 174;
  imageWidth = 170;

  heigth = (600 - imageHeigth) / 2;
  width = (600 - imageWidth) / 2;

  imageIcon = new ImageIcon("images/one.jpg");
  image = imageIcon.getImage();

  boundsOfImage = new Rectangle(width, heigth, imageWidth, imageHeigth);

  addMouseListener(this);

  Timer timer = new Timer(5, this);

  timer.start();
 }

 public void paintComponent(Graphics g) {
  super.paintComponent(g);
  g.drawImage(image, heigth, width, imageWidth, imageHeigth, this);

 }

 @Override
 public void actionPerformed(ActionEvent arg0) {

  if (mouseClickedInImageArea) {
   if (imageHeigth < 494) {
    ++imageHeigth;
    heigth = (600 - imageHeigth) / 2;
   }
   if (imageWidth < 450) {
    ++imageWidth;
    width = (600 - imageWidth) / 2;
    
   }
   boundsOfImage = new Rectangle(width, heigth, imageWidth, imageHeigth);
   
   repaint();
  }

  else if (!mouseClickedInImageArea ) {
   if (imageHeigth > 174) {
    --imageHeigth;
    heigth = (600 - imageHeigth) / 2;
   }
   if (imageWidth > 170) {
    --imageWidth;
    width = (600 - imageWidth) / 2;
   }
   boundsOfImage = new Rectangle(width, heigth, imageWidth, imageHeigth);
   
   repaint();
  }

 }

 @Override
 public void mouseClicked(MouseEvent e) {

  if (boundsOfImage.contains(e.getX(), e.getY())) {
   mouseClickedInImageArea = true;
  } else
   mouseClickedInImageArea = false;
 }

 @Override
 public void mouseEntered(MouseEvent e) {

 }

 @Override
 public void mouseExited(MouseEvent e) {

 }

 @Override
 public void mousePressed(MouseEvent e) {
  

 }

 @Override
 public void mouseReleased(MouseEvent e) {
  

 }

}

Main class:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import javax.swing.JFrame;

/**
*
* @author Anak1n
*/

public class ImageMain {

 public static void main(String[] args) {

  JFrame frame = new JFrame();
  frame.setTitle("Image enlarge");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setSize(600, 600);
  ImageEnlarge enlarge = new ImageEnlarge();
  frame.add(enlarge);
  frame.setVisible(true);

 }

}

If it is too slow and you wish to enlarge image faster you can play with values on lines 63, 67 in ImageEnlarge class, and for restoring to previous state you can change values on lines 76,80, to speed up the process or to slow it, or to change timer value on line 47.

Note: You can create folder images and put in it image named one.jpg or chose path to image. Or you can load images on other ways.

Video:

1 comment:

  1. You should fetch the image width as imageWidth = image.getWidth(null);
    imageHeight = image.getHeight(null);

    ReplyDelete