aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Magnifying only a part of the image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Magnifying only a part of the image" Watch "Magnifying only a part of the image" New topic
Author

Magnifying only a part of the image

Sanam Chandrasekaran
Greenhorn

Joined: May 05, 2004
Posts: 17
Hello,

I have a BufferedImage displayed on an applet,
I wnat a magnifier ,
so that when i move the magnifier on the image only that part of the image
on which the magnifier is there is magnified.

Could anyone plz help me out with this.
It will be kindful if anyone could get me the code.

Plz help.


Regards
Sanam
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Sanam Chandrasekaran
Greenhorn

Joined: May 05, 2004
Posts: 17
Thanks a lot for the reply, ill try it out n let u know.
Sanam Chandrasekaran
Greenhorn

Joined: May 05, 2004
Posts: 17
this is the code i have written ,

u can try out the code by just changing the image name ,

when i click on the magnifier, i want the magnifier tool to appear,
and even all the buttons should appear.

can u please tell how do i club ur code into mine.

it would be kindful if u could help me with this.


import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.util.Vector;
import java.applet.*;

import java.awt.image.*;
import javax.swing.*;
import javax.swing.event.*;


// **************************************** ZoominApplet1 class ***************************************
public class ZoominApplet2 extends JApplet
{
public static JFrame f;
public AppletZoomPanel2 zoomPanel;
public static Container cp;
public BufferedImage image;
JTextArea textArea;
public Graphics g;


public void init()
{
zoomPanel = new AppletZoomPanel2();
ZoomAction2 action = new ZoomAction2(zoomPanel);

cp = getContentPane();
cp.setLayout(new BorderLayout());
cp.add(action, "North");
cp.add(new JScrollPane(zoomPanel));
txtArea();

}


public void txtArea()
{

zoomPanel.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
textArea = new JTextArea();
textArea.setBorder(BorderFactory.createLineBorder(Color.BLACK));
zoomPanel.add(textArea);
textArea.setBounds(e.getX(), e.getY(), 100,50);
}
});

SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
f.pack();
f.show();
}
});
}

public static void main(String[] args)
{
JApplet applet = new ZoominApplet2();
f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(applet);
f.setSize(300,300);
//f.setLocation(100,100);
applet.init();
f.setVisible(true);

}
}



// *********************************** AppletZoompanel class ******************************
class AppletZoomPanel2 extends JPanel
{

public BufferedImage image;
public double scale, scaleInc;
public boolean autofit = false;

public String actionType = "";
public String drawType ="";
private double currentAngle;

public int box_x = 0;
public int box_y = 0;
public int box_width = 100;
public int box_height = 100;
public int width = 900;
public int height = 900;


Graphics g = getGraphics();


public AppletZoomPanel2()
{

loadImage();

scale = 1.0;
scaleInc = 0.01;
setBackground(Color.white);
//Set to null
setLayout(null);
repaint();
}


protected void paintComponent(Graphics g)
{
super.paintComponent(g);

if(actionType.equals("Auto Fit"))
{

Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
int parentWidth = getParent().getWidth();
int parentHeight = getParent().getHeight();
double imageWidth = image.getWidth();
double imageHeight = image.getHeight();
double newImageHeight;
double newImageWidth;

if(parentHeight < parentWidth)
{
newImageHeight = parentHeight;
newImageWidth = (newImageHeight * imageWidth) / imageHeight;

double scaleX = newImageWidth / imageWidth ;

double scaleY = newImageHeight / imageHeight ;

AffineTransform xform1 = AffineTransform.getTranslateInstance(0, 0);
xform1.scale(scaleX,scaleY);
g2.drawRenderedImage(image, xform1);
}
else
{
newImageWidth = parentWidth;
newImageHeight = (newImageWidth * imageHeight) / imageWidth;
double scaleX = newImageWidth / imageWidth ;
double scaleY = newImageHeight / imageHeight ;
AffineTransform xform1 = AffineTransform.getTranslateInstance(0, 0);
xform1.scale(scaleX,scaleY);
g2.drawRenderedImage(image, xform1);
}
}
else if(actionType.equals("Not Auto Fit"))
{

Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
int w = getWidth();

int h = getHeight();

double imageWidth = scale * image.getWidth();
double imageHeight = scale * image.getHeight();

double x = (w - imageWidth)/2;
double y = (h - imageHeight)/2;

AffineTransform xform = AffineTransform.getTranslateInstance(x, y);
xform.scale(scale, scale);
g2.drawRenderedImage(image, xform);
}

else if(actionType.equals("Rotate"))
{
Graphics2D g2 = (Graphics2D)g;
AffineTransform origXform = g2.getTransform();
AffineTransform newXform = (AffineTransform)(origXform.clone());
//center of rotation is center of the panel
int xRot = this.getWidth()/2;
int yRot = this.getHeight()/2;
newXform.rotate(Math.toRadians(currentAngle), xRot, yRot);
g2.setTransform(newXform);
//draw image centered in panel
int x = (getWidth() - image.getWidth(this))/2;
int y = (getHeight() - image.getHeight(this))/2;
g2.drawImage(image, x, y, this);
g2.setTransform(origXform);

}


else if(actionType.equals("Magnify"))
{

}
else
{
Graphics2D g11 = (Graphics2D)g;
g11.drawImage(image, null, 100, 100);
}
}




public void rotate()
{
//Rotate five degrees at a time
currentAngle+=5.0;
if (currentAngle >= 360.0)
{
currentAngle = 0;
}
repaint();
}

public Dimension getPreferredSize()
{
Dimension d = new Dimension();
d.width = (int)(scale * image.getWidth());
d.height = (int)(scale * image.getHeight());
return d;
}


public void setScale (int inc)
{
scale += inc * scaleInc;
revalidate();
repaint();
}

private void loadImage()
{
String fileName = "plot4_g.gif";
try
{
URL url = getClass().getResource(fileName);
image = ImageIO.read(url);
}
catch(MalformedURLException mue)
{
System.out.println("url: " + mue.getMessage());
}
catch(IOException ioe)
{
System.out.println("read: " + ioe.getMessage());
}
}
}


// ******************************* ZoomAction Class **************************************
class ZoomAction2 extends JPanel
{
static final String[] fontStyleString = new String[] {"Font.PLAIN", "Font.ITALIC", "Font.BOLD", "Font.ITALIC+Font.BOLD"};
static final int[] fontStyleInt = new int[] { Font.PLAIN , Font.ITALIC , Font.BOLD , Font.ITALIC+Font.BOLD };
AppletZoomPanel2 zoomPanel;


public ZoomAction2(AppletZoomPanel2 azp)
{
zoomPanel = azp;

final JButton zoomIn = new JButton("zoom in"), zoomOut = new JButton("zoom out"), magnifier = new JButton("Magnifier"), autoFit = new JButton("Auto Fit"), rotate = new JButton("Rotate"), txtButton = new JButton("Text Area");

JComboBox fontCombo;
JComboBox styleCombo;
String[] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();

Vector visFonts = new Vector(fontNames.length);
for(int i=0; i<fontNames.length; i++)
{
Font f = new Font(fontNames[i], Font.PLAIN, 12);
if (f.canDisplay('a'))
{
visFonts.add(fontNames[i]);
}
else
{
//System.out.println("No alphabetics in " + fontNames[i]);
}
}

fontCombo = new JComboBox(visFonts);
styleCombo = new JComboBox(fontStyleString);

ActionListener l = new ActionListener()
{
int inc;

public void actionPerformed(ActionEvent e)
{
JButton button = (JButton)e.getSource();
zoomPanel.actionType = "Not Auto Fit";

if(button == zoomIn)
inc = 5;
if(button == zoomOut)
inc = -5;
zoomPanel.setScale(inc);
//zoomPanel.repaint();
}
};
zoomIn.addActionListener(l);
zoomOut.addActionListener(l);


ActionListener af = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JButton button = (JButton)e.getSource();

if(button == autoFit)
{
//System.out.println("autofit");
//System.out.println("ZoomPanel Parent Width: " +zoomPanel.getParent().getWidth());
zoomPanel.actionType = "Auto Fit";
zoomPanel.repaint();
}
/*if(button == txtButton)
{

}*/

}
};

autoFit.addActionListener(af);
txtButton.addActionListener(af);

ActionListener ro = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JButton button = (JButton)e.getSource();

if(button == rotate)
{
zoomPanel.actionType = "Rotate";
zoomPanel.rotate();
//zoomPanel.repaint();
}
}
};
rotate.addActionListener(ro);


ActionListener ma = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JButton button = (JButton)e.getSource();

if(button == magnifier)
{/*
zoomPanel.actionType = "Magnify";
System.out.println("Magnify");
zoomPanel.repaint();
*/

//MagnifierTest mm = new MagnifierTest();
}
}
};
magnifier.addActionListener(ma);

add(txtButton);
add(zoomIn);
add(zoomOut);
add(magnifier);
add(autoFit);
add(rotate);
add(fontCombo);
add(styleCombo);

}
}
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
You have some bits and pieces gathered together. Time to step back and decide what you want to do in your applet. What do you want this component to do? How do you want the user to be able to use it? This is more about imagination. It could be to demonstrate different ways to manipulate an image, or an image editor of sorts or just about anything you want.

Let’s say you want to build an editor. You could allow the user to select any operation or series of operations from a menu and then present the component editor that does that. For instance, say you want to allow the user to zoom, clip and save an image. You could load up a component that provides zooming, clipping to a selected Shape and saving the result to disc.

To do this you might consider alternatives like CardLayout, JTabbedPane or adding/removing components to/from the gui. What is it that best suits what you want to do? Once you decide the overall design the details will fall into line. Sometimes it may be useful to create a small demo app to explore and/or check your concept. This may help unpleasant "oh-oh" surprises down the line.

The component editors you present with each selection can be complete classes or a JPanel that you put together somewhere. There are lots of possibilities and how you put things together seems to be a big part of the fun.
Sanam Chandrasekaran
Greenhorn

Joined: May 05, 2004
Posts: 17
Thanks a lot for ur reply n ur suggestion.
Actuall what my applet has to do is,
i have an applet which displays an image when it is loaded,
n the following buttons have to be displayed at the top
of the panel


1. magnifier button - when i click on magnifier button , it has to display the magnifier on the image(same as the code u had sent)
2. zoom in button - the image has to be zoomed
3. joom out button
4. auto fit button- window as to auto fit in the window size
5. rotate button - the image has to rotate.

I am not able to get the modifier work when i click on the modifier button.

At all times no matter what oeration , i want all the buttons to be displayed.

Can u please tell how can i get the magnifier when i click on the magnification button.

Rest all operations r working fine.

I am just not able to get the modifier when i click on modification button.

Please help
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Changes to the code you posted:

Where MP and MC are:
Sanam Chandrasekaran
Greenhorn

Joined: May 05, 2004
Posts: 17
Hello,

Thanks a lot, It was a great help.
The magnifier works absolutely fine.

Thankyou.

Regards
Sanam
Sanam Chandrasekaran
Greenhorn

Joined: May 05, 2004
Posts: 17
Hello,


Can please help me with this,

i am stuck up with a wnother problem.
It would be kindful if u could help me out with this.


i.e when i click on the mask button a mask(Rectangle box) should appear that can be resized , so that i can place it on any location
on the image. so that i can modify that part of the image ,
such as i should be able to change the color etc ( it should function like a mask)


Plz help. i dono how to do it
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
1 — you can use an actionType in your paintComponent code that allows you to draw a Rectangle over the image
2 — you create the Rectangle by dragging the mouse over the image with code in the mouseDragged method of your MouseListener
3 — once you have placed the Rectangle you can use the techniques shown in the tutorial page given in your other thread Mask on Image to create an Area that covers the image minus the Rectangle you drew on it
4 — you can set the paint color before you fill the Area in paintComponent
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Magnifying only a part of the image