Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.

Anunnaki posted:

My CS teacher's been telling me it's a "hybrid." v:shobon:v

That's somewhat correct, although it's a lazy way of explaining it.

The bytecode is interpreted to machine code on the fly at first, until the hotspot just-in-time compiler identifies code that it should spend time compiling to native machine code. It looks for "hot spots" (hence the name) and it sends those units off for further optimization. At that point it's native code and therefore no longer interpreted.

Adbot
ADBOT LOVES YOU

HFX
Nov 29, 2004

TRex EaterofCars posted:

That's somewhat correct, although it's a lazy way of explaining it.

The bytecode is interpreted to machine code on the fly at first, until the hotspot just-in-time compiler identifies code that it should spend time compiling to native machine code. It looks for "hot spots" (hence the name) and it sends those units off for further optimization. At that point it's native code and therefore no longer interpreted.

The same is true for many interpreted languages which use a jit interpreter. It is also why benchmarks can be nearly meaningless when comparing Java to statically compiled languages without having a sufficiently long runtime.

Anyway, the biggest difference between the two virtual machines (32 and 64) is the maximum amount of addressable RAM. 32bit has an upper limit of something around ~1200MB (found by testing). I'm not sure of the limit on the 64bit version at the moment, as 4 gigs was good enough.

HFX fucked around with this message at 15:23 on Sep 21, 2009

Volguus
Mar 3, 2009

HFX posted:

The same is true for many interpreted languages which use a jit interpreter. It is also why benchmarks can be nearly meaningless when comparing Java to statically compiled languages without having a sufficiently long runtime.

Anyway, the biggest difference between the two virtual machines (32 and 64) is the maximum amount of addressable RAM. 32bit has an upper limit of something around ~1200MB (found by testing). I'm not sure of the limit on the 64bit version at the moment, as 4 gigs was good enough.

A 32 bit program (any 32 bit program) cannot access more than 4GB of ram (architecture limitations). In real-life 2GB is the max that normal Windows allows (2GB for the kernel, 2GB for apps). That is... the normal Windows. With PAE it can access more. You have found 1200MB to be the limit because of the other things the JVM needs RAM for (loading the classes for example, that Perm space).

In a 64 bit program, the limitation is much much higher: 2^64 = huge number. On MSDN (http://msdn.microsoft.com/en-us/library/aa366778%28VS.85%29.aspx) MS says that the limit is 8TB.

So...that should be enough or everybody, shouldn't it?

Anyhow, the only case you would have to care about that, if your Java program is using JNI. Then you would have to make libraries available for whatever OS/Architecture you intend to support:
A 64 bit dll for 64 bit windows with a 64 bit JVM
A 32 bit dll for 64/32 bit windows with a 32 bit JVM
A 64 bit .so for 64 bit Linux with a 64 bit JVM
...
and so on and so forth for every Unix and JVM under the sun.

But the Java program would not change. the JNI call will always be the same, just the actual native code may be different.

necrobobsledder
Mar 21, 2005
Lay down your soul to the gods rock 'n roll
Nap Ghost
Due to the limitations of hardware (and software), at present there is no (consumer-available) system that can address the full 64 bit range of memory on our current 4KB page standard x86 addressing model. What we do have, however, are systems that can do 40, 48, and etc. bits of physical memory and so we can have enough memory addressable for the foreseeable future while hardware can catch up to the needs of such software. In some sense, we already had 40-bit memory systems a long time ago due to how addresses work in modern "32-bit" x86 systems, but the 8 bits weren't usable for anything that JVM designers would want to expand.

The reason you would care about a 64-bit JVM is if your Java application needs more than 3GB or so total of memory and you cannot distribute the data across multiple nodes in a distributed execution model. Also, due to fragmentation issues, you may require a 64-bit JVM even if you're not that close to 3GB of memory required.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
I'm having an issue using the POI HSSF library to write to an Excel file. There are 10,000+ rows that I am trying to output, each row is identified with a username. It appears one of the usernames is not playing nice with the HSSF stuff, and I get a "This spreadsheet has unreadable content..." message. I've been trying to narrow down which username is causing problems and I'm not getting anywhere. What is strange is I found that replacing every character:

code:
username = username.replaceAll(".", "A");
will cause the spreadsheet to output and open fine, with no error message, but if I do something like:

code:
username = username.replaceAll("[^A-Za-z0-9]", "A");
it will still produce the same error message.

Any suggestions?

HFX
Nov 29, 2004

rhag posted:

A 32 bit program (any 32 bit program) cannot access more than 4GB of ram (architecture limitations). In real-life 2GB is the max that normal Windows allows (2GB for the kernel, 2GB for apps). That is... the normal Windows. With PAE it can access more. You have found 1200MB to be the limit because of the other things the JVM needs RAM for (loading the classes for example, that Perm space).

So...that should be enough or everybody, shouldn't it?

Unless your customer keeps collecting more data per interval of processing units. I did have to have a session with my customer explaining how garbage collectors work and how you can only expect to have 1/2 the total ram on a given os available.

Just for the record that was on RHEL4 32bit.

Overall, I hated that code base. Java 1.5 was out for several months before this project began. Lead developer insisted upon 1.4. Program while having a huge number of threads was only single processor in effect as the threads deadlocked on each other except for I/O and the database side I wrote. The center core of the program had an algorithm that at best was 600n^3. It featured very many premature optimizations which would bite me later in the rear end when he left and I took over. Of course what do you do? It's only been in the last several months I've gotten my coworkers to start using at least Java 1.5 features.

RussianManiac
Dec 27, 2005

by Ozmaugh

HFX posted:

Unless your customer keeps collecting more data per interval of processing units. I did have to have a session with my customer explaining how garbage collectors work and how you can only expect to have 1/2 the total ram on a given os available.

Just for the record that was on RHEL4 32bit.

Overall, I hated that code base. Java 1.5 was out for several months before this project began. Lead developer insisted upon 1.4. Program while having a huge number of threads was only single processor in effect as the threads deadlocked on each other except for I/O and the database side I wrote. The center core of the program had an algorithm that at best was 600n^3. It featured very many premature optimizations which would bite me later in the rear end when he left and I took over. Of course what do you do? It's only been in the last several months I've gotten my coworkers to start using at least Java 1.5 features.

do you guys use any testing framework, or any QA at all?

HFX
Nov 29, 2004

RussianManiac posted:

do you guys use any testing framework, or any QA at all?

Define testing and QA. Hell define specs. We sort of do QA at the top most user level at best. Now you know why I picked up a smoking habit while working there. The clients I deal with refuse to have anyone else from the company work on their code or their in field systems. There is a reason all have offered me a job if I want to change companies at some point.

The sad part is much of my industry follows the same practices. You wonder why a certain consumer device in your home feels and works like utter poo poo.

The bad thing is, I only consider myself a better then average programmer who just happened to have a couple good professors in college who opened his mind to good design, good languages, and an understanding of the basics. Technically, I am the only person with a computer science degree and almost had a masters so I suppose that is part of it. There is one manager who came in last year who shares my philosophy but so far his clients do not want to pay the cost of a US programmer.

HFX fucked around with this message at 06:16 on Sep 22, 2009

Max Facetime
Apr 18, 2009

fletcher posted:

Any suggestions?

Maybe it doesn't like a username that has a number in a wrong place.

Generally, to narrow it down you'll want to do a binary search on your rows, i.e add rows 1-5000 or rows 5001-10000, then depending on which one is invalid split that range in two until you have the offending row.

Painless
Jan 9, 2005

Turn ons: frogs, small mammals, piles of compost
Turn offs: large birds, pitchforks
See you at the beach!

Anunnaki posted:

My CS teacher's been telling me it's a "hybrid." v:shobon:v

The distinction between "compiled languages" and "interpreted languages" is almost completely arbitrary fyi
There are Java interpreters (and they're still used in embedded systems), there are Java JIT compilers and there's at least one Java compiler (gcj) although I'm not sure how well it works
For some languages, one option of executing just makes more sense than others, for example interpreting C++ directly would be really slow and hard

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip
gcj can only be said to 'work' in the loosest sense of the word

Tulenian
Sep 15, 2007

Getting my 'burg on.

necrobobsledder posted:

The reason you would care about a 64-bit JVM is if your Java application needs more than 3GB or so total of memory and you cannot distribute the data across multiple nodes in a distributed execution model. Also, due to fragmentation issues, you may require a 64-bit JVM even if you're not that close to 3GB of memory required.

Does the Sun 32-bit hotspot JVM actually support 3GB of memory? I've never been able to get it to allocate more than ~1.5GB. Any more and it just refuses to start.

necrobobsledder
Mar 21, 2005
Lay down your soul to the gods rock 'n roll
Nap Ghost

Tulenian posted:

Does the Sun 32-bit hotspot JVM actually support 3GB of memory? I've never been able to get it to allocate more than ~1.5GB. Any more and it just refuses to start.
Contiguous memory is the issue - I've seen cases where it won't start with more than 768MB for -Xmx. In a lot of configurations, you won't be able to allocate more than 1.5GB of memory in a single segment for the application. If you need more than 1 GB of memory for your Java app, you should be looking at 64-bit JVMs and OSes as part of a scaling strategy.

Bing the Noize
Dec 21, 2008

by The Finn
How does one convert types in Java? I'm new to Java, but I know my way around C pretty well. I'm trying to convert a variable of type AnyType to String, and another variable of type AnyType to float. How do I go about doing this?

Shavnir
Apr 5, 2005

A MAN'S DREAM CAN NEVER DIE

ACID POLICE posted:

How does one convert types in Java? I'm new to Java, but I know my way around C pretty well. I'm trying to convert a variable of type AnyType to String, and another variable of type AnyType to float. How do I go about doing this?

You cast it same as you normally would. Do know that it can throw exceptions if you don't check first.

code:
AnyType x=new String();
if(x instanceof String){
   ((String)(x)).stringMethodGoesHere();
}
Edit : Do note that since float is a primitive you won't be able to cast objects as it, however you can cast them to Float.

HFX
Nov 29, 2004

ACID POLICE posted:

How does one convert types in Java? I'm new to Java, but I know my way around C pretty well. I'm trying to convert a variable of type AnyType to String, and another variable of type AnyType to float. How do I go about doing this?

You can always cast to a more general types. That is to say, you can always cast to a super type of your current type.

Casting to a more specific types (supertype to subtype) will result in a runtime error if the object is not that specific type.

Specifically, in your case, you cannot cast AnyType to String unless Anytype inherits from string through extension since String in a class and not an interface. The fact you say you are trying to convert AnyType to a float also means that it cannot have inherited from string since float is a primative and the class Float is not a subclass of String.

Shavnir posted:

Edit : Do note that since float is a primitive you won't be able to cast objects as it, however you can cast them to Float.

What Shavnir is talking about here is the autoboxing feature of the Java compiler. The reason this works is that the compiler will autobox a float into a Float. What is really going on behind the scenes is that the compiler is doing new Float(float) for you. It will autounbox for you also by calling the Float.floatvalue() function for you.

Since you are coming from C and not C++ this behavior with casting may seem strange to you. However, it is because Java is a strongly typed language while C is weakly typed language. The language is trying to protect you, and honestly outside of a few very specific applications it is a good thing.

HFX fucked around with this message at 00:43 on Sep 23, 2009

Lysidas
Jul 26, 2002

John Diefenbaker is a madman who thinks he's John Diefenbaker.
Pillbug

Shavnir posted:

You cast it same as you normally would. Do know that it can throw exceptions if you don't check first.

code:
AnyType x=new String();
if(x instanceof String){
   ((String)(x)).stringMethodGoesHere();
}
Edit : Do note that since float is a primitive you won't be able to cast objects as it, however you can cast them to Float.
What a strange block of code. Since String is a final class, you generally cannot have a user-defined type that subclasses it. I can only see this code working in two situations:
  • You actually have a String, but your variable is declared as one of the interfaces that String implements (Serializable, CharSequence, Comparable<String>).
  • You're dealing with the Java 1.4 (or previous) Collections API, and you have to cast your Object into the type that you actually want.

I doubt that ACID POLICE is dealing with something that's already a String; the general solution to his problem is to call the .toString() method on your object (casting is not conversion).

If AnyType does not override toString(), then you'll get the default method defined in Object -- you get the class of the object and its hash code. If you can't implement toString() yourself, you should probably do something like
code:
String.format("%s or %d or whatever", anyTypeObject.getPieceOfInformationYouWant(),
    anyTypeObject.getSecondFieldYouWant());
to convert the object's fields to the format that you want. See the documentation of String.format() for details. The format string above is similar to C printf.

As for converting to a Float, you can call Float.parseFloat(String string) or 'new Float(value)'. It's hard to say which is better without any details about the type you're converting from.

Vandorin
Mar 1, 2007

by Fistgrrl
I'm trying to write a method that sets a third of my pictures pixels to a certain color, but just can't figure it out. here is my code so far:

code:
  public void firstThird()
     {
        // Get the pixels
       Pixel[] thePixels = this.getPixels();
       int count = 0;
         
       // Get the current Pixel.
       Pixel thisPixel = thePixels[count];
       
       while (count < 213 )
       {
           
          //Get the pixel and set it's color.  
          int redAmount = thisPixel.getRed();
          thisPixel.setRed(244);
           
           // Go on to next pixel
           count = count + 1;
        }
           
        
        
      }

Anybody know what I'm doing wrong? When I run it, all I get is a blank picture.

RussianManiac
Dec 27, 2005

by Ozmaugh

Vandorin posted:

I'm trying to write a method that sets a third of my pictures pixels to a certain color, but just can't figure it out. here is my code so far:

code:
  public void firstThird()
     {
        // Get the pixels
       Pixel[] thePixels = this.getPixels();
       int count = 0;
         
       // Get the current Pixel.
       Pixel thisPixel = thePixels[count];
       
       while (count < 213 )
       {
           
          //Get the pixel and set it's color.  
          int redAmount = thisPixel.getRed();
          thisPixel.setRed(244);
           
           // Go on to next pixel
           count = count + 1;
        }
           
        
        
      }

Anybody know what I'm doing wrong? When I run it, all I get is a blank picture.

I am not sure what class you are using that in, but your loop is wrong.

code:
  public void firstThird()
     {
        // Get the pixels
       Pixel[] thePixels = this.getPixels();
       int s = thePixels.length;
       int count = 0;
         
       for( int i = 0; i < s / 3; ++i )
      {
           thePixels[i].setRed(244);
}
           
        
        
      }
thisPixel doesn't change in your loop at all, you only change one pixel.

Vandorin
Mar 1, 2007

by Fistgrrl

RussianManiac posted:

I am not sure what class you are using that in, but your loop is wrong.

code:
  public void firstThird()
     {
        // Get the pixels
       Pixel[] thePixels = this.getPixels();
       int s = thePixels.length;
       int count = 0;
         
       for( int i = 0; i < s / 3; ++i )
      {
           thePixels[i].setRed(244);
}
           
        
        
      }
thisPixel doesn't change in your loop at all, you only change one pixel.

Ah thanks. Now I've just got to figure out how to add in color for the other 2 thirds. Would I just mess with the for loop, making i > s and i == s?

Volguus
Mar 3, 2009

Vandorin posted:

Ah thanks. Now I've just got to figure out how to add in color for the other 2 thirds. Would I just mess with the for loop, making i > s and i == s?

Depends what do you want out of the image.
One way would be to go from 0 to s, inside the for do int m=i%3;. Now, m can have 3 possible values: 0,1,2. If m=0 color it one way, if it equals 1 another way and if it equals 2 another way.

Another alternative would be to go from 0 to s, but check the values that i has:
if(i<s/3) do this,
if(i>=s/3 && i<2*s/3) do that,
if(i>2*s/3) do another thing.

Or 3 for loops.
The possibilities are endless :).

Vandorin
Mar 1, 2007

by Fistgrrl

rhag posted:

Depends what do you want out of the image.
One way would be to go from 0 to s, inside the for do int m=i%3;. Now, m can have 3 possible values: 0,1,2. If m=0 color it one way, if it equals 1 another way and if it equals 2 another way.

Another alternative would be to go from 0 to s, but check the values that i has:
if(i<s/3) do this,
if(i>=s/3 && i<2*s/3) do that,
if(i>2*s/3) do another thing.

Or 3 for loops.
The possibilities are endless :).

I tried the alternative code you mentioned (because I understand it better) but for some reason only the first third is getting filled. Here's what I've typed so far:
code:
  public void secondThird()
     {
        // Get the pixels
       Pixel[] thePixels = this.getPixels();
       int len = thePixels.length;
       int count = 0;
       
       // For loop to make sure it's inbetween the first third, and the last third.
         
       for( int i = 0; i >= len / 3 && i < 2 * len / 3; i++ )
      {
           thePixels[i].setBlue(2);
      }
   
    }
I made sure to put the secondThird method in my main program, so I can't figure out why it's not working. :(

OddObserver
Apr 3, 2009
0 is not >= len / 3, so that loop never runs (well, unless len == 0... too lazy to think about the other conjunct)

Vandorin
Mar 1, 2007

by Fistgrrl

OddObserver posted:

0 is not >= len / 3, so that loop never runs (well, unless len == 0... too lazy to think about the other conjunct)

edit: Just tried switching i to a number like 250, and it still doesn't run.

Shavnir
Apr 5, 2005

A MAN'S DREAM CAN NEVER DIE

Vandorin posted:

edit: Just tried switching i to a number like 250, and it still doesn't run.

What rhag was suggesting was logic inside your old for loop. Right now you're setting up a for loop that frankly makes no sense.

Set up the for loop to iterate through all the pixels as before and try putting rhag's if block inside the loop, if that makes sense.

EDIT : vvv Ah now I see what you were going for. I figured if he didn't even know how to throw together a for loop it wasn't worth trying to do a more complex for.

Shavnir fucked around with this message at 14:29 on Sep 23, 2009

Volguus
Mar 3, 2009

Vandorin posted:

I tried the alternative code you mentioned (because I understand it better) but for some reason only the first third is getting filled. Here's what I've typed so far:
code:
  public void secondThird()
     {
        // Get the pixels
       Pixel[] thePixels = this.getPixels();
       int len = thePixels.length;
       int count = 0;
       
       // For loop to make sure it's inbetween the first third, and the last third.
         
       for( int i = 0; i >= len / 3 && i < 2 * len / 3; i++ )
      {
           thePixels[i].setBlue(2);
      }
   
    }
I made sure to put the secondThird method in my main program, so I can't figure out why it's not working. :(


Try this:
code:
//This will set all the pixels in the middle (between first third and the last third)
  for( int i = (len / 3);  i < (2 * len / 3); i++ )
      {
           thePixels[i].setBlue(2);
      }
The brackets are not really needed, but it makes the code clearer a tiny little bit.
So, for let's say: len=6.

i starts at 2;
runs until it gets to 2*6/3=4;
That means, pixels 2 and 3 (in the zero based array) will get their blue color set to 2.
To get the last third (4 and 5 pixels) you do this:
code:
  for( int i = (2 * len / 3);  i < len; i++ )
      {
           thePixels[i].setBlue(2);
      }
The reason your code didn't work is this:
Lets say len =6;
i starts at 0;

The for loop as you wrote it runs until (0 >= 6/ 3 && 0 < 2 * 6/ 3)==false

as you can see 0>=2 is false ,thus the entire expression is false .

I assume you are a student and this is some school project. I hope that you're not a developer that somebody pays to write code. right?

Vandorin
Mar 1, 2007

by Fistgrrl

rhag posted:

Try this:
code:
//This will set all the pixels in the middle (between first third and the last third)
  for( int i = (len / 3);  i < (2 * len / 3); i++ )
      {
           thePixels[i].setBlue(2);
      }
The brackets are not really needed, but it makes the code clearer a tiny little bit.
So, for let's say: len=6.

i starts at 2;
runs until it gets to 2*6/3=4;
That means, pixels 2 and 3 (in the zero based array) will get their blue color set to 2.
To get the last third (4 and 5 pixels) you do this:
code:
  for( int i = (2 * len / 3);  i < len; i++ )
      {
           thePixels[i].setBlue(2);
      }
The reason your code didn't work is this:
Lets say len =6;
i starts at 0;

The for loop as you wrote it runs until (0 >= 6/ 3 && 0 < 2 * 6/ 3)==false

as you can see 0>=2 is false ,thus the entire expression is false .

I assume you are a student and this is some school project. I hope that you're not a developer that somebody pays to write code. right?

Wow, thanks for the detailed explanation :D . And I'm a student, taking my first java course.

UnNethertrash
Jun 14, 2007
The Trashman Cometh
I have what's probably a retardedly simply question about moving objects using the acm.graphics import.

All I want is an object to follow the mouse within the borders of the program (actually it only needs to move in the x-direction).

So, I can click on an object and drag it using:

code:
	private GObject gobj;
	private double lastX;
	private double lastY;

        /*selects object when mouse button is pressed*/
	public void mousePressed(MouseEvent e) {
		lastX = e.getX();
		lastY = e.getY();
		gobj = getElementAt(lastX, lastY);
	}
	
        /*moves object when mouse dragged*/
	public void mouseDragged(MouseEvent e) {
		if (gobj != null) {
			gobj.move(e.getX() - lastX, 0);
                        lastX = e.getX();
		}
	}
The above code lets me click on any object and drag it around, including the one thing I actually want to move (which is a rectangle named paddle).

Since just following the mouse doesn't require association with a specific object, the mousePressed method is unnecessary, leaving me with:

code:
	private GObject paddle;
	private double lastX;
	private double lastY;

        /*just a method to draw a rectangle called paddle*/
        public void drawPaddle(){...}

	public void mouseMoved(MouseEvent e) {
		lastX = e.getX();
		paddle.move(e.getX() - lastX, 0);
	}
However, this gives me a null pointer exception whenever I move the mouse. Hopefully that's all the relevant information. If it matters, I am using eclipse.

Thanks in advance.

Max Facetime
Apr 18, 2009

Knowing the line that throws the NPE would help, but you aren't checking against null in your second example, so I'd guess that's the culprit.

E: The line numbers are printed in the exception stacktrace.

Max Facetime fucked around with this message at 08:26 on Sep 24, 2009

UnNethertrash
Jun 14, 2007
The Trashman Cometh

Eclipse Console posted:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Breakout.mouseMoved(Breakout.java:78)
at java.awt.AWTEventMulticaster.mouseMoved(AWTEventMulticaster.java:272)
at java.awt.Component.processMouseMotionEvent(Component.java:5628)
at java.awt.Component.processEvent(Component.java:5352)
at java.awt.Container.processEvent(Container.java:2010)
at java.awt.Component.dispatchEventImpl(Component.java:4050)
at java.awt.Container.dispatchEventImpl(Container.java:2068)
at java.awt.Component.dispatchEvent(Component.java:3885)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4256)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3949)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3866)
at java.awt.Container.dispatchEventImpl(Container.java:2054)
at java.awt.Window.dispatchEventImpl(Window.java:1791)
at java.awt.Component.dispatchEvent(Component.java:3885)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:176)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

That's exactly what the eclipse console shows. If the numbers on the right are the line number, then the culprit is:

code:
paddle.move(e.getX() - lastX, 0);
If by "checking against null" you mean in the first example:

code:
		if (gobj != null) {
			gobj.move(e.getX() - lastX, 0);
                        lastX = e.getX();
That's just if you click on an area of the screen that doesn't contain an object then nothing happens (gobj is just whatever you click on). I don't know what the analogy would be for something that is always supposed to follow the mouse because paddle is the object that the move() method is called for, and paddle is always there.

I'm sure we can figure this out, there has got to be an easy solution.

czg
Dec 17, 2005
hi

UnNethertrash posted:

That's exactly what the eclipse console shows. If the numbers on the right are the line number, then the culprit is:

code:
public void mouseMoved(MouseEvent e) {
		lastX = e.getX();
		paddle.move(e.getX() - lastX, 0);
	}

Two things are dereferenced and so could be null here, paddle and e.
It is extremely unlikely that the event dispatch thread will send you a null mouse event, and also you dereferenced it on the line before the exception is thrown, so I'd say you haven't created/set up your paddle yet.
Add a if(paddle == null) return; first or something.

Also it looks like you've swapped the two lines in that method. I'm pretty sure you want to keep lastX for the next frame, and so you should set it after you've moved the paddle. As it is now you're doing paddle.move(e.getX() - e.getX(), 0) which won't move it at all I guess.

UnNethertrash
Jun 14, 2007
The Trashman Cometh
Ok, I changed it to this:

code:
	private GObject paddle;

	public void run() {	
                [...]
		draw_paddle();
		addMouseListeners();

	}

	public void mouseMoved(MouseEvent e) {
		if (paddle != null) { 
			lastX = paddle.getX();
			paddle.move(e.getX() - lastX, 0);
		}
It's not throwing any exceptions, but it's also not moving.

Here is my draw_paddle();
code:
	public void draw_paddle() {
		GRect paddle = new GRect(PADDLE_X, PADDLE_Y, PADDLE_WIDTH, PADDLE_HEIGHT);
		paddle.setFilled(true);
		paddle.setFillColor(Color.BLACK);
		add( paddle );
	}
So I declare a private class variable that is a GObject named paddle. Then later I just create a GRect named paddle. I have been assuming that the draw_paddle method would just use the class variable, was this perhaps incorrect, giving me an unused GObject named paddle and a separate GRect also named paddle?

I'll play around with it, and see what happens.

UnNethertrash
Jun 14, 2007
The Trashman Cometh
Holy gently caress! It worked.


For reference the new version is:
code:
	private GRect paddle;

	public void run() {	
                [...]
		paddle = draw_paddle();
		add( paddle );
		addMouseListeners();
        }
	public GRect draw_paddle() {
		GRect paddle = new GRect(PADDLE_X, PADDLE_Y, PADDLE_WIDTH, PADDLE_HEIGHT);
		paddle.setFilled(true);
		paddle.setFillColor(Color.BLACK);
		return paddle;
	}
Thanks, it was totally "I'd say you haven't created/set up your paddle yet" that got me to the answer.

czg
Dec 17, 2005
hi
^^^^ Oh OK then.

Gah, nevermind this, I am dumb and also fat and I cannot read.

UnNethertrash posted:

Ok, I changed it to this:

code:
	public void mouseMoved(MouseEvent e) {
		if (paddle != null) { 
			lastX = paddle.getX();
			paddle.move(e.getX() - lastX, 0);
		}
It's not throwing any exceptions, but it's also not moving.

Since you're setting lastX before you're reading it, and e.getX() doesn't change between those two lines, what you're doing boils down to
code:
paddle.move(e.getX() - e.getX(), 0);
which again boils down to
code:
paddle.move(0, 0);
Put the lastX assignment below the paddle movement and it probably behaves as expected.

czg fucked around with this message at 21:46 on Sep 24, 2009

Alvie
May 22, 2008

Quick question. I'm new to Java and am having a bit of trouble with strings. I want to initialize an array of strings with values defined by int variables. The part I'm really having trouble with is that strings are constant.

Basically, what I'm working on is a sort of basic appointment-book type application. The appointmentbook class accepts two variables for a start time and an end time in military format (800, 1500, etc.). I'm trying to find a way to create a String array holding each time at one-hour intervals between the start and end time.

I can't just say like

code:
String[] times={"1100", "1200", "1300"};
because I need each value to be based on input which may change. Is there some other way to assign a value to a string array that I'm missing? There's probably some easy way that I just haven't figured out, but I'm totally stumped at this point.

EDIT: For now I'm just gonna say gently caress it and initialize an array with all 24 hours and just work from that because I'm not even sure if this is possible. I'm gonna leave my question up, though, because I'm curious.

Alvie fucked around with this message at 02:43 on Sep 25, 2009

Mr.Radar
Nov 5, 2005

You guys aren't going to believe this, but that guy is our games teacher.
Why do they need to be strings?

Alvie
May 22, 2008

Mr.Radar posted:

Why do they need to be strings?
I need to output them to a text field. I imagine there's a way to do it with other types, but I've managed it by just initializing the array manually {"100", "200"...}. I guess my real question is is it possible to set an array of strings any way other than this?

Mr.Radar
Nov 5, 2005

You guys aren't going to believe this, but that guy is our games teacher.
Of course:
code:
String[] strings = new String[5];

for(int i = 0; i < string.length; i++) {
    strings[i] = Integer.toString(i);
}

Alvie
May 22, 2008

Mr.Radar posted:

Of course:
code:
String[] strings = new String[5];

for(int i = 0; i < string.length; i++) {
    strings[i] = Integer.toString(i);
}

Thanks, that's awesome. I think my biggest problem is that the rest of my code is all over the place so it's pretty tough to find out what specifically is wrong. When I read that strings are constants I assumed you couldn't change them after they're declared, but I guess when you use strings[i]="whatever", you're actually allocating a new string and assigning the pointer of the original one to the new one?

Adbot
ADBOT LOVES YOU

Mr.Radar
Nov 5, 2005

You guys aren't going to believe this, but that guy is our games teacher.
Strings are immutable (which means you can't change the value of a String instance), but references to strings (remember in Java that all object variables are really references to object instances or null) are definitely mutable.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply