Win a copy of Raising Young Coders: A Parent’s Guide to Teaching Programming at Home this week in the General Computing forum!
Forums Login/signup

Code to print , rotate, zoom an image from an applet

+Pie Number of slices to send: Send
HI,
I am in need of applet code to print an image and also the zooming and rotation of the images in the applet.
I tried with samples which is giving problem like image will be cleared from screen when print preview window is displayed and if i cancel the print preview window, it is displayed continuously..infinite loop.
I would really appreciate if i get solution for this because this is need for an hour.
+Pie Number of slices to send: Send
It's not clear what you have so far, what you need done, and what exactly is the problem you're struggling with. Without seeing some code, preferably an SCCEE, I don't think we will be in a position to help.
+Pie Number of slices to send: Send
Hi please find the code where i am able to do rotation,etc only print option is left.. i am getting only first button on the print copy.


import javax.imageio.ImageIO;

import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.ByteArrayInputStream;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import javax.swing.*;
import javax.swing.border.Border;

import java.net.*;










public class First extends JApplet{

private static BufferedImage clouds[]=new BufferedImage[2];
private JScrollPane scroll;
URL url = null;
URLConnection servletConnection = null;
private int flag = 0;
//private int scale = 1;
private int newAngle = 0;
private BufferedImage temp;
private int zoomState=0;
Scrollbar slider;
int sliderValue;
int number=0;
// PrintImagePanel fImgPanel;



/** The customer header comp. */
// private CustomerHeaderComp customerHeaderComp;

public void init() {
File input = new File("D:\\Pooja\\flower030.gif");
File input1=new File("D:\\Pooja\\rose.jpg");


/*String image1 = getParameter("image1");
String image2 = getParameter("image2"); */

try {




// url = new URL(getCodeBase(), "rose.jpg");
// URL imageURL = new URL(getCodeBase()+"/ChequeImgRetrievalServlet");
// String base64encJPG = (String)request.getParameter("data");
// byte decoded[] = new sun.misc.BASE64Decoder().decodeBuffer(image1);
// byte img1by[] = image1.getBytes();
// InputStream in = new ByteArrayInputStream(img1by);
// BufferedImage img = ImageIO.read(imageURL);
// clouds[0] = img;

// url = new URL(getCodeBase(), "ff.jpg");
// byte decoded1[] = new sun.misc.BASE64Decoder().decodeBuffer(image2);
// byte img2by[] = image2.getBytes();
// InputStream in1 = new ByteArrayInputStream(img2by);
// img = ImageIO.read(url);
// clouds[1] = img;
clouds[0] = ImageIO.read(input);
clouds[1]=ImageIO.read(input1);


} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Demo demo = new Demo();
getContentPane().add(demo);
getContentPane().add("South", new DemoControls(demo));



}


public class Demo extends Canvas
{
public String direction = "image";
public Object scalingFactor = "100%";
public JScrollPane scrollPane;
public Demo() {

setBackground(Color.white);

}

public void drawDemo(int width, int height, Graphics g) {
Graphics2D g2 = (Graphics2D) g;

/* if (scalingFactor.equals("100%")) {
scale = 1;
} else if (scalingFactor.equals("200%")) {
scale = 2;
} else if (scalingFactor.equals("300%")) {
scale = 3;
}*/
if (direction.equals("image")) {
temp = Rotation(clouds[number], 0);
newAngle = 0;

} else if (direction.equals(">>>")) {
temp = Rotation(clouds[number], newAngle - 90);

newAngle = newAngle - 90;
flag = 0;
} else if (direction.equals("<<<")) {

temp = Rotation(clouds[number], newAngle + 90);

newAngle = newAngle + 90;
flag = 1;
} /*else if (direction.equals("zoom")) {

if(scalingFactor.equals("50%")){
temp = zoomOut(Rotation(clouds[number], newAngle),2);}
else
temp=zoomIn(Rotation(clouds[number], newAngle), scale);
//spd = new ScrollPaneDemo(temp);
}*/
else if(direction.equals("zoomIn")){
zoomState=zoomState+1;
temp=resizeImage(zoomState);
}
else if(direction.equals("zoomOut")){
zoomState=zoomState-1;
temp=resizeImage(zoomState);
}
else if (direction.equals("back")){
number=0;
temp=Rotation(clouds[number], 0);
}
else if (direction.equals("next")){

number=1;
temp = Rotation(clouds[number], 0);
}

// temp = Rotation(clouds[number], 0);
// newAngle = 0;
// int frame_width = 360;
// int frame_height = 360;
//
// /* PrintTestApp f = new PrintTestApp();
// f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//
// f.init (temp);
// f.setSize (frame_width, frame_height);
// f.setVisible (true);*/
// PrintingApplet pa=new PrintingApplet();
// } // main
//
//


return;
}

public BufferedImage resizeImage(int zoomState){
BufferedImage img=null;
if(zoomState<0){
img=zoomOut(Rotation(clouds[number], newAngle),(-1*zoomState)+1);

}
else if(zoomState>0){
img=zoomIn(Rotation(clouds[number], newAngle),zoomState+1);
}
else if(zoomState==0){
img=zoomIn(Rotation(clouds[number], newAngle),1);
}

switch(zoomState){
case 3:img=zoomIn(Rotation(clouds[number], newAngle),4);System.out.println("3");break;
case 2:img=zoomIn(Rotation(clouds[number], newAngle),3);System.out.println("2");break;
case 1:img=zoomIn(Rotation(clouds[number], newAngle),2);System.out.println("1");break;
case 0:img=zoomIn(Rotation(clouds[number], newAngle),1);System.out.println("0");break;
case -1:img=zoomOut(Rotation(clouds[number], newAngle),2);System.out.println("-1");break;
case -2:img=zoomOut(Rotation(clouds[number], newAngle),3);System.out.println("-2");break;
case -3:img=zoomOut(Rotation(clouds[number], newAngle),4);System.out.println("-3");break;
}
return img;
}


// Function For ZoomIn
public BufferedImage zoomIn(BufferedImage bi, int scale) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
int screenWidth=0 ;
int screenHeight=0;
for (int i=0; i<gs.length; i++) {
DisplayMode dm = gs[i].getDisplayMode();
screenWidth = dm.getWidth();
screenHeight = dm.getHeight();
}

int width = scale * bi.getWidth();
int height = scale * bi.getHeight();
BufferedImage biScale = new BufferedImage(width, height,
bi.getType());
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++)
biScale.setRGB(i, j, bi.getRGB(i / scale, j / scale));
if((screenWidth<width)||(screenHeight<height)){
System.out.println("Screen out Of Bound");

}
return biScale;
}

// Function For ZoomOut
public BufferedImage zoomOut(BufferedImage bi,int d) {
int width = (bi.getWidth() / d);
int height = (bi.getHeight() / d);
BufferedImage biScale = new BufferedImage(width, height,bi.getType());
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++)
biScale.setRGB(i, j, bi.getRGB(i*d, j * d));
return biScale;
}

// Method For Rotating Image
public <Picture> BufferedImage Rotation(BufferedImage pic, double angl) {
Picture pic1 = (Picture) pic;
int width = pic.getWidth();
int height = pic.getHeight();
double angle = Math.toRadians(angl);
double sin = Math.sin(angle);
double cos = Math.cos(angle);
double x0 = 0.5 * (width - 1); // point to rotate about
double y0 = 0.5 * (height - 1); // center of image
BufferedImage pic2 = new BufferedImage(width, height, 8);
// rotation
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
double a = x - x0;
double b = y - y0;
int xx = (int) (+a * cos - b * sin + x0);
int yy = (int) (+a * sin + b * cos + y0);

// plot pixel (x, y) the same color as (xx, yy) if it's in
// bounds
if (xx >= 0 && xx < width && yy >= 0 && yy < height) {
pic2.setRGB(x, y, pic.getRGB(xx, yy));
// pic2.set(x, y, pic1.get(xx, yy));
}
}
}
return pic2;
}

//For Printing


public void paint(Graphics g) {

{Graphics2D g2 = (Graphics2D) g;
Dimension d = getSize();
g.clearRect(0, 0, d.width, d.height);
drawDemo(d.width, d.height, g);
g2.drawImage(temp, 100, 20, this);}



}

}

static class DemoControls extends JPanel implements ActionListener,Printable
{
Demo demo;
JToolBar toolbar;
protected JTextArea textArea;

public DemoControls(Demo demo) {
ImageIcon theIcon = new ImageIcon("D:\\Pooja\\printer.png");
ImageIcon clockwise = new ImageIcon("D:\\Pooja\\arrow3.png");
ImageIcon anticlockwise = new ImageIcon("D:\\Pooja\\arrowanti.png");
this.demo = demo;
demo.setBackground(Color.white);
add(toolbar = new JToolBar(), BorderLayout.PAGE_START);
toolbar.setFloatable(true);
addTool("image", "true Image",theIcon, true);
addTool("<<<", "anticlockwise",anticlockwise, false);
addTool(">>>", "clockwise",clockwise, false);
//addTool("zoom", "zoom", false);
//addTool("zoomIn","zoomIn",false);
//addTool("zoomOut","zoomOut",false);
addTool("back","last image",theIcon,false);
addTool("next","next image",theIcon,false);
addTool("print","Printing",theIcon,false);


/*add(combo = new JComboBox());
combo.addItem("50%");
combo.addItem("100%");
combo.addItem("200%");
combo.addItem("300%");
combo.addItemListener(this);*/
}

public void addTool(String str, String tooltip,ImageIcon curr, boolean state) {
JButton b = (JButton) toolbar.add(new JButton(str));
b.setBackground(state ? Color.pink : Color.lightGray);
b.setIcon(curr);
b.setToolTipText(tooltip);
b.setSelected(state);
b.setOpaque(false);
b.addActionListener(this);
}

/*public void itemStateChanged(ItemEvent event) {
if (event.getSource() == combo
&& event.getStateChange() == ItemEvent.SELECTED) {
System.out.println("Change:" + combo.getSelectedItem());
//System.out.println("Inside Item Listener");
demo.scalingFactor = combo.getSelectedItem();
}
}*/

public void actionPerformed(ActionEvent e) {

for (int i = 0; i < toolbar.getComponentCount(); i++) {
//System.out.println("Inside Action Listener");
JButton b = (JButton) toolbar.getComponentAtIndex(i);
b.setBackground(Color.lightGray);

}
JButton b = (JButton) e.getSource();
b.setBackground(Color.GRAY);
demo.direction = b.getText();
if(b.getText().equalsIgnoreCase("print")){
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(this);
System.out.println("prrint dialog"+printJob.printDialog());
if (printJob.printDialog())
{
try
{
printJob.print();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}

}
demo.repaint();
}
public int print(Graphics g, PageFormat pf, int pi) throws PrinterException {
if (pi >= 1) {
return Printable.NO_SUCH_PAGE;
}

g.translate(100, 100);
Font f = new Font("Monospaced",Font.PLAIN,12);
g.setFont (f);
paint (g);
return Printable.PAGE_EXISTS;
}
}

public static void main(String[] args) {
First demo = new First();
demo.init();
JFrame f = new JFrame("ToolBar");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

f.pack();
f.setSize(new Dimension(200, 200));
f.show();



}


}



reply
reply
This thread has been viewed 3651 times.
Similar Threads
Display Image in an Applet
Print Preview instead of print page
displaying java.awt.Image in a panel
I18N DIALOG PRINT
window print problem
More...

All times above are in ranch (not your local) time.
The current ranch time is
Jun 27, 2025 05:35:40.