This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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;
} } 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(); } }