JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Let me check: you server is still somewhere on the net but your client is on a LAN behind a NAT router? In which case the server just sees the routers's internet address as the getRemoteSocketAddress() for all such clients?
In that case, can't you use the ip address plus port number to distinguish between these clients? (The router should map them all to different outgoing port numbers.)

ps: Even so, ~s.o.s~ has pointed you in a better direction

masterofpuppets commented: that could work as well :) +5
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

getLocalAddress "Gets the local address to which the socket is bound" (API doc). This is address of the machine where the server is running, so of course its always the same, regardless of where the client is.
getRemoteSocketAddress() will give you the address where the client is, if that's what you need.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

A quick look at the generated code reveals the mechanism - if the inner class accesses a final variable "fred" of the enclosing method then the inner class is given a private final variable called "val$fred" whose value is a copy of fred's.
fred (which was allocated on the stack when the method was invoked) is freed up when the method terminates and is popped of the stack, but val$fred's lifetime is the same as the inner class instance to which it belongs.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

method local variables do go out of scope unless they're declared final, in which case they're preserved between method calls.

Sorry, I don't think that's quite right. final variables do go out of scope, and a new final variable is definitely assigned for each invocation of the method. The same final variable may have different values in different invocations. Inner classes are given their own copy of any final variables they need. They must be final to avoid any ambiguity about which value should be given to the copy.

public class Demo {

   static void doFinal(int i) {
      final int f = i; // different value in each invcation
      new javax.swing.Timer(2000, new java.awt.event.ActionListener() {
         // anonymous inner class
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            System.out.println("f is " + f); // ref to final variable
         }
      }).start();
   }

   public static void main(String[] args) {
      doFinal(1);
      doFinal(2);
      try {
         Thread.sleep(3000); // allow Timers to do their stuff
      } catch (InterruptedException e) {
      }
   }

}
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

All Swing activity - including actionPerformed etc and screen repaints happen on a single thread - the Swing thread or Event Dispatch Thread (EDT). Google it.
If your code is started by from a button the whole Swing thing will be waiting for your method to return - but that's sitting in a loop waiting for the input to finish.
You can update Swing GUI components as much as you like. Nothing will be repainted until your button handler returns.
If you want to do anything that takes time you must put it in a new thread so it doesn't interfere with the GUI. Use your own new Thread, or use the SwingWorker class.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It may not be the "perfect" solution, but it's yours, it's understandable, and it will work.. Go for it!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, that looks OK to me. It may not be the "perfect" way to do it, but it's understandable and it will work. Try it!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

(A guess) this may be a thread problem - text area not being updated until the loop is exited. Does you code above run as part of a GUI - eg in response to a button press or somesuch?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

@J Thanks.
One event would trigger one call to a listener which would then determine the new location, etc

Yes.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

What do you mean by "one event"? Your terms are unusual.

