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

Thread: Help converting a program into an applet

  1. #1
    Unlimited is offline Member
    Join Date
    Sep 2014
    Posts
    6
    Rep Power
    0

    Default Help converting a program into an applet

    Hi everyone, I'm new here and I'm in great need of help. I've been searching for years how to make an applet out of this program but can't seem to do it. Here it is, and sorry if it's a bit long. It's been derived from a game that i've stripped down to some of its core needs for the sake of simplification.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.JLabel;
    import javax.swing.JFrame;
    import java.io.*;
    import java.util.Scanner;
    
    class Player
    {
    	  double LifePoints = 30000;	 
          int LevelLocation = 10;
          int LeftRightLocation;
          int UpDownLocation;
    }
    
    public class Joueur extends JFrame
    {
    	public static int NewGame = 0;
    	private KeyboardPanel keyboardPanel = new KeyboardPanel();
    	private ImageIcon GameIcon = new ImageIcon("d:/Pics/01 9.gif");
    
    	public Joueur()
    	{
    		setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20));
    		add(keyboardPanel);
    
    	    keyboardPanel.setFocusable(true);
    
           add(new JLabel(GameIcon));
           repaint();
    	}
    
        public static void main(String[] args)
        {
        	Player Player1 = new Player(); 	  	
        	Player1.LeftRightLocation = 37;
        	Player1.UpDownLocation = 28;
        
           	int Gamerun = 0;
        	Scanner in = new Scanner(System.in);
    
        	int levelarea = 0;
        	int i, j, a, b;
        	char[][] levelx;
        	char[][] Levela;
        	 char[] temp;
    
        	 levelx = new char[3000][3000];
            Levela = new char[3000][3000];
        	String level[];
        	FileInputStream fin;
    
        	 level = new String [50];
             String MoveSet;
        	try
        	{
        		fin = new FileInputStream ("d:/School Programmation3/Niveau test.txt");
    
        	 for(  i = 0; i < 45; i++)
        	 {
        	   level[i] = new DataInputStream(fin).readLine() ;
    
        	   for (j = 0; j < 122; j++)
        	   {
        	   	levelx[i][j] = level[i].charAt(j);
        	   }
        	  }
        		fin.close();
        	}
    
        	catch (IOException e)
        	{
        		System.err.println("Unable to read from file");
        		System.exit(-1);
        	}
        	Joueur3 frame = new Joueur3();
        	frame.setTitle("Dungueons Of Darkness");
        	frame.setLocationRelativeTo(null);
        	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        	frame.setSize(1280, 800);
        	frame.setVisible(true);
            frame.setLocation(0,0);
    
        	while (NewGame < 100)
        	{
        		if (NewGame == 1)
        		{
        		 Gamerun = 1;
        		 frame.setVisible(false);
    
        			 for(i = 0; i < 45; i++)
        	           {
        	           for (j = 0; j < 122; j++)
        	           {
        	   	         Levela[i][j] = levelx[i][j];
        	             }
        		        }
        		   levelarea = 1;
        		   NewGame = 0;
        		}
        		if (levelarea == 1)
        		{
        			 for(i = 0; i < 45; i++)
        	        {
        	           for (j = 0; j < 122; j++)
        	           {
        	   	         Levela[i][j] = levelx[i][j];
        	           }
        		     }
        		}
        	     if (Player1.LifePoints > 0)
        		Levela[Player1.UpDownLocation][Player1.LeftRightLocation] = 'P';            
        		if (levelarea == 1)
        		{
        			 for(i = 0; i < 45; i++)
        	           {
        	           for (j = 0; j < 122; j++)
        	           {
        	             System.out.print(Levela[i][j]);
        	             }
        	            System.out.println("");
        		        }		       	
        		}
    
        	  if (Gamerun == 1)
        	  {
           MoveSet = in.nextLine();
    
           System.out.println("You typed : " + MoveSet);
    
             if (Levela[Player1.UpDownLocation][Player1.LeftRightLocation  + 1] == '.' )
             {
               if (MoveSet.indexOf('r') == 0)
                {
                Player1.LeftRightLocation += 1;
                if (Player1.LifePoints > 0)
        		Levela[Player1.UpDownLocation][Player1.LeftRightLocation] = 'P';
                }
              }
        	   if (Levela[Player1.UpDownLocation + 1][Player1.LeftRightLocation  ] == '.' )
        	  if (Levela[Player1.UpDownLocation][Player1.LeftRightLocation + 1] != '_' )
             {
               if (MoveSet.indexOf('d') == 0)
                {
                Player1.UpDownLocation += 1;
                  if (Player1.LifePoints > 0)
        		Levela[Player1.UpDownLocation][Player1.LeftRightLocation] = 'P';
                }
              }
               if (Levela[Player1.UpDownLocation][Player1.LeftRightLocation  - 1] == '.' )
        	  if (Levela[Player1.UpDownLocation][Player1.LeftRightLocation - 1] != '|' )
             {
               if (MoveSet.indexOf('l') == 0)
                {
                 if (Player1.LifePoints > 0)
        		Levela[Player1.UpDownLocation][Player1.LeftRightLocation] = 'P';
                Player1.LeftRightLocation -= 1;
                }
              }
        	   if (Levela[Player1.UpDownLocation - 1][Player1.LeftRightLocation] == '.' )
        	    if (Levela[Player1.UpDownLocation - 1 ][Player1.LeftRightLocation] != '_' )
               {
               if (MoveSet.indexOf('u') == 0)
                {
                Player1.UpDownLocation -= 1;
                	if (Player1.LifePoints > 0)
        		Levela[Player1.UpDownLocation][Player1.LeftRightLocation] = 'P';
                }
               }			
        	     }   	   						
        	  }
        	}  	
      static class KeyboardPanel extends JPanel
      {
      	private int x = 100;
      	private int y = 100;
      	private char keyChar = 'A';
    
      	public KeyboardPanel()
      	{
      		addKeyListener(new KeyAdapter()  {
      			public void keyPressed(KeyEvent e) {
    
      				switch (e.getKeyCode())
      				{
      					case KeyEvent.VK_N:	NewGame = 1; break;
      					default: keyChar = e.getKeyChar();
      				}
      			      repaint();
      			}
      	});
      }
      }
    }
    Technically, when you run the program, a gif page appears (that I put as a placeholder) and when you press the letter 'n' JCreator runs the program, displaying a character P on a .txt field made using the Notepad on Windows. You can move it left, right, down, and up andf it will move accordinly. However, each time I make such action, the compiler has to rewrite to whole .txt field with the new position.
    My former tutor said that in order for it to be "real-time", we have to use an applet, hence my posting this thread here. I humbly ask the community to please help me in this ordeal.
    Last edited by Unlimited; 09-30-2014 at 03:57 AM.

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

    Default Re: Help converting a program into an applet

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    Basically, replace the JFrame as the primary container with the JApplet. Use the applet's init method instead of the main() method and constructor.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Unlimited is offline Member
    Join Date
    Sep 2014
    Posts
    6
    Rep Power
    0

    Default Re: Help converting a program into an applet

    Thanks for the code tag, I've changed it.
    For the Applet, when I tried to run some on my computer, it says that it can't because the main() method isn't there... or that I don't have Java Fx or whatnot when I do with the latest versions. The only one I ran had one, but it was and applet using WindowListener in order to display the word "hello".

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

    Default Re: Help converting a program into an applet

    applets are executed from an html page by a browser. Do some research on how to setup and execute an applet.
    The tutorial: http://docs.oracle.com/javase/tutori...let/index.html
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Help converting a program into an applet

    Quote Originally Posted by Unlimited View Post
    Technically, when you run the program, a gif page appears (that I put as a placeholder) and when you press the letter 'n' JCreator runs the program, displaying a character P on a .txt field made using the Notepad on Windows. You can move it left, right, down, and up andf it will move accordinly. However, each time I make such action, the compiler has to rewrite to whole .txt field with the new position.
    My former tutor said that in order for it to be "real-time", we have to use an applet, hence my posting this thread here. I humbly ask the community to please help me in this ordeal.
    First, the compiler doesn't rewrite anything. The program is writing the output.
    And I am not certain what your "tutor" is talking about. The term real-time
    is not appropriate. Interactive is more like it. Assuming I understand what
    you are trying to do you don't need to convert it to an applet. An applet is a java
    program downloaded from a website and executed on your machine via a java plugin.

    What I presume you want to do is not have to redraw the screen each time a move is
    made. There are two methods. The first goes back tot he first adventure games and
    involves cursor addressing. You only update the section of the screen what has changed
    by targeting a row column. You would need a console that emulates something that
    handles that (like an old VT52 or VT100). Another way would be to convert the program
    to use graphics and the painting capabilities of Java. The latter requires more expertise and
    familiarity with the painting and event system of Java.

    If you want a program to be interactive across the web, you still don't need an applet. Just
    a mechanism for sending and receiving updates about the game and displaying them on
    the participants screens.

    Now I could be all wrong about this but I don't believe converting the program to an applet
    is going to solve your problem.

    Regards,
    Jim
    Last edited by jim829; 09-30-2014 at 04:51 AM.
    gimbal2 likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Unlimited is offline Member
    Join Date
    Sep 2014
    Posts
    6
    Rep Power
    0

    Default Re: Help converting a program into an applet

    To some extent, you're right, I don't want to redraw the screen each time a move is made. For the applet, the tutor and I were researching that option, but if there's another one, I would gladly take it. What are you taking about on the mechanism for sending and receiving updates ? Would that solve my problem ?

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Help converting a program into an applet

    I really don't understand what the problem here is. I see a good-hearted attempt to guess that it has to do with redrawing the screen, but that is not really a problem. I've built games using pure Java and I let the "screen" (actually its not the screen but the drawable area of the program) repaint itself completely 50 times per second whether something changed or not.

    This program as far I as can tell only repaints when a key is pressed.

    The problem really has to be described more properly. And that probably means that the problem needs to be personally understood better first. Its impossible to get help when you can't explain properly what you need help with.

    Rewinding the progress in this thread, this is the heart of the matter:

    the compiler has to rewrite to whole .txt field with the new position
    That needs to be corrected to this:

    the program has to rewrite to whole .txt field with the new position
    But what does that mean???. What is the ".txt field" ???
    Last edited by gimbal2; 09-30-2014 at 01:56 PM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: Help converting a program into an applet

    Quote Originally Posted by Unlimited View Post
    What are you taking about on the mechanism for sending and receiving
    updates? Would that solve my problem?
    I honestly don't know (but doubt it). As Gimbal pointed out, we still aren't quite certain what the problem
    is. And as he said, I too have written programs that update the screen many times a second with no flicker
    (and my lap top is over 10 years old). My quick perusal of your program made me think about drawing
    to a console, which can be very slow or at least noticeable compared to doing actual graphics. My suggestion
    about sending and receiving updates was only if are playing against someone on the web. It has nothing to do
    with display speed. I probably shouldn't have even mentioned it.

    To get help on this you need to provide more insight into the problems you are having. A small Short, Self Contained, Correct Example
    that demonstrates the problem would be useful (one that we can compile and run).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    Unlimited is offline Member
    Join Date
    Sep 2014
    Posts
    6
    Rep Power
    0

    Default Re: Help converting a program into an applet

    To Mr. Gimbal2

    the .txt field is the player area of movement that I've written using the notepad program. That's where the player, that the compiler outputs 'P' can move around. It's a man-made rectangle with dots in it. Each time I move the player, the compiler, redraws the whole page with the player new position instead of the 'P' character only moving on its own. My tutor was suggesting that we use an applet to correct that, hence why I'm here. Basically this program is bigger, because it's meant to be used as a game where ther's enemy to be defeated. Having the compiler redraw the whole playing field is a hassle for those types of games, making gameplay slower and difficult to see 'P' 's movements. I hope that helps.

  10. #10
    Unlimited is offline Member
    Join Date
    Sep 2014
    Posts
    6
    Rep Power
    0

    Default Re: Help converting a program into an applet

    To Mr jim829

    No, the game is not meant to be played against someone in the web, but you're right, the console redraws the field all the time. to give you an opportunity to run it, let's see.
    Here's the .txt text that I copied from my Notepad. It was a bit bigger, but I repurposed it for the sake of the forum. That's the playing field that the console outputs with the 'P' character on it. It's supposed to be put where there the line D: School Programmation3 etc. Obviously, you'll have to adapt it according to your computer.
    __________________________________________________ _____________________
    |................................................. .................................................. .......................|.
    |................................................. .................................................. .......................|.
    |................................................. .................................................. .......................|.
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    |................................................. .................................................. .......................|
    __________________________________________________ _____________________
    .................................................. .................................................. ........................
    .................................................. .................................................. ........................
    .................................................. .................................................. ........................
    .................................................. .................................................. ........................

    You also need any .gif file you have that you'll place on the line next to Image Icon close to the beginning of the program. I trust you can easily find such thing, and you should normally be able to run it.

    When you do, you will be shown the .gif file you took (or not), the most important here is that the whole thing compiles. No matter what is shown, you have to press the letter n to access the "game". You will see the 'P' shown on the console inside the field. You can press either l for left, r for right, u for up, d for down and validate with enter, and the character will move accordinly, but because the compiler has to redraw the whole thing, you will see the previous page go up as the new page is shown. the compiler will keep the previous pages within the console instead of making them dissapear, and what I want is to avoid that problem by having my character moving when I press a command and validate in "real-time". Hope that helps.

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

    Default Re: Help converting a program into an applet

    As having been said before you are using wrong terminology. The compiler is a software program that converts your code into
    byte code which can be executed by the JVM. So it is your program, not the compiler, that has to keep updating the image (or .txt
    files as it were). In java, you can actually cause objects to move up down left and right by keyboard entry or by mouse operations.
    You don't need to type enter to have this work.

    Also, you said you have been wanting for years to make an applet out of the program (the one you pasted). Is that the original code or
    your attempt at converting existing code? Where did the code come from?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    Unlimited is offline Member
    Join Date
    Sep 2014
    Posts
    6
    Rep Power
    0

    Default Re: Help converting a program into an applet

    As I said before, this code is a stripped down version of the original code which was made by my tutor and I. In this code, I have to type enter in order for my character to make a new action after writing a letter, the way I programmed it. Sorry about the terminology, I'ts been a while since I last programmed, so...

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

    Default Re: Help converting a program into an applet

    If this is really not about applets I'll move it to the New to Java section.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Help converting a program into an applet

    Okay, here is a small snippet of code which demonstrates what I was talking about. You should be able to copy and run it
    as is. It just moves a circle around a frame in response to keyboard arrow input. I doubt you will get yours to work the
    way it was programmed. I will also tell you there are problems with this demo in terms of border detection, repeated
    character delay, etc. All can be easily fixed, if you are experienced with Java. My recommendation to you would be to learn
    Java starting with the basics. I know it is highly desirable to want to jump in and create a game or do the more advanced
    stuff but it just isn't realistic unless you know the language and its subtleties.

    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class SimpleMoveDemo extends JPanel {
       private JFrame           frame;
       private final static int SIZE        = 500;
       private final static int MOVE_AMOUNT = 10;
       private int ydir = SIZE/2;
       private int xdir = SIZE/2;
    
       private SimpleMoveDemo() {
          frame = new JFrame("Move Object");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setPreferredSize(new Dimension(SIZE, SIZE));
          frame.add(this);
          addKeyListener(new MyKeyListener());
          setFocusable(true);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                new SimpleMoveDemo();
             }
          });
       }
    
       public void paintComponent(Graphics g) {
          super.paintComponent(g);
          Graphics2D g2d = (Graphics2D) g;
          g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
          g2d.setColor(Color.blue);
          g2d.fillOval(xdir,ydir,10,10);
    
       }
    
       private class MyKeyListener extends KeyAdapter {
          public void keyPressed(KeyEvent ke) {
             int kcode = ke.getExtendedKeyCode();
             switch (kcode) {
                case KeyEvent.VK_UP:
                   ydir -= MOVE_AMOUNT;
                   break;
                case KeyEvent.VK_DOWN:
                   ydir += MOVE_AMOUNT;
                   break;
                case KeyEvent.VK_LEFT:
                   xdir -= MOVE_AMOUNT;
                   break;
                case KeyEvent.VK_RIGHT:
                   xdir += MOVE_AMOUNT;
                   break;
                default:
             }
             repaint();
          }
       }
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: Help converting a program into an applet

    Moved to New to Java
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Need help converting a program to an applet
    By Iam_Sam in forum New To Java
    Replies: 3
    Last Post: 11-30-2013, 03:45 PM
  2. Converting Connect Four to Applet
    By j0sh77 in forum Java Applets
    Replies: 5
    Last Post: 05-17-2013, 04:36 PM
  3. need help converting java program into a java applet
    By i'mhighdef in forum Java Applets
    Replies: 3
    Last Post: 05-04-2011, 11:55 PM
  4. Need Help Converting This to Applet Plz
    By RecursiveEndUserFromMars in forum Java Applets
    Replies: 2
    Last Post: 01-19-2011, 04:10 AM
  5. Converting an App to Applet
    By josephdcoleman in forum New To Java
    Replies: 1
    Last Post: 02-21-2009, 07:07 AM

Posting Permissions

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