Changing a string

A place for tutorials on how to get the most out of Flash

Changing a string

Postby BlueLight » Fri Mar 09, 2012 9:21 pm

Hey so i'm having trouble with my code. The under lining logic works but I'm having trouble with a string that is used for a URL (Location on the computer).
Now my trouble is only happening when my code is being run from a .jar

Look under getURL(filename)
Code: Select All Code
import java.awt.Color;
import java.awt.GridLayout;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.io.Console;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;



/**
 * @category class
 * @
 * @author Jordan C.D
 * @author Blue Light
 * {@link http://www.comweb.nl/java/Console/Console.html}
 */
@SuppressWarnings({ "serial", "unused" })
public class mapPanel extends JPanel{
   GridLayout layout = new GridLayout(3,0);
   static private char[][] map;
   static private JLabel[][] tileMap;
   URL[] gameURL = {
         getURL("DefualtTileBackup.gif"),
         getURL("Wall.gif"),
         getURL("Floor.gif"),
         getURL("Character.gif"),
         getURL("Monster.gif")
   };


   /**
    * @param args
    */
   public static void main(String[] args) {
      //System.console();
      char[][] charTest = {
            {'@','F','M','S','$'},
            {'M','@','F','S','$'},
            {'$','@','F','M','S'}
      };

      JFrame frame = new JFrame();

      JFrame.setDefaultLookAndFeelDecorated(true);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      mapPanel panel = new mapPanel(charTest);
      panel.setLayout(new GridLayout(3,5));

      frame.add(panel);
      frame.pack();
      frame.setVisible(true);
      
      //String Ur = new String(""+ClassLoader.getSystemResource("mapPanel.class"));
      //System.out.println(Ur);
      //Ur.replace("test%202.jar!/mapPanel.class", "...test"); //if jar use this
      //System.out.println(Ur);
      //Ur.replace("Jar:", "");
      //System.out.println(Ur);
      
   }   


   mapPanel(char[][] map){
      super();
      setMap(map);
      displayMap();
      for(int Y = 0; Y < mapPanel.map.length; Y = Y+1 ){
         for(int X = 0; X < mapPanel.map[Y].length; X = X+1 ){
            this.add(mapPanel.tileMap[Y][X]);
         }
      }

   }

   private static URL getURL(String filename) {
      String Ur = new String(//"jar:file:/C:/Java/Eclipse/FallingStar/Jar/Tile%20test/Tile%20test%202.jar!/mapPanel.class");
            ""+ClassLoader.getSystemResource("mapPanel.class"));
      System.out.println(Ur);
      URL url = null;

      try {
         System.out.println("URL was " + url);

         // This if statement check to see if Jar: is in front and if true it will assume this is a Jar and remove Jar elements to the URL
         //if(!"jar".equals(Ur.substring(0,3))){
            System.out.println("JAR is running");

            Ur.replace("/mapPanel.class", " BLA!"); //if jar use this
         //Ur.replace("jar:", "");
            System.out.println(Ur);

            url = new URL(Ur);
            Ur.replace(//"test%202.jar!
                  "/mapPanel.class", filename);
            System.out.println(Ur + " I hope this worked");
         //}

         /*else{
            //
            System.out.println("CLASS is running");
            System.out.println(Ur);

            url = new URL(Ur.replace("bin/mapPanel.class", filename)); //if class use this
         }   */
      }
      catch (Exception e) { }

      return url;
   }

   private void displayMap() {
      for(int Y=0; Y<map.length;Y = Y+1){
         for(int X=0; X<map[Y].length; X = X+1){


            JLabel tempLabel = new JLabel();
            try {

               switch(map[Y][X]){

               case('$'):
                  System.out.println("Wall, $");
               tempLabel= new JLabel(new ImageIcon(gameURL[1]));
               break;

               case('F'):
                  System.out.println("Floor, #");
               tempLabel = new JLabel(new ImageIcon(gameURL[2]));
               break;

               case('@'):
                  System.out.println("Character, @");               
               tempLabel = new JLabel(new ImageIcon(gameURL[3]));
               break;

               case('M'):
                  System.out.println("Monster, !");
               tempLabel = new JLabel(new ImageIcon(gameURL[4]));
               break;

               default:
                  System.out.println("defualt,?");
                  tempLabel = new JLabel(new ImageIcon(gameURL[0]));
                  break;// end default


               }// end switch
               mapPanel.tileMap[Y][X] = tempLabel;

            } catch (Exception e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
            }

         }
      }
   }

   void setMap(char[][] map){
      mapPanel.map = map;
      mapPanel.tileMap = new JLabel[mapPanel.map.length][mapPanel.map[0].length];
      //displayMap();
   }

   char[][] getMap(){
      return mapPanel.map;
   }
}


So I've been trying to get these two strings to work.
"jar:file:/C:/Java/Eclipse/FallingStar/Jar/Tile%20test/Tile%20test%202.jar!/mapPanel.class"
and what you get when you call this.
""+ClassLoader.getSystemResource("mapPanel.class"));

