Google Answers Logo
View Question
 
Q: Zooming an image in Java ($25.00) ( No Answer,   0 Comments )
Question  
Subject: Zooming an image in Java ($25.00)
Category: Computers > Programming
Asked by: vkcalif-ga
List Price: $25.00
Posted: 30 Jan 2004 12:07 PST
Expires: 01 Feb 2004 19:09 PST
Question ID: 301871
Hi!
	I was looking for some help for my application here.  This
application has two panels: the one on the left side, the smaller one,
is the PDA panel (drawingPanelPDA) while the one on the right side,
the larger one, is the computer screen panel (cPanel).  cPanel has
another panel (drawingPanelComputer) which initially is the same size
as the drawingPanelPDA.  The goal of this application is to make sure
whatever is drawn on the drawingPanelPDA is also reflected on
drawingPanelComputer.  In addition to the above, drawingPanelPDA has 6
buttons on a toolbar: Left, Right, Up, Down, ZoomIn (++) and ZoomOut
(--).  When the user hits any of these buttons the
drawingPanelComputer and the drawing on it should perform the
respective action.  Say, for instance, when the left button is
pressed, the drawingPanelComputer and the drawing on it should move
left and so on.  Similarly with zoomIn and zoomOut.  Say, for
instance, when zoomIn is pressed, the drawingPanelComputer and the
drawing on it should increase.

           Right now I am able to move the drawingPanelComputer and
its drawing left, right, up and down.  However, zoomIn and zoomOut is
where I am having difficulties.  At this point, zoomIn increases only
the drawingPanelComputer, but not the drawing while zoomOut decreases
only the drawingPanelComputer, but not the drawing.  Also I am to use
?Bilinear interpolation? to zoomIn and zoomOut.

The final goal is to zoom in and zoom out the drawingPanelComputer
with the drawing using Bilinear interpolation (pixel manipulation
algorithm).  Now in order to manipulate pixels we need a BufferedImage
instance and then perform the interpolation. So this is a two-step
process:

1. Obtain the BufferedImage
2. Perform bilinear interpolation.

The main issue is that in the first step itself --- from whatever the
code is there right now it is not sure whether the BufferedImage in
the drawingPanel class is being created correctly or not within the
paintComponent() method. We need the BufferedImage object so that we
can obtain its pixel data (Raster) and manipulate the pixel data
(getRGB() and setRGB() methods for BufferedImage let you do this).

So my stress would be to solve the first step mentioned above. The 2nd
step interpolation) I almost know how to do (given the first step ie.
given that the  BufferedImage is created correctly and has all the
drawing data -- which is what I wish to be solved).

I am attaching my entire code alongwith.  Also I am using JDK 1.4.

Thanks

File: InternalFrameTest.java

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.beans.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.*;
import java.awt.event.*;

import java.awt.geom.*;
import java.awt.image.*;

public class InternalFrameTest {

    public static void main(String[] args) {
        MainFrame frame = new MainFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.show();
    } //end main()
} // end class InternalFrameTest

class MainFrame extends JFrame {

    protected int mainFrameWidth, mainFrameHeight, pFrameWidth, pFrameHeight,
        lastX, lastY,
        cFrameWidth, cFrameHeight, cPanelWidth, cPanelHeight,
        drawingPanelComputerWidth,
        drawingPanelComputerHeight, pPanelHeight, pPanelWidth,
        controlToolBarWidth, controlToolBarHeight,
        drawingPanelPDAWidth, drawingPanelPDAHeight;
    protected JInternalFrame pFrame, cFrame;
    protected JPanel cPanel, pPanel;
    protected JToolBar controlToolBar;
    protected JButton left, right, up, down, zoomIn, zoomOut;
    protected drawingPanel drawingPanelPDA, drawingPanelComputer;
    protected ArrayList pointArrayList = new ArrayList();

    protected boolean zoomInPressed = false;
    protected boolean zoomOutPressed = false;

    public MainFrame() {
		// get screen dimensions
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        mainFrameHeight = screenSize.height;
        mainFrameWidth = screenSize.width;

		// Center frame in screen
        setSize(mainFrameWidth, mainFrameHeight);
        setLocation(0, 0);

		//set title
        setTitle("Simulation of PDA screen output to computer screen");

        JDesktopPane desktop = new JDesktopPane();
        desktop.setOpaque(true);
        getContentPane().add(desktop, BorderLayout.CENTER);

        System.out.println("mainFrameWidth = " + mainFrameWidth);
        System.out.println("mainFrameHeight = " + mainFrameHeight);

		//pda frame
        pFrame = new JInternalFrame("PDA Screen", false, false, false, false);
        desktop.add(pFrame, new Integer(1));

        pFrameWidth = (int) (mainFrameWidth / 4.75);
        pFrameHeight = (mainFrameHeight / 2);

        pFrame.reshape(this.getX(), this.getY(), pFrameWidth, pFrameHeight);
        pFrame.setVisible(true);
        try {
            pFrame.setSelected(true);
        } catch (PropertyVetoException e) {
            System.out.println("Error while setting pdaFrame to visisble");
        }
        //System.out.println("pFrameWidth is " + pFrame.getWidth());
        //System.out.println("pFrameHeight is " + pFrame.getHeight());
        pFrame.show();

		//toolbar
        controlToolBar = new JToolBar("myToolBar", SwingConstants.HORIZONTAL);

		//boolean isZoomButton =
        left = makeButton("Left", "Move to Left", -5, 0, false);
        right = makeButton("Right", "Move to Right", 5, 0, false);
        up = makeButton("Up", "Move Up", 0, -5, false);
        down = makeButton("Down", "Move Down", 0, 5, false);
        zoomIn = makeButton("++", "Zoom In", 10, 10, true);
        zoomOut = makeButton("--", "Zoom Out", -10, -10, true);

        controlToolBar.add(left);
        controlToolBar.add(right);
        controlToolBar.add(up);
        controlToolBar.add(down);
        controlToolBar.add(zoomIn);
        controlToolBar.add(zoomOut);

        controlToolBar.setVisible(true);
        controlToolBar.setBackground(Color.white);

		//class PositionRecorder
        class PositionRecorder extends MouseAdapter {
            public void mousePressed(MouseEvent event) {
                record(event.getX(), event.getY());
            }
        }

		//class LineDrawer
        class LineDrawer extends MouseMotionAdapter {

            public void mouseDragged(MouseEvent event) {
                int x = event.getX();
                int y = event.getY();
                Point pt = new Point(x, y);
                pointArrayList.add(pt);

                if (x >= drawingPanelPDA.getX() &&
                    x <= drawingPanelPDA.getWidth() &&
                    y >= drawingPanelPDA.getY() &&
                    y <= drawingPanelPDA.getHeight()) {
                    drawingPanelPDA.getGraphics().drawLine(lastX, lastY, x, y);
                    drawingPanelComputer.getGraphics().drawLine(lastX, lastY, x,
                                                                y);
                }
                record(x, y);
            }
        }

		//pPanel

        pPanel = new JPanel(null);
        pFrame.getContentPane().add(pPanel);
        pPanelWidth = pFrameWidth;
        pPanelHeight = pFrameHeight;
        pPanel.setBounds(pFrame.getX(), pFrame.getY(), pPanelWidth,
                         pPanelHeight);
        pPanel.setVisible(true);
        pPanel.setBackground(Color.orange);
        pPanel.setLocation(0, 0);
        pPanel.add(controlToolBar);
        //System.out.println("pPanelHeight is " + pPanel.getHeight());
        //System.out.println("pPanelWidth is " + pPanel.getWidth());

        controlToolBarWidth = pPanelWidth;
        controlToolBarHeight = (pPanelHeight / 9);
        controlToolBar.setBounds(pPanel.getX(), pPanel.getY(),
                                 controlToolBarWidth, controlToolBarHeight);
        controlToolBar.setLocation(0, 0);

		//drawingPanelPDA
        drawingPanelPDA = new drawingPanel();

        drawingPanelPDAWidth = pFrameWidth;
        drawingPanelPDAHeight = (pPanel.getHeight() - controlToolBarHeight);

        drawingPanelPDA.setBounds(pPanel.getX(),
                                  (pPanel.getY() + controlToolBarHeight + 1),
                                  drawingPanelPDAWidth, drawingPanelPDAHeight);
        drawingPanelPDA.setVisible(true);

        pPanel.add(drawingPanelPDA);
        pFrame.pack();
        lastX = drawingPanelPDA.getX();
        lastY = drawingPanelPDA.getY();

        /*System.out.println("pFrameWidth is " + pFrame.getWidth());
        System.out.println("pFrameHeight is " + pFrame.getHeight());
        System.out.println("pPanelHeight is " + pPanel.getHeight());
        System.out.println("pPanelWidth is " + pPanel.getWidth());
        */

		//computer frame

        cFrame = new JInternalFrame("Computer Screen", false, false, false, false);
        desktop.add(cFrame, new Integer(2));

        cFrameWidth = (mainFrameWidth - (pFrameWidth + 10));
        cFrameHeight = (mainFrameHeight - (mainFrameHeight / 12));

        cFrame.reshape( (this.getX() + pFrameWidth + 10), this.getY(),
                       cFrameWidth, cFrameHeight);

        cFrame.setVisible(true);
        try {
            cFrame.setSelected(true);
        } catch (PropertyVetoException f) {
            System.out.println("Error while setting computerFrame to visisble");
        }
        cFrame.getContentPane().setLayout(null);
        cFrame.show();

		//cPanel
        cPanel = new JPanel(null);
        cFrame.getContentPane().add(cPanel);
        cPanelWidth = cFrameWidth;
        cPanelHeight = cFrameHeight;
        cPanel.setBounds(cFrame.getX(), cFrame.getY(), cPanelWidth,
                         cPanelHeight);
        cPanel.setVisible(true);
        cPanel.setBackground(Color.orange);
        cPanel.setLocation(0, 0);

		//drawingPanelComputer
        drawingPanelComputer = new drawingPanel();
        drawingPanelComputerWidth = drawingPanelPDAWidth;
        drawingPanelComputerHeight = drawingPanelPDAHeight;
        drawingPanelComputer.setBounds(0, 0, drawingPanelComputerWidth,
                                       drawingPanelComputerHeight);
        cPanel.add(drawingPanelComputer);
        drawingPanelComputer.setLocation(0, 0);

		//addding MouseListeners
        drawingPanelPDA.addMouseListener(new PositionRecorder());
        drawingPanelPDA.addMouseMotionListener(new LineDrawer());

    } // end constructor MainFrame

    JButton makeButton(String text, String toolTip, int hor, int ver,
boolean isZoomButton) {
        JButton button = new JButton(text);

        final int hori, verti;
        hori = hor;
        verti = ver;
        button.setToolTipText(toolTip);
        button.setBorder(BorderFactory.createEmptyBorder());

        if (!isZoomButton) {
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent event) {
                    Point p = new Point(0, 0);
                    p.setLocation( (drawingPanelComputer.getX() + hori),
                                  (drawingPanelComputer.getY() + verti));
                    drawingPanelComputer.setLocation(p);
                    drawingPanelComputer.repaint();
                }
            });
        } else {
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent event) {
                    if ( (event.getActionCommand()).equals("++")) {
                        zoomInPressed = true;
                    } else if ( (event.getActionCommand()).equals("--")) {
                        zoomOutPressed = true;
                    }

                    drawingPanelComputerWidth =
drawingPanelComputerWidth + (int) ( ( (float) hori / 100) *
drawingPanelComputerWidth);
                    drawingPanelComputerHeight =
drawingPanelComputerHeight + (int) ( ( (float) verti / 100) *
drawingPanelComputerHeight);
                    drawingPanelComputer.setBounds(0, 0, drawingPanelComputerWidth,
                                                   drawingPanelComputerHeight);
                    drawingPanelComputer.setLocation(0, 0);
                    drawingPanelComputer.repaint();
                }
            });
        }
        return button;
    } // end makeButton()

    void record(int x, int y) {
        lastX = x;
        lastY = y;
    }

    public class drawingPanel extends JPanel {

        private Image I = null;
        private BufferedImage BI = null;

        public drawingPanel() {
            super();
            setBackground(Color.white);
            setDoubleBuffered(true);
            setOpaque(true);
        } // end constructor drawingPanel

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

			//g2d = (Graphics2D)this.getGraphics();
			//Graphics2D g2d = (Graphics2D)g;

            for (int i = 0; i < (pointArrayList.size() - 1); i++) {
				//DO NOT USE drawingPanelComputer.getGraphics()
                g.drawLine( (int) ( (Point) pointArrayList.get(i)).getX(),
                           (int) ( (Point) pointArrayList.get(i)).getY(),
                           (int) ( (Point) pointArrayList.get(i + 1)).getX(),
                           (int) ( (Point) pointArrayList.get(i + 1)).getY()
                           );
            } // end for

            I = this.createImage(getSize().width, getSize().height);

            BI = this.toBufferedImage(I);
            Graphics2D g2d = BI.createGraphics();

            if (zoomInPressed) {

                System.out.println("Zoom In Pressed");
                zoomInPressed = false;
            } else if (zoomOutPressed) {
                System.out.println("Zoom Out Pressed");
                zoomOutPressed = false;
            } else {
                g2d.drawImage(BI, 0, 0, null);
            }

        } // end paintComponent()

		// This method returns a buffered image with the contents of an image
        public BufferedImage toBufferedImage(Image image) {
            if (image instanceof BufferedImage) {
                return (BufferedImage) image;
            }

			// Determine if the image has transparent pixels;
            boolean hasAlpha = false;

			// Create a buffered image with a format that's compatible with the screen
            BufferedImage bimage = null;
            GraphicsEnvironment ge = GraphicsEnvironment.
                getLocalGraphicsEnvironment();
            try {
				// Determine the type of transparency of the new buffered image
                int transparency = Transparency.OPAQUE;
                if (hasAlpha) {
                    transparency = Transparency.BITMASK;
                }

		        //Create the buffered image
                GraphicsDevice gs = ge.getDefaultScreenDevice();
                GraphicsConfiguration gc = gs.getDefaultConfiguration();
                bimage = gc.createCompatibleImage(
                    image.getWidth(null), image.getHeight(null), transparency);
            } catch (HeadlessException e) {
				// The system does not have a screen
            }

            if (bimage == null) {
				// Create a buffered image using the default color model
                int type = BufferedImage.TYPE_INT_RGB;
                if (hasAlpha) {
                    type = BufferedImage.TYPE_INT_ARGB;
                }
                bimage = new BufferedImage(image.getWidth(null),
image.getHeight(null), type);
            }

			// Copy image to buffered image
            Graphics2D g = bimage.createGraphics();

			// Paint the image onto the buffered image
            g.drawImage(image, null, null);
            g.dispose();
            return bimage;
        }

    } // end class drawingPanel

} // end class MainFrame
Answer  
There is no answer at this time.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy