Results 1 to 15 of 15
Like Tree1Likes
  • 1 Post By Tolls

Thread: How to convert java standalone application into applet

  1. #1
    chirayu is offline Member
    Join Date
    Aug 2012
    Posts
    18
    Rep Power
    0

    Question How to convert java standalone application into applet

    Hi ,
    I am newbie to java. Created a simple application and i want to run it as an applet.
    Please help me out.
    Here is my code....

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.AffineTransform;

    import javax.swing.JComponent;
    import javax.swing.JFrame;

    public class AffineTransformTest {

    private static TransformingCanvas canvas;

    public static void main(String[] args) {
    JFrame frame = new JFrame();
    canvas = new TransformingCanvas();
    TranslateHandler translater = new TranslateHandler();
    canvas.addMouseListener(translater);
    canvas.addMouseMotionListener(translater);
    canvas.addMouseWheelListener(new ScaleHandler());
    frame.setLayout(new BorderLayout());
    frame.getContentPane().add(canvas, BorderLayout.CENTER);
    frame.setSize(500, 500);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_C LOSE);
    frame.setVisible(true);
    }

    private static class TransformingCanvas extends JComponent {
    private double translateX;
    private double translateY;
    private double scale;

    TransformingCanvas() {
    translateX = 0;
    translateY = 0;
    scale = 1;
    setOpaque(true);
    setDoubleBuffered(true);
    }

    @Override public void paint(Graphics g) {

    AffineTransform tx = new AffineTransform();
    tx.translate(translateX, translateY);
    tx.scale(scale, scale);
    Graphics2D ourGraphics = (Graphics2D) g;
    ourGraphics.setColor(Color.WHITE);
    ourGraphics.fillRect(0, 0, getWidth(), getHeight());
    ourGraphics.setTransform(tx);
    ourGraphics.setRenderingHint(RenderingHints.KEY_AN TIALIASING,
    RenderingHints.VALUE_ANTIALIAS_ON);
    ourGraphics.setRenderingHint(RenderingHints.KEY_TE XT_ANTIALIASING,
    RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    ourGraphics.setColor(Color.BLACK);
    ourGraphics.drawRect(50, 50, 50, 50);
    ourGraphics.fillOval(100, 100, 100, 100);
    ourGraphics.drawString("Test Affine Transform", 50, 30);
    // super.paint(g);
    }
    }

    private static class TranslateHandler implements MouseListener,
    MouseMotionListener {
    private int lastOffsetX;
    private int lastOffsetY;

    public void mousePressed(MouseEvent e) {
    // capture starting point
    lastOffsetX = e.getX();
    lastOffsetY = e.getY();
    }

    public void mouseDragged(MouseEvent e) {

    // new x and y are defined by current mouse location subtracted
    // by previously processed mouse location
    int newX = e.getX() - lastOffsetX;
    int newY = e.getY() - lastOffsetY;

    // increment last offset to last processed by drag event.
    lastOffsetX += newX;
    lastOffsetY += newY;

    // update the canvas locations
    canvas.translateX += newX;
    canvas.translateY += newY;

    // schedule a repaint.
    canvas.repaint();
    }

    public void mouseClicked(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    public void mouseMoved(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    }

    private static class ScaleHandler implements MouseWheelListener {
    public void mouseWheelMoved(MouseWheelEvent e) {
    if(e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {

    // make it a reasonable amount of zoom
    // .1 gives a nice slow transition
    canvas.scale += (.1 * e.getWheelRotation());
    // don't cross negative threshold.
    // also, setting scale to 0 has bad effects
    canvas.scale = Math.max(0.00001, canvas.scale);
    canvas.repaint();
    }
    }
    }

    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: How to convert java standalone application into applet

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: How to convert java standalone application into applet

    Quote Originally Posted by chirayu View Post
    I am newbie to java. Created a simple application
    years ago?
    https://forums.oracle.com/forums/thr...readID=1262753

    Don't try to take credit for someone else's code. you couldn't even apply the good advice further down in that thread.

    Or did you lift it from Experts-Exchange?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    chirayu is offline Member
    Join Date
    Aug 2012
    Posts
    18
    Rep Power
    0

    Default Re: How to convert java standalone application into applet

    Quote Originally Posted by DarrylBurke View Post
    years ago?
    https://forums.oracle.com/forums/thr...readID=1262753

    Don't try to take credit for someone else's code. you couldn't even apply the good advice further down in that thread.

    Or did you lift it from Experts-Exchange?

    db
    I think didnt use the correct words.......inspite of creating i would hace used i have piece of code and I am trying to learn to put that in applet...........Sorry , But I dont wanna steal someone's code..........and i am newbie just wanna help from java experts.

  5. #5
    chirayu is offline Member
    Join Date
    Aug 2012
    Posts
    18
    Rep Power
    0

    Default Re: How to convert java standalone application into applet

    db that guy in oracle forum took it from Java2D: Have Fun With Affine Transform :)

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: How to convert java standalone application into applet

    Learn Java: The Java™ Tutorials

    If you already know the basics, you'll want to focus on the Java 2D and Applet deployment sections, as well as the Swing trail.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    chirayu is offline Member
    Join Date
    Aug 2012
    Posts
    18
    Rep Power
    0

    Default Re: How to convert java standalone application into applet

    Basically I am C programmer ....due project requirement i need to do this patch asap......just send me the code to deploy that in applet....i will atleast learn by wat you did.....I know its iritating but thats Y i knocked your forum to be straight n true....

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: How to convert java standalone application into applet

    You have a project requirement to take code you found on the internet and turn it into an applet?
    Fubarable likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    chirayu is offline Member
    Join Date
    Aug 2012
    Posts
    18
    Rep Power
    0

    Default Re: How to convert java standalone application into applet

    No its just a prototype i took..........its just a sample..........I just want to know how can i deploy that in applets if it is standalone application......
    I have my own code which works like this :
    First it reads all parameters from HTML tags and stores in global variables. Then it sends query to CGI to read graph data. Then it converts data for plotting and draws graph. There is a option for user to select 1-24 hours. Based on the selection graph will be plotted by plotting only selected data. Every 30 sec it sends query to CGI for collecting data.

    I need to add zooming feature in it. As it is completely in awt....so just wanna know how can i deploy a code which uses frames and all swing components........Please if you guys can stop interrogating and can just do some real work.......

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: How to convert java standalone application into applet

    Please if you guys can stop interrogating and can just do some real work.......
    How can we help you learn how to write the java code you want to write?
    If you are looking for someone to write code for you, you've come to the wrong spot.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    chirayu is offline Member
    Join Date
    Aug 2012
    Posts
    18
    Rep Power
    0

    Default Re: How to convert java standalone application into applet

    Quote Originally Posted by Norm View Post
    How can we help you learn how to write the java code you want to write?
    If you are looking for someone to write code for you, you've come to the wrong spot.
    I am not asking to write code ..i m asking how to implement zooming with using awt classes and functions....i need the suggestion..........I have planed to use AffineTransform ...but it just zoom my image...i have graph...........i want that... when i zoom ...only the contents inside gets zoomed and accordingly x-y axis should get granualar.....Here i am bothered about granuality of x-y axis when zoom in happens so that graph ploted gives me more accurate values on x-y axis....

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: How to convert java standalone application into applet

    Can you post the code you are working with as a small program that compiles, executes and shows the problem?
    This part of your problem about drawing graphs and zooming should be posted in another part of the forum. This section is for applets.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    chirayu is offline Member
    Join Date
    Aug 2012
    Posts
    18
    Rep Power
    0

    Default Re: How to convert java standalone application into applet

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.awt.geom.AffineTransform;
    import java.util.*;
    import java.text.*;
    
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    
    public class AffineTransformTest {
    
    	private static TransformingCanvas canvas;
    	public static int SIZE_X			= 480;
    	public static int	SIZE_Y			= 250;
    	public static int	SIZE_Y1			= 240;
    	public static int xOffset = 50;
    	public	static int yOffset = 40;
    
    	public	static int Y1_MAX_VALUE = 100;
    
    	public static int Y2_MAX_VALUE = 3;
    
    	public	static int MAX_NUM_INTERVELL_PER_DAY = 96;
    	public static int 	duration=1;
    	public static int sizeY = SIZE_Y;
    
    
    	public static void main(String[] args) {
    		JFrame frame = new JFrame();
    		canvas = new TransformingCanvas();
    		TranslateHandler translater = new TranslateHandler();
    		canvas.addMouseListener(translater);
    		canvas.addMouseMotionListener(translater);
    		canvas.addMouseWheelListener(new ScaleHandler());
    		frame.setLayout(new BorderLayout());
    		frame.getContentPane().add(canvas, BorderLayout.CENTER);
    		frame.setSize(500, 500);
    		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    		frame.setVisible(true);
    	}
    
    	private static class TransformingCanvas extends JComponent {
    		private double translateX;
    		private double translateY;
    		private double scale;
    
    		TransformingCanvas() {
    			translateX = 0;
    			translateY = 0;
    			scale = 1;
    			setOpaque(true);
    			setDoubleBuffered(true);
    		}
    
    		@Override public void paint(Graphics g) {
    
    				Color 	plotColor1,plotColor2,plotColor3,plotColor4,plotColor5,plotColor6,plotColor7,plotColor8,plotColor9,plotColor10;
    
    			plotColor1 		= Color.orange;
    				plotColor2 		= Color.black;
    				plotColor3 		= Color.blue;
    				plotColor4 		= Color.magenta;
    				plotColor5 		= Color.cyan;
    				plotColor6 		= Color.pink;
    				plotColor7 		= Color.gray;
    				plotColor8		= Color.red;
    				plotColor9		= Color.green;
    	plotColor10		= Color.lightGray;
    
    			AffineTransform tx = new AffineTransform();
    			tx.translate(translateX, translateY);
    			tx.scale(scale, scale);
    			Graphics2D ourGraphics = (Graphics2D) g;
    			ourGraphics.setColor(Color.WHITE);
    			ourGraphics.fillRect(0, 0, getWidth(), getHeight());
    			ourGraphics.setTransform(tx);
    			ourGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    					RenderingHints.VALUE_ANTIALIAS_ON);
    			ourGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
    					RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    			ourGraphics.setColor(Color.BLACK);
    			ourGraphics.drawRect(50, 50, 50, 50);
    			ourGraphics.fillOval(100, 100, 100, 100);
    			ourGraphics.drawString("Test Affine Transform", 50, 30);
    
    			g.setColor(Color.black);
    				g.drawString("ES" , xOffset, 10);
    				g.drawString("SES", xOffset+30,10);
    				g.drawString("BBE", xOffset+60,10);
    				g.drawString("UAS",xOffset+90,10);
    				g.drawString("ESR", xOffset+120,10);
    				g.drawString("SESR", xOffset+160,10);
    				g.drawString("BBER" , xOffset+200,10);
    				g.drawString("SUSPECT",xOffset+240,10);
    				g.drawString("TIME-ELAPSED",xOffset+310,10);
    
    				g.setColor(plotColor1);
    				g.fillRect(xOffset+5, 15, 12, 8);
    
    				g.setColor(plotColor2);
    				g.fillRect(xOffset+35, 15, 12, 8);
    
    				g.setColor(plotColor3);
    				g.fillRect(xOffset+65, 15, 12, 8);
    
    				g.setColor(plotColor4);
    				g.fillRect(xOffset+95, 15, 12, 8);
    
    				g.setColor(plotColor5);
    				g.fillRect(xOffset+125, 15, 12, 8);
    
    				g.setColor(plotColor6);
    				g.fillRect(xOffset+170, 15, 12, 8);
    
    				g.setColor(plotColor7);
    				g.fillRect(xOffset+210, 15, 12, 8);
    
    				g.setColor(plotColor8);
    				g.fillRect(xOffset+260, 15, 12, 8);
    
    				g.setColor(plotColor9);
    				g.fillRect(xOffset+340, 15, 12, 8);
    				/*<Bugzilla><1903><07-Dec-2009><RR><END>*/
    	g.setColor(Color.black);
    
    			/*Dram Graph boarder */
    				g.drawRect(xOffset,yOffset,SIZE_X,sizeY);
    				g.drawRect(xOffset-1,yOffset-1,SIZE_X+2,sizeY+2);
    
    			   /*Plot X axis*/
    				int max_x_marks = 8;
    				int temp = 1,cnt_graph = 0;
    				int float_temp,float_temp2;
    
    				/*max 8 plots on x axis*/
    				for(int x=max_x_marks; x>0; x--)
    				{
    
    					float_temp = (int)((MAX_NUM_INTERVELL_PER_DAY*duration)/max_x_marks)*((max_x_marks+1)-x);
    					float_temp2 = SIZE_X-(60*cnt_graph);
    
    					g.drawString(String.valueOf(float_temp),(float_temp2-20) ,SIZE_Y+yOffset+35);
    					cnt_graph++;
    				}
    
    				/*Plot Y1 AXIS*/
    				temp = Y1_MAX_VALUE;
    				for(int x = 0; x <= SIZE_Y; x+= 25)
    				{
    					g.drawString(String.valueOf(temp), 25, x + yOffset+10);
    					temp -= (Y1_MAX_VALUE - 0)/10;
    				}
    				temp = 1000;
    				 /*Plot Y2 AXIS*/
    				int index_log = 1;
    				for(int x = 0; x <= SIZE_Y1; x+= 80)
    				{
    					if(x== 240)
    					index_log--;
    					if(temp>=1)
    					{
    						g.drawString(String.valueOf(temp), 550, x+yOffset+8-index_log);
    						g.drawLine(530,x+yOffset+5-index_log, 540, x+yOffset+5-index_log);
    					}
    					temp = temp/10;
    				}
    
    				Font thisFont = new Font("Times New Roman", Font.BOLD, 14);
    				g.setFont(thisFont);
    				g.drawString("Y2", 550, 160);
    	g.drawString("Y1",5, 160);
    			// super.paint(g);
    		}
    	}
    
    	private static class TranslateHandler implements MouseListener,
    			MouseMotionListener {
    		private int lastOffsetX;
    		private int lastOffsetY;
    
    		public void mousePressed(MouseEvent e) {
    			// capture starting point
    			lastOffsetX = e.getX();
    			lastOffsetY = e.getY();
    		}
    
    		public void mouseDragged(MouseEvent e) {
    
    		// new x and y are defined by current mouse location subtracted
    			// by previously processed mouse location
    			int newX = e.getX() - lastOffsetX;
    			int newY = e.getY() - lastOffsetY;
    
    			// increment last offset to last processed by drag event.
    			lastOffsetX += newX;
    			lastOffsetY += newY;
    
    			// update the canvas locations
    			canvas.translateX += newX;
    			canvas.translateY += newY;
    
    			// schedule a repaint.
    			canvas.repaint();
    		}
    
    		public void mouseClicked(MouseEvent e) {}
    		public void mouseEntered(MouseEvent e) {}
    		public void mouseExited(MouseEvent e) {}
    		public void mouseMoved(MouseEvent e) {}
    		public void mouseReleased(MouseEvent e) {}
    	}
    
    	private static class ScaleHandler implements MouseWheelListener {
    		public void mouseWheelMoved(MouseWheelEvent e) {
    			if(e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
    
    				// make it a reasonable amount of zoom
    				// .1 gives a nice slow transition
    				canvas.scale += (.1 * e.getWheelRotation());
    				// don't cross negative threshold.
    				// also, setting scale to 0 has bad effects
    				canvas.scale = Math.max(0.00001, canvas.scale);
    				canvas.repaint();
    			}
    		}
    	}
    
    }
    Last edited by Norm; 08-03-2012 at 05:41 PM. Reason: Fixed ending code tag

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: How to convert java standalone application into applet

    You should post this question on:
    Java 2D or
    AWT / Swing

    Not related to applets.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: How to convert java standalone application into applet

    Reposted in AWT/Swing: Zooming in graph using awt/swing

    db

    THREAD CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. How to develop a standalone application using java
    By santhoshkeleti in forum New To Java
    Replies: 2
    Last Post: 11-15-2011, 06:21 PM
  2. developing a standalone application using java
    By santhoshkeleti in forum New To Java
    Replies: 1
    Last Post: 11-15-2011, 06:18 PM
  3. Replies: 0
    Last Post: 02-03-2011, 04:36 AM
  4. java standalone to applet
    By Nikohw in forum Java Applets
    Replies: 1
    Last Post: 09-15-2009, 09:36 AM
  5. Replies: 4
    Last Post: 04-23-2009, 09:54 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •