|
God, that's so cool! And it just goes and does what you want it to... I get it - in one thread Swing does what it's supposed to do - hide the list on click - and in another thread my code runs. Well, there goes the mystery.
|
# ? Sep 16, 2013 19:16 |
|
|
# ? Jun 11, 2024 15:02 |
|
supermikhail posted:God, that's so cool! And it just goes and does what you want it to... Actually, the code in the Runnable still runs in the Event Dispatch Thread, but just after every other event has been completed (in this case, the item selection event). Be careful to not abuse invokeLater, as weird stuff can happen.
|
# ? Sep 17, 2013 00:31 |
|
rhag posted:You don't have those guarantees on linux either. I personally have my jdks installed in /home/<user>/java/jdk-version . Now, I do set PATH, JAVA_HOME, etc. properly in my .bashrc to not have to deal with those issues, but I don't have to. rrrrrrrrrrrt posted:If your assumption on Linux is that java will be on the PATH, why can't you make the same assumption for Windows? On linux, the user has probably installed it through the package manager, which I generally trust to set up paths correctly; if they're knowledgeable enough to install it by hand, I assume they're also knowledgeable enough to know how to use their hand-installed java to run a jar. rhag posted:If you want to keep things simple, and not go the jsmooth/launch4j way, you can make a bat file, in which you can check for the existence of the required variables, or the java executable. If that's not found, just open the browser and go to java.com . Then the users just have to install that. If they install it from the installer, it will be in the PATH and everything will be fine. Tesseraction posted:Looking through all my Windows environmental variables (User and System) I can't see Java on it anywhere; Java is installed, though, and java -jar works fine. I admit it's hardly a solution, but have you encountered a system where Java is installed but a call to java doesn't work? This is exactly what I tried last time, and is the reason I'm in here using phrases like "waking nightmare" and trying desperately to avoid having to do it again. I started out assuming that if java was installed, it was in %PATH%. Then I got reports of systems where that didn't work, but where at least one of the %JAVA_*% environment variables was set properly. So I added checks for those, too. Then I got reports of systems where those weren't set either, but you could locate it through the registry. All of these systems, I note, had Java 1.5 or later installed via one an official installer. The end result was 150 lines of batch file fuckery and there were still two or three users who had the JRE installed - as in it showed up in add/remove programs, the files were present in the filesystem, and double-clicking on jar files would run them - but for whom the batch file did not work. I mean, I guess they could have copied java from another system and set up the file associations manually and just lied about it, but why? Using jsmooth/launch4j, if they work as advertised, is the simple way.
|
# ? Sep 17, 2013 00:47 |
|
Gravity Pike posted:I hate to tell you to RTFM, but Java has really good documentation. You can basically google any classname, and among the first few results will be a detailed description of the class, it's complete hierarchy, and a complete description of all of it's methods. Alright, always found that stuff overwhelming, but it's time to sit down and learn it. Anyways, I did manage to get the program working and it is using multiple threads, but it's still too slow. I think it just comes down to the size of the dataset making this method unfeasible. Still, I learned a lot programming it. Thanks again for the help folks.
|
# ? Sep 17, 2013 12:24 |
|
Not specifically a Java question, but it's the language I'm working on at the moment, so I put it here. I've been reading up on OOP. I think I get the idea behind it, so I wrote something up as an example of classes and objects. I made a list of elements you'd find in a basic RPG. Each line contains the type of class or object, the name of the class or object, and certain parameters of the class or object (in brackets). e.g.: The superclass Items has one parameter: [amount]. The Weapons subclass of Items, however, has two parameters: [amount, power]. It inherits [amount] from Items (all Items have an amount), and innately has [power] (all Weapons have damage dealing power, but not all Items do). Similarly, the Potions subclass only has the parameter [amount], inherited from Items. It has no inherent parameters. Certain Objects of class Potions, however, have the parameters [amount, bar, points], where [points] is how many points of which [bar] (HP or MP) is recovered upon using the item. Both [bar] and [points] are inherent parameters of the Objects Health and Mana. On the other hand, the Object Water has no parameters either, as it is neither associated with HP or MP, nor restores any points. It has only an [amount] of Water potions held. Here's the list of stuff: code:
vvvv Thanks, I changed it around. I don't think I've gotten to interfaces yet, but I'll get to it. Pollyanna fucked around with this message at 21:23 on Sep 17, 2013 |
# ? Sep 17, 2013 21:12 |
|
Almost. The items in your third level are also classes, don't forget. An object would be an instance of any of those classes, so Knife is a class, but if you give a knife to the player you would make an instance of the Knife class which would have values for its member variables (and the member variables of its superclasses.) This is where it gets tricky, since I don't think Classes are considered objects in OOP, yet most programming languages will have some method of obtaining an object representing a class in the system (and even modifying that class.) The next concept you will likely read about will be interfaces and, if the text is Java oriented, abstract classes, which you may find better fit your modeling of an RPG.
|
# ? Sep 17, 2013 21:19 |
|
Superclass and subclass are relationships that one class has to another, rather than a specific location in a class hierarchy. In your example as written (where 'Baker' is a class), Baker is a subclass of Villagers but then Villagers is in turn a subclass of Mobiles. Reciprocally, Mobiles is a superclass of Villagers and Villagers is in turn a superclass of Baker. A note on terminology: usually when we discuss objects, the word 'member' is used to describe the data that belongs to that object and is kept around as long as that object exists. 'Parameter' is more typically used to describe data that is passed into a method (aka 'member function') and is kept around only for one call on that method. It can be confusing because members usually begin their lives as parameters passed into a constructor or method which then are saved by the object in members. Also, I am inferring from your use of plural class names such as 'Mobiles' and 'Villagers' that singular things such as 'Baker' and 'Blacksmith' should really be objects (instances of 'Villagers' in this case) rather than classes themselves. If this is incorrect, consider renaming your various Villager subclasses to be plural in order to be consistent with your other naming conventions. So then you would have a class hierarchy something like: class Mobiles -> class Villagers -> class Bakers -> object Alice, object Bob, object Cindy (all instances of Bakers)
|
# ? Sep 18, 2013 16:42 |
|
Is plural class names a convention that anyone actually follows? I mean, it's not Lists<Dog> bestDogsList = new ArrayLists<Dog>();. The only place you see plural class names are the static "Utility" classes in Commons/Guava, where they really want to put a bunch of static methods in the parent class. (ie Lists.newArrayListWithExepectedSize(10);)
|
# ? Sep 18, 2013 18:39 |
|
Plural class names are a standard for precisely those static utility classes. It's definitely inappropriate to pluralize a class that's designed to be instantiated; "bestDogsList IS-A ArrayLists" wouldn't be right.
|
# ? Sep 18, 2013 19:09 |
|
Gravity Pike posted:Is plural class names a convention that anyone actually follows? I was just speaking to the convention in the example along with a conjecture about Pollyanna's mental model of classes and objects. I generally don't use plural class names, and would more likely use plurals for collections, but even then I might go with "villagersList" over "villagers" if the container type wasn't likely to change.
|
# ? Sep 18, 2013 22:12 |
|
Pollyanna posted:The superclass Items has one parameter: [amount]. The Weapons subclass of Items, however, has two parameters: [amount, power]. It inherits [amount] from Items (all Items have an amount), and innately has [power] (all Weapons have damage dealing power, but not all Items do). A rule of thumb is that class inheritance is for sharing common implementations of functionality, but as far as behavior, you should take a good look at interfaces once your inheritance topology starts feeling limited and strange. A subclass can only extend one parent class, but can implement several interfaces and thus "be" many things at once. For example a Cloak and a Blacksmith can both implement Burnable and needn't have anything else in common rather than having implemented burn() or whatever the Burnable interface requires, at some point of their respective class hierarchy. Definitely read up and mess around with interfaces, they're more useful than they appear. pigdog fucked around with this message at 15:34 on Sep 19, 2013 |
# ? Sep 19, 2013 15:32 |
|
I've got writer's block. This application that I want to store preferences for, I just realized that I have to start with something - I have to have a default path where I would store all the non-default paths. I could slap it right in the home folder like the amateur I am, but maybe there's some trick that adult apps use? I'm also worried that if I slap the file there I might overwrite another one that accidentally has the same name but doesn't belong to my program. Brethren (and sistren), what say ye?
|
# ? Sep 20, 2013 15:55 |
|
supermikhail posted:I've got writer's block. How about you have code in your app that on startup looks in current folder for the file, if it doesnt exist create it As an example look at minecraft server, when you first run it will check for the ops.txt file and create it if it is not there.
|
# ? Sep 20, 2013 16:08 |
|
So I'm trying to figure out how to put two conditions inside of an if statement, and I can't phrase the question well enough to get an answer from google.Java code:
|
# ? Sep 20, 2013 16:24 |
|
TheresaJayne posted:How about you have code in your app that on startup looks in current folder for the file, The perfectionist says that the executable file might be in the home folder, in which case the default paths file will be there, too, and we're back at the start (could overwrite something important)... Dammit. Although, I'll gladly look up to Minecraft for good coding practices (after all, it brought Java into mainstream gaming ).
|
# ? Sep 20, 2013 16:36 |
|
KildarX posted:So I'm trying to figure out how to put two conditions inside of an if statement, and I can't phrase the question well enough to get an answer from google. Googling 'java or operator' seems to be pretty easy. You're looking for || You also have some issues with your usage of =. If you're testing whether something is equivalent, you use ==. Also, single characters must be enclosed in single quotation marks. This is what you want: code:
|
# ? Sep 20, 2013 16:52 |
|
Kilson posted:Googling 'java or operator' seems to be pretty easy. You're looking for || Still learning the language so not really sure what to call things, thanks a lot.
|
# ? Sep 20, 2013 16:56 |
|
supermikhail posted:The perfectionist says that the executable file might be in the home folder, in which case the default paths file will be there, too, and we're back at the start (could overwrite something important)... Dammit. Although, I'll gladly look up to Minecraft for good coding practices (after all, it brought Java into mainstream gaming ). Pointless snark aside, if you're that worried about it, have backup names and check if a file exists by the first name and open it, and if it doesn't look like your file then choose a different name. "Adult" apps probably use either the Preferences API or the Properties API, and the former seems like overkill for storing one list of paths, while the latter is usually placed relative to the working directory.
|
# ? Sep 20, 2013 16:56 |
|
KildarX posted:Still learning the language so not really sure what to call things, thanks a lot. Here's a list of all the operators in Java: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html You probably won't use some of these unless you're doing a lot of operations on raw bits, but it's good to have if you every come across a use of >>> and want to know what it does.
|
# ? Sep 20, 2013 16:58 |
|
YES! So I think I've finally pinned down the directory that the running jar is in. It involves StackOverflow's notes in the margins and code:
|
# ? Sep 20, 2013 18:47 |
|
Anyone has any book recommendation for Aspect Oriented Programming in java. I started slowly incorporating it into some of our Spring applications but I could definitely used a good resource to pass around our development team.
|
# ? Sep 20, 2013 19:02 |
|
I'm working on an assignment for a Java course where I make a program that reads in a word search puzzle from a text file (2 numbers for the number of rows and columns, then all the characters in the text file as an array) along with a list of the words it needs to find, then it checks for the words in all 8 directions and outputs them to a text file along with the coordinates of the first letter of the word. I figured out that I would have to use a method to check in each direction for a word because doing 8 different if statements wouldn't be clean enough, but I'm not sure if how I'm doing the method is right in terms of parameters and what I'm supposed to put into the method to get the word. This is what I have so far, I put it into Pastebin because the amount of parameters in the method break tables when I use [code] for them: http://pastebin.com/E2Yk0A93
|
# ? Sep 22, 2013 15:15 |
|
You're on the right track with creating a method to search for a words, but I think it needs to encapsulate a simpler functionality than to check for all words. I would actually create two methods: one to scan through the puzzle in order until it comes to a letter that is the first letter of a word (or multiple words!) in the list (that hasn't been found yet.) When a matching letter is found, the method can call another method, which takes in a word to look for, coordinates to start with, and a direction to look in, and returns whether the word is found in that direction. That latter method could be called up to 8 times, one for each direction, until one returns true or you've exhausted directions to look, then you move on to the next word that starts with that letter, or if no other words are found, move to the next letter in the puzzle. Just remember to handle what happens if you hit the edge of the puzzle. The only edge case I can think of to handle would be if one of the words in the list was also a substring of another word in the list (think "the" and "there", for instance), which could be taken care of by searching for longer words before shorter ones.
|
# ? Sep 22, 2013 16:44 |
|
Having some trouble utlizing the GSON library on a JSP website. I used this library without any problems in some stand alone java code I was using. I migrated that code to another machine and am invoking it on some jsp pages. Now this is where I am hving trouble, it seems as if the code breaks when I callcode:
JDK 1.7 JRE 1.7 new machine JDK 1.7 JRE 1.6 I am unable to update the JRE on the new machine. The weird thing is no errors are thrown, and if I put a break point on that line its never reached. I have tried with the Gson 2.2 and 2.4 and encounter the same problem. Does anyone have troubleshooting ideas? edit: nevermind. I needed to add the file in the lib directory on the JSP website.... DholmbladRU fucked around with this message at 22:11 on Sep 23, 2013 |
# ? Sep 23, 2013 21:49 |
|
I went back and wrote a method for the word search which uses loops for each direction, but it doesn't work right and I tried debugging with println() to see what was wrong and got nothing out of it. The loops are most likely wrong but I'm not sure which way and I haven't made ones to check diagonals yet. http://pastebin.com/tzUi0ZiE
|
# ? Sep 25, 2013 22:28 |
|
Did you try debugging it with, you know, a debugger?
|
# ? Sep 26, 2013 00:17 |
|
Zeether posted:I went back and wrote a method for the word search which uses loops for each direction, but it doesn't work right and I tried debugging with println() to see what was wrong and got nothing out of it. The loops are most likely wrong but I'm not sure which way and I haven't made ones to check diagonals yet. Did you make a copy/paste error? wordCheck is never getting called. wordToFind is getting set within a loop, but never used. You've also got logic errors, but I kind of feel like you should work those out on your own. Learning a debugger is like a super essential skill, and it's pretty dang easy for straightforward cases like your own. Are you using an IDE, like Eclipse? Read this tutorial.
|
# ? Sep 26, 2013 02:49 |
|
When I annotate a method/field/parameter/type, where are is the annotation's equals and hashcode defined? I'm digging through the OpenJDK sources and I can't figure it out. I'm curious to see the definition of equals() and hashCode(), as we're getting into a slapfight with our JVM vendor on what logical equivalence is. In HotSpot, it seems a normal annotation instance (via .getAnnotations()) can be logically equivalent to a user defined implementation of that annotation interface (AnnotationImpl implements OurAnnotation), but in our production JVM they can't be. This problem, of course, manifested itself as crazy undefined behaviors when working with HashMaps.
|
# ? Sep 26, 2013 06:03 |
|
horse_ebookmarklet posted:When I annotate a method/field/parameter/type, where are is the annotation's equals and hashcode defined? I'm digging through the OpenJDK sources and I can't figure it out. I don't think I understand. So, you have @OurAnnotation, and a class that implements it, AnnotationImpl. All good so far. You can implement in AnnotationImpl hashCode and equals without a problem. However ... what does that have to do with the HashMap? Are you storing annotations in a HashMap? Or ... how does this is supposed to work then?
|
# ? Sep 26, 2013 18:36 |
|
Annotations inherit equals() and hashCode() directly from Object, and AccessibleObject.getAnnotations() is not specified to ensure reference equality with anything else. I'm also not quite clear on what you're trying to do, but I think the right answer is to override those two methods.
|
# ? Sep 26, 2013 20:16 |
|
supermikhail posted:YES! That's standard code for getting the working directory, its very common. It won't do anything dangerous in of itself, but as you said, you're getting files, so there's a chance you try to grab something afterwards and overwrite it or whatever. This is running in a browser, right? If so you're pretty drat safe, unlikely user would have anything to do with those folders. If you wanna be extra careful just give it a fairly unique filename, and you've done your due diligence. Zaphod42 fucked around with this message at 22:23 on Sep 26, 2013 |
# ? Sep 26, 2013 22:20 |
|
rhag posted:I don't think I understand. We are storing them in HashMaps, which is how I discovered the problem, and not really the problem itself. The hashmap is calling equals() which I believe is behaving incorrectly. Amarkov posted:Annotations inherit equals() and hashCode() directly from Object, and AccessibleObject.getAnnotations() is not specified to ensure reference equality with anything else. I'm also not quite clear on what you're trying to do, but I think the right answer is to override those two methods. I'm not attempting to check reference equality, I'm attempting to check logical equality using .equals(). My AnnotationImpl implements equals and hashCode per the description in the Javadoc. The @OurAnnotation that the runtime creates seemingly doesn't implement equals properly. With a known "logically equal" annotation, the runtime equals() returns false. In the AnnotationImpl we created equals() returns true. In the Oracle JVM, both return true as expected. I'm looking for either a specification or a reference implementation in the OpenJDK of equals() and hashCode() for Annotations. Our JDK vendor is saying that its implementation defined, which doesn't seem correct.
|
# ? Sep 27, 2013 03:16 |
|
I really don't understand why you would store an annotation in a hashmap
|
# ? Sep 27, 2013 05:09 |
|
horse_ebookmarklet posted:Our JDK vendor is saying that its implementation defined, which doesn't seem correct. Is your annotation actually an instance of the same annotation type, with all those same properties? Does that javadoc describe the specification, or just one implementation? If you want a hashmap that uses a custom definition of equality ... just use a hashmap with a custom comparator? Jabor fucked around with this message at 05:26 on Sep 27, 2013 |
# ? Sep 27, 2013 05:22 |
|
It is an instance of an implementation of the same type, with the exact same properties. The annotationType()s of both return the same class. I don't know if that javadoc describes the specification or an implementation, which is the point of contention with our vendor. Thats why I'm either looking for a real spec, or something in the OpenJDK reference implementation. Why? At startup we scan a .properties file and create annotations based off the .properties keys. These annotations and their values are inserted into a hashmap. Later, after we create a new instance of something, we call a static method that scans for Fields that have annotations. If that field's annotation is in the hashmap we set that field equal to the value in the map. Its pretty crude, and could be replaced with a real DI framework. So I could change it, but if its technically allowable via specification or reference implementation I'd rather our vendor fix their JVM.
|
# ? Sep 27, 2013 06:05 |
|
horse_ebookmarklet posted:It is an instance of an implementation of the same type, with the exact same properties. The annotationType()s of both return the same class. What ends up being called on my machine is a package-visible class sun.reflect.annotation.AnnotationInvocationHandler and its equalsImpl() method. This is called by a runtime generated proxy-class ($Proxy3) instance which marks one location that was annotated in the source code. There's something weird in the equalsImpl() method. It has a check to see if the other object can be treated as one of us but if not it does a member-wise compare anyway. This smells of something. Then Java 7 API docs say Annotation.equals() posted:Returns true if the specified object represents an annotation that is logically equivalent to this one. In other words, returns true if the specified object is an instance of the same annotation type as this instance Annotation posted:The common interface extended by all annotation types. Note that an interface that manually extends this one does not define an annotation type. So my reading is that if you are mixing your own classes implementing Annotation and generated Annotations then the Oracle implementation is wrong if it says generated Annotations are equal to subclasses of Annotations.
|
# ? Sep 27, 2013 10:29 |
|
horse_ebookmarklet posted:It is an instance of an implementation of the same type, with the exact same properties. The annotationType()s of both return the same class. Just use Guice's @Named("Foo") annotation, and for that matter just use Guice.
|
# ? Sep 27, 2013 14:21 |
|
1337JiveTurkey posted:Just use Guice's @Named("Foo") annotation, and for that matter just use Guice. What actually he needs to use is the MapBinder or Multibinder from Guice. The usage of annotations in this case looks a bit ... weird to say the least, but there could be a solution (kind of): You read properties from a properties file. That is, key,value pairs. You can store that anywhere (in a map, properties file or simply a resource bundle). Now, via reflection you scan for fields, and you look for certain annotations (you have to know what annotations you're looking for). Then you can read the properties of that annotation (value, or whatever other things it may have), and then do whatever you need to do (update the field's value for example). Even in your "not invented here" approach, you still don't need to store annotations in a map. Don't think of annotations as POJOs, think about them as metadata (which they are). An annotation doesn't make any sense nor does it have any reason to exist (logically) if not applied to a class, field or method. But really, just use a CDI library (google's guice is lightweight and easy to setup and work with, you don't need to call the big boys like spring or weld).
|
# ? Sep 27, 2013 14:40 |
|
Max Facetime posted:There's something weird in the equalsImpl() method. It has a check to see if the other object can be treated as one of us but if not it does a member-wise compare anyway. This smells of something.
|
# ? Sep 27, 2013 17:24 |
|
|
# ? Jun 11, 2024 15:02 |
|
Doctor w-rw-rw- posted:Annotations from different classloaders would be different classes wouldn't they? You know, I've never actually thought about how classloading of annotations happens. Intuitively it feels like annotations would have to end up in different classloaders and classes to support things like unloadable plugins, but then on the same token removing a field from some @AnnotationThingy version 2.0 shouldn't suddenly make it compare equal to some unrelated @FooBarAnnotation in version 1.0. The most straightforward thing might be to write some test code to just see what happens.
|
# ? Sep 27, 2013 19:52 |