Hi Norm
I guess (I really really hope) he is using a javax.swing.Timer to control this. Swing Timers generate an event every n mSec, which passes an ActionEvent object to your code. So "event" would be the appropriate term in this case.
J

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Hint.
When people animate things moving around the screen they usually have variables to hold the h & v velocity of the object (that's the amount by which the x and y positions change in each time cycle). So every time cycle its just
xPosition = xPosition + xVelocity; (ditto for y)
if xVelocity is (say) +1 the object will move slowly right. If it's -10 the object will move rapidly left.
Does that help?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, that explanation clarifies it. DefaultListModel sounds ideal - it supports a Vector-like data model and was designed for the JList GUI component to track its changes, but the Listener design pattern it uses will work for any other class that wants to know about changes. I doubt you will find a (much) better solution.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

In English:
if x is less than zero go right one step
if x is greater than 500 go left one step
therefore
if x is between 0 and 500 do nothing
... but it should continue to move in whatever its current direction is.

Think about keeping track of its current direction, moving that way while the ball is in range, and changing the direction when the ball reaches 0 or 500.

If in doubt, write it down in English and run through a few cases on a piece of paper to get the algorithm right before you try to code it in Java.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I think you will have to wrap the Vector (or extend the Vector class) in your own class that implements an addListener(...) method and overrides add() etc to broadcast the change event forwarding the change to the the Vector.

This is basically what javax.swing.DefaultListModel does to support JList objects - you may find that a javax.swing.DefaultListModel is all you need.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Cannot find ... Method Add( Java.Lang.String.Java.lang.String)

Its looking for a method Add with two String parameters and it cannot find one. Look at the API doc for your container object and see what methods there are, and what parameters they need.

What are you trying to add to container? If you are trying to display some text you need to put that into a JLabel or somesuch.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

lcdOutput is a String.
container.add(... can only add GUI components eg JLabels
So yes, line 123 is invalid.
You're doing something wrong here. The compiler will have given you an explicit error message about not being able to find a method add(String.. etc.
You must look for such messages and read them. There's no point trying to execute your program when there's a compile error - by that time the proper error message has gone and all you get is a generic "uncompilable code".

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Just a thought...
The best-encapsulated way to implement this may be to have a constructor in each class that takes a StringTokeniser as a parameter - that way all the knowledge about how many fields there are and how to parse and save them is where it should be - in the class itself...

StringTokenizer stknzr = new StringTokenizer(dataRenglon,"_");
String clave = stknzr.nextToken();
switch (clave.charAt(0)) {
   case 'P': Profesor profesor = new Profesor(stknzr);  break;
   //etc
}
...
    public Profesor(StringTokenizer stknzr){
       nombre = stknzr.nextToken();
       dni = stknzr.nextToken();
       despacho = stknzr.nextToken();
       departamento = stknzr.nextToken();
       ...
    }
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Almost.
If you create the instance outside main method as an initialiser for a static variable the you can use that variable within main to access the instance and all the instance's fields.
Your code creates the instance outside main method as an initialiser for a instance variable. That variable will be initialised (and thus create an instance) once for each time an instance of the class is created. If you don't create an instance, then the initialiser will never be executed. You can create that instance inside your main if you want, and that will give you a reference to that instance that you can use... except...
There's an additional complexity in the way the code is constructed:
Helloworld hello=new Helloworld(); will be executed once every time an instance of Helloworld is created. Each time it's executed it creates a new instance, so you have a recursive loop that results very quickly in a stack overflow!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

If you need a display formatted in rows/columns like that then JTable may be the best thing

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

at olmosProyecto.GestionDeHorarios.cargarCurso(GestionDeHorarios.java:44)

That line from the error message tells you which line of your source code (line 44 in GestionDeHorarios.java) threw the error, so you can see what token it was looking for on that line, which should help you identify the error.

ps
Not all lines in the data have 8 tokens, so you can't try to read 8 from every line. Read the first token to see what kind of record it is, then read the correct number of tokens for that record type and create the appropriate object.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Use a JTextPane instead of a JTextArea. Same functionality plus formatted text. API JavaDoc is in the usual places.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

A class variable has one value which is associated with the class itself.
An instance variable has zero or more values, one for each instance of the class that has been created. You can only use it by specifying which instance's value you are referring to, as in hello.call() or, within an instance method, just call(), which is short for this.call(); (ie use the value for the current instance)
In c/c++ terms, a static variable's memory is part of the class's memory, allocated when the class definition is loaded and is never released. An instance variable's memory is allocated dynamically each time an instance of the class is created, and is released when the instance is garbage collected.
Variables are instance variables unless you declare them static.

So in your latest code hello is an instance variable. In your main method (a class method) there is no instance of Helloworld that can be used to resolve hello's value, so the reference is an error.
If you change your declaration of hello to be static then your code will work - hello will only have one value (associated with the class itself) and you can correctly use it within a static method.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Opps, sorry. my mistake. Yes - just create a new Socket(), then use connect(SocketAddress) instead. (or just create a Socket() and let your ConnectToServer method do the rest - better IMHO because of less duplicated code).

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Maybe something like...

SocketAddress addr = new InetSocketAddress("192.168.0.23", 10123);
...
Socket socket10123 = new Socket(addr);
...
public boolean ConnectToServer()  {
  try {
    if (!Globals.socket10123.isConnected()) {
       Globals.socket10123.connect (addr);
     }
     return Globals.socket10123.isConnected();
  } catch(Exception e) {
     e.printStackTrace();
     return false;
  }
}

... but you will still have to deal with the input/output Streams that will have closed when the connection was dropped, and any reads that were blocked waiting on an input Stream.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK, that's weird, but I guess you have your reasons.

Fruit is an Object. It is being saved into the 'List<Food> item' .

will not work if Fruit isn't a subclass of Food

It's impossible to diagnose any more from these isolated fragments of code and an error message with no line number. Can you post all the code with the full error message including the class/line number?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

(30 minutes later) ... no answer. Obviously not urgent after all.

jwenting commented: indeed +15
Salem commented: LOL +17
debasisdas commented: :) +13
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Does the Fruit class extend Food?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I just saw the updated line number. Agree Norm - uninitialised variable -> NPE, but that's not the message you posted - puzzling!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That's a run time message generated when you try to run code that previously failed in compilation. What is the exact message that you get when you first compile it?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I'm just wondering...
If you are using TWO JSliders, why are you not using TWO handlers?
It would solve your problem... wouldn't it?

I agree. I always wonder the same thing when I see a handler that has blocks of
if (e.getSource() == firstJComponent) ...
if (e.getSource() == secondJComponent) ...
...
(even worse if it's messing about with Strings to achieve the same thing)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Set the names for sliders
jSlider1.setName("waveSlider1"); ...
and inside stateChanged method simply write
if ("waveSlider1".equals(source.getName())){

@quuba:
How is that better than
if (source == waveSlider1)
?

and for next time check if code does/doesn't contains any changes, before any your next steps :-)

?
Did you expect me to check 527 lines of code against the original to look for changes?
Even if it was changed, it still needs the copyright notice.
(or did I misunderstand you?)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

mKorbel (or an admin)
I suggest you delete the previous post a.s.a.p. - it's copyrighted material that has been posted in violation of the licence agreement, which begins

* Copyright (c) 1995 - 2008 Sun Microsystems, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.

mKorbel commented: thanks for notice +8
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

if ("waveSlider1".equals(source)){

You are trying to compare a String object with a JSlider Object - always false.
Assuming waveSlider1 is a variable containing the first JSlider you need to compare the variable, not a String containing the variable's name

if (source == waveSlider1) {

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

just looking at your latest code, there's nowhere you add the LWG object to the main JFrame. Since it isn't part of any visible window, it's paintComponent won't be called

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
public void graphicsComponent (Graphics g){

looks odd to me, are you sure you don't mean

public void paintComponent (Graphics g){
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

it draw one sphere but when i enter 2nd.. it gives me error

It would help if you told us exactly what the error was.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

null pointer exception means you have tried to use a variable that has not been initialised. Reading down the messages we see it happened in the add method for Container, and that was called from line 18 of jpad.java
That line reads
add(mainTextArea, BorderLayout.CENTER);
and there's the problem. You have declared the variable mainTextArea but that variable has never been initialised.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I think you may be mis-reading the compiler messages. Class Machine is unknown, so there's no way the compiler would know anything about deprecated anything in that class. The deprecated warning is quite separate, and relates to the Bio class's code so, as Norm says, use Xlint to get the details.
If you don't have the source for Machine do you have a .class file? If so, it's not in your classpath for some reason, if not then you're screwed until you can find a copy.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Do you have the source code for that class?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You can do this kind of thing using the indexOf and substring methods of the String class. So myUrlAsString.indexOf("secret_code=") gives the start point of that string, add 12 to get the index of the first char after it, which is where your target text is located. Use the same approach to find the end of your target (either the end of the URL string, or an "&" maybe?). Then use substring to pick out the text between those two indexes.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Also
If you take the time to read the API doc for JList you will find an explanation with examples of how to use a ListCellRenderer to format JList entries any way you want.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You can allow the user to type in an expression in JavaScript then use/call that as code dynamically at runtime using Java's recent support for scripting languages.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Actually there's quite a difference between

i want a free tool to draw structure chart

and

i asked if any one know a good one suggest it
thanks

If you had tried the second version from the beginning you might have got a better response.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

1. readObject can throw Exceptions (see the JavaDoc for details) so you have to use try/catch to handle them.
2. In Java 1.5 collections like Vector were enhanced to allow specification of what kind of Objects they contain (previously they just contained Objects, which had to be cast at runtime to (hopefully) the appropriate class. Since 1.5 you can say things like
Vector<String> v = new Vector<String>(); // this vector always and only holds Strings
So now Vectors without such a specification are called "raw types", and you are strongly recommended to update them to the improved version. If the vector really does hold absolutely any kind of Object, you can use Vector<Object>.
Look up "Java generics" for more details.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I mean
if (spoof && th<T1) then all your if tests are false and you will do nothing
similarly,
if (!spoof && !(th>=T1 || th<T2)) then all your if tests are false and you will do nothing

Maybe your 3rd line, where nothing was printed, is because one or the other of these two things happened. You can check that by looking at spoof, th, T1 and T2 for that line.
If that is the problem, then you could add these cases to your set of if tests, or you could have a final else clause that prints "none of the above", so you know what's happening.

If I'm using English words you don't know, please ask. I'm English but I live in France, so I know how hard it can be to work in a foreign language.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I personally don't "want" anything. I was just pointing out that there are two cases where your if tests don't print anything out. Since you were looking for an explanation of why nothing was printed for some data, I thought this could be relevant.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

General strategy:
Don't try to use paintComponent for viruses or ships, they do not extend anything with that method. Just have a public paintMe(Graphics g) method for each.
Have the game panel with its overridden paintComponent method, and within that method call paintMe(g) for all the viruses and ships so they paint themselves on the game panel.
Rather than dodgy pause methods, use a javax.swing.Timer timer to call your move/repaint method at regular intervals. This is designed to work properly with Swing's approach to threads.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

What about these two cases?
(spoof && th<T1)
(!spoof && !(th>=T1 || th<T2))
in both these cases you do nothing

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

In that case I would go for the approach outlined in my previous post - chain the records together so you can insert physically at the end but logically in the middle.