Results 1 to 9 of 9
  1. #1
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    163
    Rep Power
    7

    Default Swing Application to Web Application

    Hello!

    I have been working on a game, using the Swing Application framework, from time to time for a couple of months now. However, I kind of recently realized that I wanted to implement my game in a "website", but apparently you can't implement runnable jars in a website.

    I have looked around for solutions for achieving this. Some sources say recoding the application into an applet is best, that would probably not work that well though since I'm not familiar with applets, however, I'm up for challenges. Some say I could use a tool called "Ajax" which translates the application somehow. What do you think is the best solution, whether it's any of those I proposed or any of your own?

    These are parts of my founding classes:
    Swing Application to Web Application-picture1.png

    Swing Application to Web Application-picture2.png

    Thanks in advance!
    Last edited by Zelaine; 09-24-2014 at 09:07 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Swing Application to Web Application

    Please post the text of the code, not images.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    163
    Rep Power
    7

    Default Re: Swing Application to Web Application

    As you wish, keep in mind though that I have removed a lot of variables and other functions that I deemed unnecessary. I just wanted you to get a quick overview of what I was talking about. For you to be able to run everything, I would have to include about 1500 lines of code.

    Java Code:
    package Modulus;
    
    import javax.swing.*;
    
    public class Game{
    	public static void main(String[] args){
    		JFrame window = new JFrame("Space Invaders");
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		window.setContentPane(new GamePanel());
    		window.setResizable(false);
    		
    		window.pack();
    		window.setVisible(true);
    		window.setLocationRelativeTo(null);
    	}
    }
    Java Code:
    package Modulus;
    
    import java.awt.*;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.awt.geom.AffineTransform;
    import java.awt.image.*;
    import java.io.IOException;
    import java.util.ArrayList;
    
    import javax.imageio.ImageIO;
    import javax.swing.JPanel;
    
    public class GamePanel extends JPanel implements Runnable, KeyListener, MouseListener, MouseMotionListener{
    	public static int WIDTH = 512;
    	public static int HEIGHT = 512;
    	
    	private Thread thread;
    	private boolean running;
    	private static boolean paused;
    
            ...
    	
    	private BufferedImage image;
    	private Graphics2D g;
    	
    	private int FPS = 60;
    
            ...
    	
    	public GamePanel(){
    		super();
    		setPreferredSize(new Dimension(WIDTH, HEIGHT));
    		setFocusable(true);
    		requestFocus();
    		
    		addKeyListener(this);
    		addMouseListener(this);
    		addMouseMotionListener(this);
    	}
    	
    	public void addNotify(){
    		super.addNotify();
    		if(thread == null){
    			thread = new Thread(this);
    			thread.start();
    		}
    	}
    	
    	public static boolean isPaused(){
    		return paused;
    	}
    	
    	public void run(){
    		running = true;
    		paused = false;
    		
    		image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    		g = (Graphics2D) image.getGraphics();
    		
    		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    		g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    
                    ...
    		
    		long startTime = System.nanoTime();
    		long frame = 1000 * 1000 * 1000 / FPS;
    		long elapsedTime = 0;
    		
    		while(running){
    			if(!paused)
    				update(elapsedTime);
    			render();
    			draw();
    			
    			elapsedTime = System.nanoTime() - startTime;
    			if(elapsedTime < frame){
    				int sleep = (int)((frame - elapsedTime) / (1000 * 1000));
    				try{
    					Thread.sleep(sleep);
    				}catch(InterruptedException e){
    					e.printStackTrace();
    				}
    			}
    			
    			startTime += elapsedTime;
    		}
    	}

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Swing Application to Web Application

    Applets have a security problem. A knowledgeable user can make changes on his system to allow them to run.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    163
    Rep Power
    7

    Default Re: Swing Application to Web Application

    So it's the best solution if one knows what to do?

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Swing Application to Web Application

    Whenever you need to implement listeners with more than one method I would recommend,
    that you use inner or static nested classes each extending the appropriate adapter class. That
    way you only need to include the methods you are implementing. Check out the KeyAdapter and
    MouseAdapter classes in the JDK. The type of class is determined by whether the class needs to
    access instance fields of the enclosing class. You can read about them at
    Nested Classes (The Java Tutorials > Learning the Java Language > Classes and Objects)
    and How to Write a Mouse Listener (The Java Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners).

    Regards,
    Jim
    Last edited by jim829; 09-24-2014 at 10:29 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Swing Application to Web Application

    Rewrite it in html and javascript
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    163
    Rep Power
    7

    Default Re: Swing Application to Web Application

    The thing is, I can't do that, because I have already come very far in the process. The code I posted above is far from everything, the GamePanel class is about 900 lines in its entirety, and I have about ten other classes. So that's why I'm asking for the best alternative option to rewriting it in HTML and JavaScript.

    And besides, it doesn't really bother me right now if applets are not the securest option, as long as it works, because that's just what I want to do now, get it to work.
    Last edited by Zelaine; 09-24-2014 at 10:47 PM.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Swing Application to Web Application

    Ok, try converting to an applet. One way is to change the GUI to go into a JPanel that can be added to either a JFrame for a desktop program or to the JApplet for the applet version.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Swing Application
    By Zelaine in forum New To Java
    Replies: 2
    Last Post: 09-13-2014, 07:35 PM
  2. Execution of Swing application on another pc
    By ksaketh2 in forum Advanced Java
    Replies: 5
    Last Post: 04-09-2014, 02:27 PM
  3. Swing application getting odd errors
    By Zeezj in forum New To Java
    Replies: 4
    Last Post: 09-14-2011, 07:14 PM
  4. Better Swing Application Framework
    By etf in forum Java Software
    Replies: 1
    Last Post: 09-07-2009, 02:25 PM
  5. designing application in swing
    By programmer_007 in forum AWT / Swing
    Replies: 12
    Last Post: 09-03-2009, 07:50 PM

Posting Permissions

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