Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: Help needed with applets

  1. #1
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Help needed with applets

    How do I convert my following code into an applet for my website?

    import java.io.*;
    import java.net.*;
    import java.util.regex.*;
    import java.util.Calendar;
    import java.util.Random;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class newIrc extends JPanel implements ActionListener {
        protected static JTextField textField;
        protected static JTextArea textArea;
        private final static String newline = "\n";
    	static Calendar now;
        static int hour;
        static int minute;
        static int second;
        static String text;
        static BufferedWriter bw;
        static String nick;
        String message2;
        String[] tok2;
        public newIrc() {
            super(new GridBagLayout());
     
            textArea = new JTextArea(10, 20);
            textArea.setEditable(false);
            JScrollPane scrollPane = new JScrollPane(textArea);
     
            textField = new JTextField(65);
            textField.addActionListener(this);
            textField.setBackground(Color.BLACK);
            textField.setForeground(Color.WHITE);
     
     
            //Add Components to this panel.
            GridBagConstraints c = new GridBagConstraints();
            c.gridwidth = GridBagConstraints.REMAINDER;
     
            c.fill = GridBagConstraints.BOTH;
            c.weightx = 1.0;
            c.weighty = 1.0;
            add(scrollPane, c);
     
            c.fill = GridBagConstraints.HORIZONTAL;
            add(textField, c);
     
     
        }
     
        public void actionPerformed(ActionEvent evt) {
        	text = textField.getText();   	
        	tok2 = text.split(" ");
     if (tok2[0].startsWith("/")) {
    	 message2 = "";
    	 if (tok2[0].equalsIgnoreCase("/nick")) { 
    		 message2 = "NICK "+tok2[1]+"\n"; 
    	     nick = tok2[1];		 
    	 }
    	 if (tok2[0].equalsIgnoreCase("/msg")) { message2 = "PRIVMSG "+tok2[1]+" :"+text.substring(text.indexOf(tok2[2]), text.length())+"\n"; }
    	 if (tok2[0].equalsIgnoreCase("/notice")) { 
    		 message2 = "NOTICE "+tok2[1]+" :"+text.substring(text.indexOf(tok2[2]), text.length())+"\n"; 
    		 addItem(gettime()+"TO: ("+tok2[1]+") "+text.substring(text.indexOf(tok2[2]), text.length()));
    	 }
     
    	 try {
    		 bw.write(message2);
    		 bw.flush();
    	 } catch (IOException e) {}
    	 	textField.setText(""); 
     } else {
     
        	try {
    			bw.write("PRIVMSG #brt93yoda :"+text+"\n");
    			bw.flush();
        	} catch (IOException e) {}
        	textField.setText("");        
            addItem(gettime()+"<"+nick+"> "+text);
        }
        }
     
        public static void addItem(String text){
            textArea.append(text + newline);
            textArea.setCaretPosition(textArea.getDocument().getLength());
        }
     
        public static String gettime(){
        	now = Calendar.getInstance(); 
        	hour = now.get(Calendar.HOUR_OF_DAY);
        	if (hour > 12) { hour-=12; }
        	minute = now.get(Calendar.MINUTE);
            second = now.get(Calendar.SECOND);
         return "("+hour+":"+minute+":"+second+") ";
        }
     
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("Bretts Java Client");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            //Add contents to the window.
            frame.add(new newIrc());
     
            //Display the window.
            frame.pack();
            frame.setResizable(false);
            frame.setVisible(true);
        }
     
     
    	public static void main(String[] args) {
    		Random generator = new Random();
    		int r = generator.nextInt();    
    		nick = "USER"+r;
    		    createAndShowGUI();
    		    String server = "irc.swiftirc.net";	        
    	        String address = "thenullbyte.org";
    	        String channel = "#brt93yoda";
    	        String[] tok;
    	        int port = 6667;
     
    	        try {
     
    	            //our socket we're connected with
    	            Socket irc = new Socket( server, port );
    	            //out output stream
    	            bw = new BufferedWriter( new OutputStreamWriter( irc.getOutputStream() ) );
    	            //our input stream
    	            BufferedReader br = new BufferedReader( new InputStreamReader( irc.getInputStream() ) );
     
     
    	            bw.write( "NICK " + nick + "\n" );
    	            bw.write( "USER " + nick + " " + address + ": Brt93yodas Java Client\n" );
    	            bw.flush();
     
    	            bw.write( "JOIN " + channel + "\n" );
    	            bw.flush();
    	            addItem("Connecting to Server");
     
    	            String currLine = null;
    	            String message = null;
    	            int length; 
    	            int i;
    	            while( ( currLine = br.readLine() ) != null )
    	            {
     
    	            	message = currLine;
    	            	tok = currLine.split(" ");
    	                if (tok[1].equalsIgnoreCase("PRIVMSG")) { message = gettime()+"<"+currLine.substring(1, currLine.indexOf("!"))+"> "+tok[3].substring(1)+" "; 
    	                length = tok.length-1;
    	                for (i=4;i<=length;i++) { message+= tok[i]+" "; }
    	                }
    	                if (tok[1].equalsIgnoreCase("NOTICE") && (currLine.indexOf("!") > 0)) { message = gettime()+"FROM: ("+currLine.substring(1, currLine.indexOf("!"))+") "+tok[3].substring(1)+" "; 
    	                length = tok.length-1;
    	                for (i=4;i<=length;i++) { message+= tok[i]+" "; }
    	                }
     
     
    	                if (tok[1].equals("JOIN")) { message = gettime()+" * "+tok[0].substring(1).replace("!", "(")+") has joined "+tok[2].substring(1); }
    	                if (tok[1].equals("PART")) { message = gettime()+" * "+tok[0].substring(1).replace("!", "(")+") has left "+tok[2]; }
    	                if (tok[1].equals("INVITE")) {message = "EMPTY"; }
    	                if (tok[1].equals("KICK")) { message = gettime()+" * "+tok[3]+" was kicked by "+currLine.substring(1, currLine.indexOf("!"))+" ("+currLine.substring(currLine.indexOf(tok[4])+1, currLine.length())+")"; }
    	                if (tok[1].equals("NICK")) { message = currLine.substring(1, currLine.indexOf("!"))+" is now known as "+tok[2].substring(1); }
    	                Pattern pingRegex = Pattern.compile( "^PING", Pattern.CASE_INSENSITIVE );
    	                Matcher ping = pingRegex.matcher( currLine );
    	                if( ping.find() )
    	                {
    	                	bw.write( "PONG " + channel + "\n" );
    	                    bw.flush();
    	                    message = "EMPTY";
    	                }
    	                if (message != "EMPTY") { addItem(message); }
    	            } 
    	        }
    	            catch ( UnknownHostException e ) {
    	                System.err.println( "No such host" );
    	            } catch ( IOException e ) {
    	                System.err.println( "There was an error connecting to the host" );
    	            } 
     
     
    }
    }
    Last edited by Brt93yoda; July 7th, 2010 at 04:13 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help needed with applets

    Move the code in main() to an init() method. Have the class extend JApplet. Get rid of createAndShow()

  3. #3
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Help needed with applets

    I tried doing that, and I get a bunch of random errors.

    Heres the errors:
    java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string (or null)
    	at java.awt.BorderLayout.addLayoutComponent(Unknown Source)
    	at javax.swing.JRootPane$1.addLayoutComponent(Unknown Source)
    	at java.awt.Container.addImpl(Unknown Source)
    	at java.awt.Container.add(Unknown Source)
    	at javax.swing.JApplet.addImpl(Unknown Source)
    	at java.awt.Container.add(Unknown Source)
    	at webIrc.<init>(webIrc.java:44)
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    	at java.lang.reflect.Constructor.newInstance(Unknown Source)
    	at java.lang.Class.newInstance0(Unknown Source)
    	at java.lang.Class.newInstance(Unknown Source)
    	at sun.applet.AppletPanel.createApplet(Unknown Source)
    	at sun.applet.AppletPanel.runLoader(Unknown Source)
    	at sun.applet.AppletPanel.run(Unknown Source)
    	at java.lang.Thread.run(Unknown Source)

    Heres my code:
    import java.io.*;
    import java.net.*;
    import java.util.regex.*;
    import java.util.Calendar;
    import java.util.Random;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.applet.*;
     
    public class webIrc extends JApplet implements ActionListener {
        protected static JTextField textField;
        protected static JTextArea textArea;
        private final static String newline = "\n";
    	static Calendar now;
        static int hour;
        static int minute;
        static int second;
        static String text;
        static BufferedWriter bw;
        static String nick;
        String message2;
        String[] tok2;
        public webIrc() {
     
     
            textArea = new JTextArea(10, 20);
            textArea.setEditable(false);
            JScrollPane scrollPane = new JScrollPane(textArea);
     
            textField = new JTextField(65);
            textField.addActionListener(this);
            textField.setBackground(Color.BLACK);
            textField.setForeground(Color.WHITE);
     
     
            //Add Components to this panel.
            GridBagConstraints c = new GridBagConstraints();
            c.gridwidth = GridBagConstraints.REMAINDER;
     
            c.fill = GridBagConstraints.BOTH;
            c.weightx = 1.0;
            c.weighty = 1.0;
            add(scrollPane, c);
     
            c.fill = GridBagConstraints.HORIZONTAL;
            add(textField, c);
     
     
        }
     
        public void actionPerformed(ActionEvent evt) {
        	text = textField.getText();   	
        	tok2 = text.split(" ");
     if (tok2[0].startsWith("/")) {
    	 message2 = "";
    	 if (tok2[0].equalsIgnoreCase("/nick")) { 
    		 message2 = "NICK "+tok2[1]+"\n"; 
    	     nick = tok2[1];		 
    	 }
    	 if (tok2[0].equalsIgnoreCase("/msg")) { message2 = "PRIVMSG "+tok2[1]+" :"+text.substring(text.indexOf(tok2[2]), text.length())+"\n"; }
    	 if (tok2[0].equalsIgnoreCase("/notice")) { 
    		 message2 = "NOTICE "+tok2[1]+" :"+text.substring(text.indexOf(tok2[2]), text.length())+"\n"; 
    		 addItem(gettime()+"TO: ("+tok2[1]+") "+text.substring(text.indexOf(tok2[2]), text.length()));
    	 }
     
    	 try {
    		 bw.write(message2);
    		 bw.flush();
    	 } catch (IOException e) {}
    	 	textField.setText(""); 
     } else {
     
        	try {
    			bw.write("PRIVMSG #brt93yoda :"+text+"\n");
    			bw.flush();
        	} catch (IOException e) {}
        	textField.setText("");        
            addItem(gettime()+"<"+nick+"> "+text);
        }
        }
     
        public static void addItem(String text){
            textArea.append(text + newline);
            textArea.setCaretPosition(textArea.getDocument().getLength());
        }
     
        public static String gettime(){
        	now = Calendar.getInstance(); 
        	hour = now.get(Calendar.HOUR_OF_DAY);
        	if (hour > 12) { hour-=12; }
        	minute = now.get(Calendar.MINUTE);
            second = now.get(Calendar.SECOND);
         return "("+hour+":"+minute+":"+second+") ";
        }
     
     
     
     
    	public void init() {
    		Random generator = new Random();
    		int r = generator.nextInt();    
    		nick = "USER"+r;
     
    		    String server = "irc.swiftirc.net";	        
    	        String address = "thenullbyte.org";
    	        String channel = "#brt93yoda";
    	        String[] tok;
    	        int port = 6667;
     
    	        try {
     
    	            //our socket we're connected with
    	            Socket irc = new Socket( server, port );
    	            //out output stream
    	            bw = new BufferedWriter( new OutputStreamWriter( irc.getOutputStream() ) );
    	            //our input stream
    	            BufferedReader br = new BufferedReader( new InputStreamReader( irc.getInputStream() ) );
     
     
    	            bw.write( "NICK " + nick + "\n" );
    	            bw.write( "USER " + nick + " " + address + ": Brt93yodas Java Client\n" );
    	            bw.flush();
     
    	            bw.write( "JOIN " + channel + "\n" );
    	            bw.flush();
    	            addItem("Connecting to Server");
     
    	            String currLine = null;
    	            String message = null;
    	            int length; 
    	            int i;
    	            while( ( currLine = br.readLine() ) != null )
    	            {
     
    	            	message = currLine;
    	            	tok = currLine.split(" ");
    	                if (tok[1].equalsIgnoreCase("PRIVMSG")) { message = gettime()+"<"+currLine.substring(1, currLine.indexOf("!"))+"> "+tok[3].substring(1)+" "; 
    	                length = tok.length-1;
    	                for (i=4;i<=length;i++) { message+= tok[i]+" "; }
    	                }
    	                if (tok[1].equalsIgnoreCase("NOTICE") && (currLine.indexOf("!") > 0)) { message = gettime()+"FROM: ("+currLine.substring(1, currLine.indexOf("!"))+") "+tok[3].substring(1)+" "; 
    	                length = tok.length-1;
    	                for (i=4;i<=length;i++) { message+= tok[i]+" "; }
    	                }
     
     
    	                if (tok[1].equals("JOIN")) { message = gettime()+" * "+tok[0].substring(1).replace("!", "(")+") has joined "+tok[2].substring(1); }
    	                if (tok[1].equals("PART")) { message = gettime()+" * "+tok[0].substring(1).replace("!", "(")+") has left "+tok[2]; }
    	                if (tok[1].equals("INVITE")) {message = "EMPTY"; }
    	                if (tok[1].equals("KICK")) { message = gettime()+" * "+tok[3]+" was kicked by "+currLine.substring(1, currLine.indexOf("!"))+" ("+currLine.substring(currLine.indexOf(tok[4])+1, currLine.length())+")"; }
    	                if (tok[1].equals("NICK")) { message = currLine.substring(1, currLine.indexOf("!"))+" is now known as "+tok[2].substring(1); }
    	                Pattern pingRegex = Pattern.compile( "^PING", Pattern.CASE_INSENSITIVE );
    	                Matcher ping = pingRegex.matcher( currLine );
    	                if( ping.find() )
    	                {
    	                	bw.write( "PONG " + channel + "\n" );
    	                    bw.flush();
    	                    message = "EMPTY";
    	                }
    	                if (message != "EMPTY") { addItem(message); }
    	            } 
    	        }
    	            catch ( UnknownHostException e ) {
    	                System.err.println( "No such host" );
    	            } catch ( IOException e ) {
    	                System.err.println( "There was an error connecting to the host" );
    	            } 
     
     
    }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help needed with applets

    at webIrc.<init>(webIrc.java:44)
    look at line 44 and see if the error makes any sense
    cannot add to layout: constraint must be a string (or null)
    Where do you set the layout manager?

  5. #5
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Help needed with applets

    Im not very good with Java GUI's, so i'm not sure what you mean.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help needed with applets

    public newIrc() {
    super(new GridBagLayout());
    You need to set the layout manager for the Applet to be able use: add(...,c);
    Or remove all references to the GridbagConstraints class and the c variable.

  7. #7
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Help needed with applets

    I removed all references to girdbagConstraints, and nothing shows up for the applet.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help needed with applets

    Did you leave the add() statements? They are what will put something in the applet to be seen.

  9. #9
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Help needed with applets

    Hahaha brain fart. Thank you lol

  10. #10
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Help needed with applets

    Hahaha brain fart. Thank you lol

    EDIT: Still doesn't work
    ERRORS:
    Java Plug-in 1.6.0_18
    Using JRE version 1.6.0_18-b07 Java HotSpot(TM) Client VM
    User home directory = C:\Users\russ
    ----------------------------------------------------
    c:   clear console window
    f:   finalize objects on finalization queue
    g:   garbage collect
    h:   display this help message
    l:   dump classloader list
    m:   print memory usage
    o:   trigger logging
    q:   hide console
    r:   reload policy configuration
    s:   dump system and deployment properties
    t:   dump thread list
    v:   dump thread stack
    x:   clear classloader cache
    0-5: set trace level to <n>
    ----------------------------------------------------
     
     
    java.lang.reflect.InvocationTargetException
    	at com.sun.deploy.util.DeployAWTUtil.invokeAndWait(Unknown Source)
    	at sun.plugin2.applet.Plugin2Manager.runOnEDT(Unknown Source)
    	at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
    	at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    	at java.lang.Thread.run(Unknown Source)
    Caused by: java.lang.ClassCastException: newIrc cannot be cast to java.applet.Applet
    	at sun.plugin2.applet.Plugin2Manager$12.run(Unknown Source)
    	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    Exception: java.lang.reflect.InvocationTargetException
    java.lang.Exception: comp is null
    	at sun.plugin2.applet.Plugin2Manager.runOnEDT(Unknown Source)
    	at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    	at java.lang.Thread.run(Unknown Source)
    java.security.AccessControlException: access denied (java.net.SocketPermission irc.swiftirc.net resolve)
    	at java.security.AccessControlContext.checkPermission(Unknown Source)
    	at java.security.AccessController.checkPermission(Unknown Source)
    	at java.lang.SecurityManager.checkPermission(Unknown Source)
    	at java.lang.SecurityManager.checkConnect(Unknown Source)
    	at sun.plugin2.applet.Applet2SecurityManager.checkConnect(Unknown Source)
    	at java.net.InetAddress.getAllByName0(Unknown Source)
    	at java.net.InetAddress.getAllByName(Unknown Source)
    	at java.net.InetAddress.getAllByName(Unknown Source)
    	at java.net.InetAddress.getByName(Unknown Source)
    	at java.net.InetSocketAddress.<init>(Unknown Source)
    	at java.net.Socket.<init>(Unknown Source)
    	at webIrc.init(webIrc.java:107)
    	at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    	at java.lang.Thread.run(Unknown Source)
    Exception: java.security.AccessControlException: access denied (java.net.SocketPermission irc.swiftirc.net resolve)
    Exception in thread "AWT-EventQueue-2" java.lang.NullPointerException
    	at java.awt.LightweightDispatcher$3.run(Unknown Source)
    	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
     
    	at java.awt.EventDispatchThread.run(Unknown Source)
    Exception in thread "AWT-EventQueue-2" java.lang.NullPointerException
    	at java.awt.LightweightDispatcher$3.run(Unknown Source)
    	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    ETC.....

    Code:
    import java.io.*;
    import java.net.*;
    import java.util.regex.*;
    import java.util.Calendar;
    import java.util.Random;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.applet.*;
     
    public class webIrc extends JApplet implements ActionListener {
        protected static JTextField textField;
        protected static JTextArea textArea;
        private final static String newline = "\n";
    	static Calendar now;
        static int hour;
        static int minute;
        static int second;
        static String text;
        static BufferedWriter bw;
        static String nick;
        String message2;
        String[] tok2;
        public webIrc() {
     
     
            textArea = new JTextArea(10, 20);
            textArea.setEditable(false);
            JScrollPane scrollPane = new JScrollPane(textArea);
     
            textField = new JTextField(65);
            textField.addActionListener(this);
            textField.setBackground(Color.BLACK);
            textField.setForeground(Color.WHITE);
     
     
            //Add Components to this panel.
     
            add(scrollPane);
            add(textField);
     
     
        }
     
        public void actionPerformed(ActionEvent evt) {
        	text = textField.getText();   	
        	tok2 = text.split(" ");
     if (tok2[0].startsWith("/")) {
    	 message2 = "";
    	 if (tok2[0].equalsIgnoreCase("/nick")) { 
    		 message2 = "NICK "+tok2[1]+"\n"; 
    	     nick = tok2[1];		 
    	 }
    	 if (tok2[0].equalsIgnoreCase("/msg")) { message2 = "PRIVMSG "+tok2[1]+" :"+text.substring(text.indexOf(tok2[2]), text.length())+"\n"; }
    	 if (tok2[0].equalsIgnoreCase("/notice")) { 
    		 message2 = "NOTICE "+tok2[1]+" :"+text.substring(text.indexOf(tok2[2]), text.length())+"\n"; 
    		 addItem(gettime()+"TO: ("+tok2[1]+") "+text.substring(text.indexOf(tok2[2]), text.length()));
    	 }
     
    	 try {
    		 bw.write(message2);
    		 bw.flush();
    	 } catch (IOException e) {}
    	 	textField.setText(""); 
     } else {
     
        	try {
    			bw.write("PRIVMSG #brt93yoda :"+text+"\n");
    			bw.flush();
        	} catch (IOException e) {}
        	textField.setText("");        
            addItem(gettime()+"<"+nick+"> "+text);
        }
        }
     
        public static void addItem(String text){
            textArea.append(text + newline);
            textArea.setCaretPosition(textArea.getDocument().getLength());
        }
     
        public static String gettime(){
        	now = Calendar.getInstance(); 
        	hour = now.get(Calendar.HOUR_OF_DAY);
        	if (hour > 12) { hour-=12; }
        	minute = now.get(Calendar.MINUTE);
            second = now.get(Calendar.SECOND);
         return "("+hour+":"+minute+":"+second+") ";
        }
     
     
     
     
    	public void init() {
    		Random generator = new Random();
    		int r = generator.nextInt();    
    		nick = "USER"+r;
     
    		    String server = "irc.swiftirc.net";	        
    	        String address = "thenullbyte.org";
    	        String channel = "#brt93yoda";
    	        String[] tok;
    	        int port = 6667;
     
    	        try {
     
    	            //our socket we're connected with
    	            Socket irc = new Socket( server, port );
    	            //out output stream
    	            bw = new BufferedWriter( new OutputStreamWriter( irc.getOutputStream() ) );
    	            //our input stream
    	            BufferedReader br = new BufferedReader( new InputStreamReader( irc.getInputStream() ) );
     
     
    	            bw.write( "NICK " + nick + "\n" );
    	            bw.write( "USER " + nick + " " + address + ": Brt93yodas Java Client\n" );
    	            bw.flush();
     
    	            bw.write( "JOIN " + channel + "\n" );
    	            bw.flush();
    	            addItem("Connecting to Server");
     
    	            String currLine = null;
    	            String message = null;
    	            int length; 
    	            int i;
    	            while( ( currLine = br.readLine() ) != null )
    	            {
     
    	            	message = currLine;
    	            	tok = currLine.split(" ");
    	                if (tok[1].equalsIgnoreCase("PRIVMSG")) { message = gettime()+"<"+currLine.substring(1, currLine.indexOf("!"))+"> "+tok[3].substring(1)+" "; 
    	                length = tok.length-1;
    	                for (i=4;i<=length;i++) { message+= tok[i]+" "; }
    	                }
    	                if (tok[1].equalsIgnoreCase("NOTICE") && (currLine.indexOf("!") > 0)) { message = gettime()+"FROM: ("+currLine.substring(1, currLine.indexOf("!"))+") "+tok[3].substring(1)+" "; 
    	                length = tok.length-1;
    	                for (i=4;i<=length;i++) { message+= tok[i]+" "; }
    	                }
     
     
    	                if (tok[1].equals("JOIN")) { message = gettime()+" * "+tok[0].substring(1).replace("!", "(")+") has joined "+tok[2].substring(1); }
    	                if (tok[1].equals("PART")) { message = gettime()+" * "+tok[0].substring(1).replace("!", "(")+") has left "+tok[2]; }
    	                if (tok[1].equals("INVITE")) {message = "EMPTY"; }
    	                if (tok[1].equals("KICK")) { message = gettime()+" * "+tok[3]+" was kicked by "+currLine.substring(1, currLine.indexOf("!"))+" ("+currLine.substring(currLine.indexOf(tok[4])+1, currLine.length())+")"; }
    	                if (tok[1].equals("NICK")) { message = currLine.substring(1, currLine.indexOf("!"))+" is now known as "+tok[2].substring(1); }
    	                Pattern pingRegex = Pattern.compile( "^PING", Pattern.CASE_INSENSITIVE );
    	                Matcher ping = pingRegex.matcher( currLine );
    	                if( ping.find() )
    	                {
    	                	bw.write( "PONG " + channel + "\n" );
    	                    bw.flush();
    	                    message = "EMPTY";
    	                }
    	                if (message != "EMPTY") { addItem(message); }
    	            } 
    	        }
    	            catch ( UnknownHostException e ) {
    	                System.err.println( "No such host" );
    	            } catch ( IOException e ) {
    	                System.err.println( "There was an error connecting to the host" );
    	            } 
     
     
    }
    }

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help needed with applets

    access denied (java.net.SocketPermission irc.swiftirc.net resolve)
    Your applet is trying to do something it is NOT allowed to do.
    Applets can only connect to the host that they were loaded from. The Socket at line 107 is trying to connect to a forbidden host.

    How are you loading the applet? Do you load an html file with applet from: irc.swiftirc.net?
    Instead of hardcoding the server address in the program, use the getCode() base method to get the URL for the server that the applet was loaded from.

  12. #12
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Help needed with applets

    I just tested it by putting an html file in the same dir as the class file. Can you explain to me why the socke7t can only connect from the host?

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help needed with applets

    ForSecurity. The applet can only communicate with its host.
    If you load the applet from that site, it will be able to connect to it.

  14. #14
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Help needed with applets

    Oh... The irc.swiftirc.net is an IRC server. So how do I connect to it? getCode("irc.swiftirc.net"); ?

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help needed with applets

    how do I connect to it?
    Use an application. It won't have any Java security issues.

    getCode("irc.swiftirc.net");
    What class is the getCode() method in?

  16. #16
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Help needed with applets

    Well the main code was an app. But I want a chat program for my website.

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help needed with applets

    Why did you change it from an app? An app will be able to connect to any website.
    An applet can ONLY connect to the site it is loaded from.
    If you have a website with a server that the applet is supposed to connect to, put the html and applet classes on that site and it should work.

Similar Threads

  1. help needed
    By The Lost Plot in forum Loops & Control Statements
    Replies: 4
    Last Post: March 16th, 2010, 03:55 AM
  2. Audioclip play makes applets slow
    By Marcus in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 19th, 2010, 01:20 PM
  3. [SOLVED] Help needed!
    By subhvi in forum Web Frameworks
    Replies: 4
    Last Post: February 18th, 2010, 09:26 AM
  4. [SOLVED] A little help needed..
    By JavaStudent87 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 22nd, 2010, 06:54 PM
  5. Learn applets in java
    By mohsendeveloper in forum Java Applets
    Replies: 2
    Last Post: June 25th, 2009, 02:50 PM