File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes AWT Applet to Application conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "AWT Applet to Application conversion" Watch "AWT Applet to Application conversion" New topic
Author

AWT Applet to Application conversion

Bo Riddly
Greenhorn

Joined: Oct 17, 2001
Posts: 2
I'm currently doin a project which involves the movement of icons around a map and the ability to zoom in on them. This is currently done on an applet which is converted to a application/ frame. My problem is that I wish to have menubars in the frame without placing them in the main function, so that when i use the menubars this will make the icons appear and dissappear on the map.
Thanks in advance, Please give an idiot proof respose if possible. Follows my code at present.
Bo
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.awt.image.*;
public class Zoom extends Applet implements Runnable {
Image theImage, map, luigi, blob;
double imageFactor; // scaling factor for image
int imageWidth, imageHeight; //scaled width and height so that image fits applet window
Thread repeatShow = null;
int x1, y1, rw, rh;// zooming rectangle
int delta = 10;// cursor area for dragging
int side = 0;
int moveDeltax , moveDeltay ;
boolean zoom = false;
int zoomWidth, zoomHeight, zoomX, zoomY;
double zoomFactor;
int newblobx=200; //starting coordinants Mario
int newbloby=200;
int newluigiy = 190; //Starting coordinants Luigi
int screenwidth = 240;
int screenheight = 320;
Image myOffScreenImage;
Graphics myOffScreenGraphics;

public void init() {


luigi = Toolkit.getDefaultToolkit().getImage ("j:\\luigi.gif");
blob = Toolkit.getDefaultToolkit().getImage ("j:\\mario.gif");
theImage = Toolkit.getDefaultToolkit().getImage ("j:\\dubmap.gif");
myOffScreenImage = createImage (getSize().width, getSize().height );
myOffScreenGraphics = myOffScreenImage.getGraphics();



setSize (240,310);
x1 = 30; rw = 60;
y1 = 30; rh = rw * getSize().height/getSize().width;
addMouseListener(new myMouseAdapter());
addMouseMotionListener(new myMouseMotionListener());
}

public void start() {
if (repeatShow == null){
repeatShow = new Thread(this);
repeatShow.start();
}
}
public void run() {
for (; {
repaint();
try {
repeatShow.sleep(300);
}
//Moves the co-ordinates of Mario and Luigi
newblobx = newblobx-2;
newbloby = newbloby-2;
newluigix = newluigix+1;
newluigiy = newluigiy-1;
}
catch( InterruptedException e) {}
}
}
public void stop() {
if (repeatShow != null) {
repeatShow.stop();
repeatShow = null;
}
}
class myMouseMotionListener extends MouseMotionAdapter {
public void mouseMoved(MouseEvent ev) {
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
int x = ev.getX();
int y = ev.getY();
// figure out where mouse is pointing
side = 0;
// outside zooming rectangle
if ( (x<x1-delta) | (x>x1+rw+delta) | (y<y1-delta) | (y>y1+rh +delta)) return;
// inside zooming rectangle
if ( (x>x1+delta) & (x<x1+rw-delta) & (y>y1+delta) & (y<y1+rh -delta)) side = 16;<br /> // 5 4 6
// +-----------+
// | |
// 1| 16 |2
// | |
// +-----------+
//98 10
if ( Math.abs(x-x1)<delta) {>
setCursor(new Cursor(Cursor.E_RESIZE_CURSOR));
side += 1;
}
if ( Math.abs(x-x1-rw)<delta ) {>
setCursor(new Cursor(Cursor.E_RESIZE_CURSOR));
side += 2;
}
if ( Math.abs(y-y1)<delta) {>
setCursor(new Cursor(Cursor.N_RESIZE_CURSOR));
side += 4;
}
if ( Math.abs(y-y1-rh)<delta) {>
setCursor(new Cursor(Cursor.S_RESIZE_CURSOR));
side += 8;
}
if (side == 5)
setCursor(new Cursor(Cursor.NW_RESIZE_CURSOR));
if (side == 6)
setCursor(new Cursor(Cursor.NE_RESIZE_CURSOR));
if (side == 9)
setCursor(new Cursor(Cursor.SW_RESIZE_CURSOR));
if (side == 10)
setCursor(new Cursor(Cursor.SE_RESIZE_CURSOR));
if (side == 16)
setCursor(new Cursor(Cursor.MOVE_CURSOR));
}

public void mouseDragged(MouseEvent ev) {
int x = ev.getX();
int y = ev.getY();
if (side==0) return;
// scale width or height with respect to changed side
if ( (side == 1) | | (side==5) | | (side==9) ) {
int dd = x-x1;
x1 = x;
rw = rw - dd;
rh = rw * getSize().height/getSize().width;
}
if ( (side == 2) | | (side==6) | | (side==10) ) {
rw = x - x1 ;
rh = rw * getSize().height/getSize().width;
}
if ( (side == 4) | | (side==5) | | (side==6) ) {
rh = rh - y + y1;
rw = rh * getSize().width/getSize().height;
y1 = y;
}
if ( (side == 8) | | (side==9) | | (side==10) ) {
rh = y - y1;
rw = rh * getSize().width/getSize().height;
}
if ( side==16) {
x1 = x - moveDeltax;
y1 = y - moveDeltay;

}
}
}
class myMouseAdapter extends MouseAdapter {
public void mousePressed(MouseEvent ev) {
moveDeltax = ev.getX() - x1;
moveDeltay = ev.getY() - y1;
}

public void mouseClicked(MouseEvent ev) {
if (zoom == true) {
zoom = false;
return ;
}
zoom = true;
zoomFactor = ( (rw/imageFactor) > (rh/imageFactor) ? (rw/imageFactor) : (rh/imageFactor));
zoomX = (int)(x1 / imageFactor);
zoomWidth = zoomX + (int)(rw / imageFactor);
zoomY = (int)(y1 / imageFactor);
zoomHeight = zoomY + (int)(rh / imageFactor);

}
}
Image offScreenImage = null;
Graphics offScreenGraphics = null;
//Eliminate flickering
/*
public void update(Graphics g) {
paint(g);
}
*/
boolean imageLoaded=false;
public void paint(Graphics g) {
if (offScreenImage == null){
offScreenImage = createImage(getSize().width, getSize().height);
offScreenGraphics = offScreenImage.getGraphics();
}
//draw the image offscreen
// display full image until it is fully loaded
if (!imageLoaded) {
imageLoaded=offScreenGraphics.drawImage(theImage, 0, 0, this);
}
// it is loaded now so calculate factors
if (imageLoaded) {
double imageFactorWidth = (double)getSize().width / (double)theImage.getWidth(null);
double imageFactorHeight = (double)getSize().height / (double)theImage.getHeight(null);
imageFactor = (imageFactorWidth<imageFactorHeight ? imageFactorWidth : imageFactorHeight);>
imageWidth = (int)(theImage.getWidth(null)*imageFactor);
imageHeight = (int)(theImage.getHeight(null)*imageFactor);


}
if (!zoom) {
offScreenGraphics.setColor(Color.lightGray);
offScreenGraphics.fillRect(0, 0, getSize().width, getSize().height);
offScreenGraphics.drawImage(theImage, 0, 0, imageWidth, imageHeight, this);
offScreenGraphics.setColor(Color.red);
offScreenGraphics.drawRect(x1, y1, rw, rh);
//Draws the Images double buffered on to map
offScreenGraphics.drawImage(blob, newblobx, newbloby, this);
offScreenGraphics.drawImage(luigi, newluigix, newluigiy, this);
//g.drawImage(blob,newblobx,newbloby,this);
//g.drawImage(luigi, newluigix, newluigiy, this);
}
else {
offScreenGraphics.drawImage(theImage,
0, 0, getSize().width, getSize().height,
zoomX, zoomY, zoomWidth, zoomHeight,this);
if (newblobx <= x1+rw && newbloby <= y1+rh)
// { offScreenGraphics.drawImage(blob, newblobx, newbloby, this);
{ offScreenGraphics.drawImage(blob, ((newblobx-x1)*screenwidth/rw), ((newbloby-y1)*screenheight/rh), this);
}
if (newluigix <= x1+rw && newluigiy <= y1+rh)
// { offScreenGraphics.drawImage(blob, newblobx, newbloby, this);
{ offScreenGraphics.drawImage(luigi, ((newluigix-x1)*240/rw), ((newluigiy-y1)*320/rh), this);
//offScreenGraphics.drawImage(blob,
//newblobx, newbloby, getSize().width, getSize().height,
//zoomX, zoomY, zoomWidth, zoomHeight,this);
}
}
//////////////////////////////////////////////////
/////////////////////////////////////////////////
////////////////////////////////////////////////
//offScreenGraphics.drawImage(blob, newblobx, newbloby, getSize().width,
// getSize().height, zoomX, zoomY, zoomWidth,
// zoomHeight, this);
//g.drawImage(blob,newblobx,newbloby,this);
//g.drawImage(luigi, newluigix, newluigiy, this);
//show it
g.drawImage(offScreenImage,0, 0, this);
}
public void update(Graphics g) {
paint(g);
paint (myOffScreenGraphics);
g.drawImage(myOffScreenImage, 0, 0, this);
}
static boolean inApplet =true;
public static void main(String args[]){
try
{
Thread.sleep(10000);
}
catch (InterruptedException ie)
{}
/*set a boolean flag to show if you are in an applet or not */
inApplet=false;
/*Create a Frame to place our application in. */
/*You can change the string value to show your desired label*/
/*for the frame */
Frame myFrame = new Frame ("Zoom");
MenuBar mb = new MenuBar();
Menu Contacts = new Menu ("Contacts");
Contacts.add (new MenuItem ("Mario"));
Contacts.addActionListener(this);
Contacts.add (new MenuItem ("Luigi"));
mb.add (Contacts);


Zoom myApp = new Zoom(); /*Add the current application to the Frame */
myFrame.add (myApp);
myFrame.setMenuBar(mb);
/*Resize the Frame to the desired size, and make it visible */
myFrame.resize(100,130);
myFrame.show();
/*Run the methods the browser normally would */
myApp.init();
myApp.start();
}
}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: AWT Applet to Application conversion
 
Similar Threads
Problems convering Applets to Applications
displaying scroll messages at the bottom of page
How to code optimization..
Why does this still flicker?
help with oval