In theory for me they are the same thing but I can't get either of them to edit.
Here is my system.out.println() information.
Code: Select All Code
jar:file:/C:/Java/Eclipse/FallingStar/Jar/test.jar!/mapPanel.class
URL was null
JAR is running
jar:file:/C:/Java/Eclipse/FallingStar/Jar/test.jar!/mapPanel.class
jar:file:/C:/Java/Eclipse/FallingStar/Jar/test.jar!/mapPanel.class I hope this worked
jar:file:/C:/Java/Eclipse/FallingStar/Jar/test.jar!/mapPanel.class
URL was null
JAR is running
jar:file:/C:/Java/Eclipse/FallingStar/Jar/test.jar!/mapPanel.class
jar:file:/C:/Java/Eclipse/FallingStar/Jar/test.jar!/mapPanel.class I hope this worked
jar:file:/C:/Java/Eclipse/FallingStar/Jar/test.jar!/mapPanel.class
URL was null
JAR is running
jar:file:/C:/Java/Eclipse/FallingStar/Jar/test.jar!/mapPanel.class
jar:file:/C:/Java/Eclipse/FallingStar/Jar/test.jar!/mapPanel.class I hope this worked
jar:file:/C:/Java/Eclipse/FallingStar/Jar/test.jar!/mapPanel.class
URL was null
JAR is running
jar:file:/C:/Java/Eclipse/FallingStar/Jar/test.jar!/mapPanel.class
jar:file:/C:/Java/Eclipse/FallingStar/Jar/test.jar!/mapPanel.class I hope this worked
jar:file:/C:/Java/Eclipse/FallingStar/Jar/test.jar!/mapPanel.class
URL was null
JAR is running
jar:file:/C:/Java/Eclipse/FallingStar/Jar/test.jar!/mapPanel.class
jar:file:/C:/Java/Eclipse/FallingStar/Jar/test.jar!/mapPanel.class I hope this worked
Character, @
Floor, #
Monster, !
defualt,?
Wall, $
Monster, !
Character, @
Floor, #
defualt,?
Wall, $
Wall, $
Character, @
Floor, #
Monster, !
defualt,?



at "I hope this worked" we should have at least removed the "/mapPanel.class"

Sorry but i have to goto class now. Post and edit any typos later.
User avatar
BlueLight
Gangs n' Whores Developer
 
Joined: Sat Jun 04, 2011 8:23 am

Re: Changing a string

Postby BlueLight » Sat Mar 10, 2012 9:53 am

So to make this more clear heres what i was trying to do.

String Ur = jar:file:/C:/Java/Eclipse/FallingStar/Jar/Tile%20test/Tile%20test%202.jar!/mapPanel.class;
Ur.replace("/mapPanel.class", " BLA!");

Now Ur is a string (duh) and i want to remove the first 4 and last 14 characters.
How i was doing this was by using a method in the string class called replace(String search, String replace);
What it did was search for a line of text and replace with another. My problem is that java hates me.
Anyways i said hell with it and decided to just do the lazy thing and cut both ends.
User avatar
BlueLight
Gangs n' Whores Developer
 
Joined: Sat Jun 04, 2011 8:23 am

Re: Changing a string

Postby BlueLight » Sun Mar 11, 2012 1:46 am

Still having trouble. It seems i crashed java... Curses!

I think the line of code was
String Ur = new String(this.getName());

Code: Select All Code
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000000006dac49d0, pid=7320, tid=5184
#
# JRE version: 6.0_24-b07
# Java VM: Java HotSpot(TM) 64-Bit Server VM (19.1-b02 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# V  [jvm.dll+0x2349d0]
#
# An error report file with more information is saved as:
# C:\Java\Eclipse\BitMaps\hs_err_pid7320.log
#
# If you would like to submit a bug report, please visit:
#   http://java.sun.com/webapps/bugreport/crash.jsp
#
User avatar
BlueLight
Gangs n' Whores Developer
 
Joined: Sat Jun 04, 2011 8:23 am

Re: Changing a string

Postby IrrelevantComment » Sun Apr 22, 2012 2:33 pm

Never used Java so can't really help. Have you tried setting a break point to check the replace function is being called? Tried comparing the string before and after the function to see if there is any change?
IrrelevantComment
 
Joined: Tue Mar 15, 2011 7:46 pm

Re: Changing a string

Postby GoRepeat » Sun Apr 22, 2012 3:38 pm

x = "my new string";

LOL AS2

But seriously, Java looks fun.
Picarto LiveStream: https://picarto.tv/GoRepeat
Other Stuff: Click Here
User avatar
GoRepeat
Moderator
 
Joined: Wed Jul 21, 2010 2:26 am
Location: Behind the Looking Glass

Re: Changing a string

Postby BlueLight » Sun Apr 22, 2012 10:11 pm

Ya but a few classes are OCD about correct formatting. I got this to work but it requires you to make a file in the memory so you get a path.
User avatar
BlueLight
Gangs n' Whores Developer
 
Joined: Sat Jun 04, 2011 8:23 am


Return to Tutorials



Who is online

Users browsing this forum: No registered users