|
Trying to wrap my head around interfaces and inheritance, My 'game' (it does nothing at the moment, obviously) has two major interfaces, "GameObject" and "GameNode", GameNode implements GameObject. I'm creating a static class called "GameUtils" that will have a static method for "ProcessTime(long timeToProcess, GameObject, objectToProcess", this method will check if objectToProcess is instanceof GameNode and if it is it will call a static method for "ProcessTime(long timeToProcess, GameNode nodeToProcess" which will issue "process gameobject" to all gameobjects attached to this node. So, the questions to this point, 1. GameObject interface only implements a static final long "globalNumberOfObjects" which is supposed to be incremented whenever an object is created and "public void action(long timeToProcess)" which contains the custom actions for every object implementing GameObject. I want GameNode object to have a constant ArrayList<GameObjects> objectCollection = new ArrayList<GameObject>() - the question is, will this create a constant reference to a new ArrayList for every class implementing GameNode? 2. In the future I wish to introduce a "SpatialNode" interface that implements an ArrayList that can only collect "SpatialGameObjects", assuming I implemented "GameNode" interface with constant reference to ArrayList<GameObject>, can I override this reference with a reference to a ArrayList<SpatialGameObject> thus essentially allowing me to preserve all the implementation I wrote for the GameNode interface? (assuming I just catch the exception that arraylist throws if I try to add an incompatible object) thanks
|
# ¿ Dec 16, 2012 14:20 |
|
|
# ¿ Apr 27, 2024 12:48 |
|
Ok, let's say I have another interface "GameObjectWithEventQueue", and I want in the future to be able to create a GameObject that implements both GameNode and EventQueue, ideally I would like to avoid copying the GameNode execution code to every class that implements game node. That's why I thought having instanceof checks could be the right way to go, I also considered having boolean constants implemented in the interface allowing me to work around the instanceof checks.
|
# ¿ Dec 16, 2012 19:02 |
|
Multiple inheritance basically. I might have a GameObject that is both a GameNode and has EventQueue, it might have some additional functionality in addition to that. I would like Nodes and Queues to automatically process all contained objects/queued events using the method defined in the GameObject interface. basically something like this: code:
|
# ¿ Dec 16, 2012 19:25 |
|
1. My design is really not set in stone cause I have no assignment or any specific project I'm working on, currently I'm trying to create a framework containing some of the basic classes and code I'll need to implement a wide variety of games in the future. It's a sort of theoretical excersice at that, I'm primarily concerned with getting better with Java and development in general. The reason I came up with this design was so that I'll always be able to update all the GameObjects in my game through issuing a simple update command to the "root node" object. 2. Yeah I realize that this doesn't make sense now, I thought I could create an object reference in every instance of a class implementing game node but I understand that this is impossible cause an interface can't declare any 'class instance' fields.
|
# ¿ Dec 16, 2012 21:00 |
|
Sorry for being slow on the uptake but I'm not sure I understand, If I have the 'Process Queue' functionality and 'Process Node' functionality each implemented in a separate abstract class how will I be able to inherit both in a single class in the future? I understand that "is-a" relationship but I was viewing it as "is a GameObject with queue" simply because for each case the update function adds extended functionality. The way I see it it's "If Object has Node, process all objects attached, If object has queue, process all queued events", I meant to use the interfaces to declare that the object is indeed a "GameObject with Queue" or whatever.
|
# ¿ Dec 16, 2012 23:40 |
|
Maybe my approach here is indeed all wrong, the reason I originally went for this approach thinking 'an object is object, it might contain other objects, it might need to queue actions'. Now it of course hits me that just because an object might need to queue events doesn't mean it needs to handle its own queue, it can pass any event it generates to its parent node, which may or may not handle queues on its own, all the way up to the 'rootNode' which will be guranteed to process events. Essentially the event just needs to contain references to the objects relevant to it or be an inner class (http://forums.somethingawful.com/showthread.php?threadid=2779598&pagenumber=235&perpage=40#post410352326 , thanks nielsm) With this approach, would it be sensible to define "getQueue"/"sendToQueue(GameEvent event)" methods within my GameObject interface? And once again, thanks for all the help.
|
# ¿ Dec 17, 2012 10:49 |
|
You're right of course, Essentially I want the 'GameWorld' to be able to schedule an event for every object in the game, but that's basically 'its own thing' and doesn't really concern the objects themselves. If I'll have an object with its own queue I'll just add a GameQueue object or whatever, maybe coupled with an interface so I could more easily track which objects have queues.
|
# ¿ Dec 18, 2012 12:40 |
|
If you simply tile the trees one on top of the other and make sure only the right side pokes out you won't have trouble with their shape and with the door.
|
# ¿ Dec 18, 2012 22:09 |
|
I have a dumb question, I'm trying to create a very simple program that uses java.util.Scanner, I'm trying to parse whether the user put in a number in which case I add an object to an arrayList or entered some invalid value or pressed 'Enter' in which case I assume he doesn't want to add more objects. I've tried something like this: code:
Yes this is a homework assignment and I know this is generally frowned upon but I got into this mess by trying to do more then the very simple assignment asked for, so I hope this qualifies as a general java question.
|
# ¿ Feb 26, 2014 17:41 |
|
carry on then posted:Well, I can tell you right now that every time you call nextLine() the user has to input something for the program to move on. To see if a string's an int, have a look at the Integer class and see if any of those methods looks helpful. Okay, that makes sense, I guess I can always assign nextLine to a string and then use this method: code:
Thanks! edit: for reference: code:
emanresu tnuocca fucked around with this message at 18:03 on Feb 26, 2014 |
# ¿ Feb 26, 2014 17:49 |
|
Hey, I got this assignment to create a primitive 'to-do list' manager with a text based menu and all sort of junk, there's a certain hierarchy where I must have 'task category' which may contain 'tasks' which may contain 'subtasks', the entire application is contained in a 'todolist' type. So, due to the way I want my text menu class to work I decided that defining two abstract classes would greatly simplify things, so I created a TodoListElement abstract class and a TodoListContainer abstract class which extends the TodoListElement class. One of the fields in the 'TodoListElement' class is 'TodoListContainer parentContainer' which basically points one step higher in the hierarchy towards the container this element is contained within. Now, obviously the highest level in the hierarchy which is 'todolist' by definition shouldn't actually have a parentContainer, I am not exactly sure how I can override this field and its setters\getters given that the implementation within the abstract superclass 'todolistelement' is not abstract. Can I simply declare the field 'parentContainer' (and the setter/getter) as final (= 'null') within the 'todolist' class, will it correctly hide the super classes' field and related methods?
|
# ¿ Jul 11, 2014 14:24 |
|
Modifying the getter makes sense. Yeah there is no real harm as my menu class would never call this method if its current 'focus' is on the top-level container, but this is only due to my hard coded menu logics, I was looking into this from a fool proofing perspective, though I guess you're probably correct and I should just handle this as an exception. Thanks!
|
# ¿ Jul 11, 2014 15:09 |
|
Guys I have a stupid question related to my previous question a few posts back. Is there any way at all to override a field using field declarations in a subclass or is this something I have to do either by overriding the setter\getter methods or alternatively through defining a constructor in the super class? i.e I have code:
code:
Thanks in advance
|
# ¿ Jul 14, 2014 12:42 |
|
Thanks guys. yeah apologies for the weed jokes I wanted to inject some humor into the question, I guess I failed. The motivation for thinking about this issue was that I was creating a simple text menu and I was looking for ways to simplify the menu logics as much as possible, I basically wanted my classes to each have a field that references an object from an enum class that specifies the String to display and an array of ints that specify the valid user inputs and indeed it would seem that for this purpose it would be 'more correct' to require this menu object to be initialized in the super's constructor and require the sub-classes to instantiate the super only using the permitted constructor. Overriding the getters is possible but makes less sense in this context. So, once again, thanks. emanresu tnuocca fucked around with this message at 15:00 on Jul 14, 2014 |
# ¿ Jul 14, 2014 14:49 |
|
Hey guys, I've got yet another stupid question. I'm screwing around with Swing a bit, I was trying to create a very simple app that draws circles of different sizes, assigns them a random velocity and direction and just lets them bounce a bit. http://codepaste.net/ruvug9 Generally it works, but there's some weird behavior when the balls collide with the JPanel's border, my code attempts to detect the collision and then just adds half a pi to the direction, I guess my math is wrong cause the balls don't always bounce back correctly, they often change directions several times before returning at roughly 180 degrees, where I'd expect them to always just add 90 degrees to their course... which admitedly doesn't make much sense. Here's the relevant snippet code:
emanresu tnuocca fucked around with this message at 13:15 on Aug 25, 2014 |
# ¿ Aug 25, 2014 13:06 |
|
Yeah we can but my teacher is somewhat pedantic and claims such names are confusing, I agree that it's actually much more legible the way you wrote it. So I didn't create local variables but I understood from you guys' comments that I should perform the calculations in accordance to the 'collision case', my trigonometry is rusty and the math is all wrong but this makes more sense: code:
Yeah I should definitely figure out the correct math before tinkering with the code. Thanks for the tips guys.
|
# ¿ Aug 26, 2014 13:22 |
|
Spatial posted:I'll go one further: you shouldn't use angles at all. Angles are a horrendous way to represent direction, the only upside is familiarity with the bare numbers. Yeah that was kind of unnecessary I guess, I'd still love not sucking at math as much but after switching "velocity" to simply be a double[2] and ignoring the whole 'velDirection' bullshitery everything worked perfectly within 2 minutes. So yay!
|
# ¿ Aug 26, 2014 18:02 |
|
Thanks, I understand the importance of writing a more legible code and sticking to conventions, in this particular example I was just trying to screw around a bit and have colorful balls jumping all over the place so I didn't mind tinkering with 'brute force', I do agree with everything you said though. And now I have some different questions pertaining to Swing. We were tasked with designing and implementing a user interface for a 'schedule manager' (personal diary?), I already have the scheduler written and working rather perfectly with a console/text based user interface and I had some 'creative' ideas I wanted to explore in regards to the graphic user interface, so I want to code some transitional animations and have some questions: 1. Say I have a JFrame with several JPanels attached to it, I'm trying to create a method that allows me to expand one Panel while contracting every other panel, so I tried using JFrame.getComponents() and iterating over the panels with a for loop, but even though I have 3 panels attached to the frame getComponents() doesn't seem to work, that is it never enters the loop. Am I missing something? Would it simply be better to create a field (ArralyList<JPanel>) that holds references to all attached panels (can create an add(JPanel) metod that calls add(component) while maintaining this reference list)? 2. As I want to create transitional animations, should I use a TimerTask? Or would it be better to create a simple loop that just uses System.CurrentTimeMilis() to do the transitions? Please let me know if these questions are not clear and attach some relevant code snippets. And once again, thank you all for your help.
|
# ¿ Aug 28, 2014 07:16 |
|
Hey guys, Different question, I'm trying to load a .png resource and use it as an icon but I'm getting an exception: code:
code:
|
# ¿ Aug 29, 2014 11:35 |
|
Argh, I placed the folder inside project/src/package, apparently it should just be at project/ oh well.
|
# ¿ Aug 29, 2014 18:54 |
|
Sorry for spamming this thread with questions, I hope nobody minds too much. I have a small problem with using an ArrayList, I had an existing project that used arrays to store objects and I wanted to switch all those annoying arrays to ArrayLists, all the classes stored in those arrays inherit from one abstract class, the thing is that one of those inheriting classes implements Comparable, and I'm required to be able to sort the ArrayList containing this inheriting class, but as the abstract superclass isn't comparable and I can't seem to cast between ArrayList<Abstract Class> to ArrayList<inheriting 'comparable' class> I feel like I might be missing something. I can think of several ways to solve this but I feel like I'm missing something rather trivial and shouldn't start copying arrays or implementing Comparable in the abstract superclass, any hints?
|
# ¿ Sep 1, 2014 21:04 |
|
Internet Janitor posted:emanresu tnuocca: I'm very confused as to why the abstract superclass couldn't implement Comparable- the contract you're expressing with those types is at odds with what you want to do, so it makes sense that it ends up clumsy. Well, in this case it's basically just a bunch of containers where only the bottom-level object in the hierarchy is comparable, for reference it contains a LocalDate field where all the other objects in the hierarchy don't, so yeah I could implement Comparable on the abstract class but it would be rather meaningless for anything other than single inheriting class with the LocalDate field. I was originally using Arrays.sort and would indeed like to use Collections.sort. rhag posted:Is there a problem with List<? extends Comparable> ? Or List<? extends MyAbstractThing> ? Thanks for the advice regarding containers, I'll mull over it. Yeah I was wondering if generics were the way to go here, but I'm still confused as to how to run it through Collections.sort? code:
|
# ¿ Sep 1, 2014 21:47 |
|
What's the conventional wisdom in regards to implementing ActionListeners? In the spirit of making my questions less obtuse I'll add some screenshots, So I have my stupid TaskManager application: At first each one of these panels implemented ActionListener and executed methods in my top-level frame class, I figured that was pretty stupid and changed it so that the toolbar and menubar no longer implement ActionListener and instead use the top-level frame as a Listener, no difficulties here. However, when you click the add button you get a JDialog frame: Pressing OK/Cancel, along with obviously disposing of the window, is also supposed to generate a new "TaskManagerElement" (i.e, category, task or subtask) for and add it to my data containing class, there's some logic involved in that such as checking out that there's enough space, sorting out the SubTask (yes the Collections.Sort question from earlier) and updating the JTree display, so most of its functionality relates to the main frame, however, for me to be able to create the window in one action and dispose of it in another, I'll probably need to turn the JDialog into a field in the main frame class. Now it isn't a big deal but I'm starting to wonder if I'm doing the right thing, should I keep the JDialog's ActionListener implementation and have it execute methods in the main frame class? Is there even any convention here. Thanks
|
# ¿ Sep 3, 2014 10:23 |
|
More questions: 1. I'm trying to give default focus to one text field in a dialog box but even though I've used code:
the focus keeps defaulting to the radio buttons, seems like it just gives focus to the first added button? Any clue about this issue? 2. This is kinda silly but is there any way to give different levels in a JTree a preset icon rather than the default "Folder/Document" thing they've got going on? I just want Level 1 to always be a folder, level 2 something else and level 3 always a document, or something along those lines. I'm really not sure how to go about this. carry on then posted:The convention you're looking for is called a callback: http://en.wikipedia.org/wiki/Callback_(computer_programming). Your JDialog should hold a reference to the main frame that created it, and your main frame should have some method like AddTask(TaskManagerElement) that will do everything to get the task added. The JDialog calls this and passes the task over, then closes itself. It sounds like this is close to what you have now, and it's a common pattern in GUI applications. rhag posted:In Java this is also known as a "listener" (look at jbutton ActionListener for example). Thanks guys, this was helpful!
|
# ¿ Sep 6, 2014 19:30 |
|
1337JiveTurkey posted:Is the dialog visible when you make the request or are you doing it when you're laying out the component before you display it? Well, it wasn't, but now I fixed that and I execute the requestFocusInWindow() right after I set the frame visible, and it still gives focus to the checkbox. I'm trying to look for stuff that might have gone wrong but as far as I can tell I finish populating the entire frame, then set it visible and only then request focus so it should work?
|
# ¿ Sep 6, 2014 21:23 |
|
Does the app try to load anything from external files? Cause I had a similar thing happen once cause I didn't copy the files to be side-by-side with the JAR file. Also, exceptions can behave a little differently outside the IDE, an exception that might be safely ignored in the IDE can sometimes just stall things (and not display) when running in an external JAR.
|
# ¿ Nov 28, 2014 16:57 |
|
Hey, I have this stupid small issue, I set up a small window with this maze like thingy that is controlled through the keyboard, there's also a text field input on a toolbar, the problem is that as soon as I click on the text field I can't get the keyboard commands to control my maze thingie, both are focusable, i'm not sure what I'm doing wrong: code:
thanks edit: apparently pressing tab does switch focus back to the other panel, dunno why clicking it with the mouse doesn't work, guess I could make it so that pressing enter also makes it lose focus somehow. emanresu tnuocca fucked around with this message at 11:51 on Jul 18, 2015 |
# ¿ Jul 18, 2015 10:06 |
|
carry on then posted:What about putting a MouseListener on the component you want to take focus and call requestFocus() in that? It sounds like focus is being given to it by the parent window when you tab over from a control that you can focus, but you have no way of your component requesting focus for itself, for instance in response to a mouse event. Thank you this was helpful! btw, do any of you guys know of a way to lock the aspect ratio of a JPanel component? I suppose I could use a component listener to ensure that width/height are the same upon resize but isn't there some more standard way to go about this?
|
# ¿ Jul 21, 2015 10:18 |
|
After setting an AffineTransform to invert the Y axis and set (0,0) to be the center of the window is there some method that returns the min/max values for x and y? Obviously it's pretty easy to calculate (+- getWidth / 2 basically), I just wonder if there's a way to get this value from the JPanel, cause it seems like there should be?
|
# ¿ Aug 5, 2015 17:04 |
|
Is there anyway to do something like this:code:
Just curious if there's a way to instantiate from a generic type like that.
|
# ¿ Sep 8, 2015 16:40 |
|
carry on then posted:Is it because you wrote T in the declaration but K in the method? Thanks. Well it looks like if I do that I'll only be able to use the default constructor. Which I guess could still work.
|
# ¿ Sep 8, 2015 17:18 |
|
I am probably missing point but why not just use Character.touppercase() ?
|
# ¿ Sep 15, 2015 18:41 |
|
Uhm yeah I was also a little confused but looking at the table: http://unicode-table.com/en/ Unicode A = '65', unicode 'a' - '97', the difference is 32, so 'a' - 'A' != 'A', looks like you need to do 'smallCaps' - 32 = 'largeCap'. Edit: ('SmallCharacter' - (a - A)) = LargeCaps Edit: yeah this works code:
emanresu tnuocca fucked around with this message at 19:35 on Sep 15, 2015 |
# ¿ Sep 15, 2015 19:16 |
|
You guys completely lost me with subtracting to value of 'A' to turn an lowercase letter into an uppercase one. My example with (char) lowerCase - ('a' - A') worked perfectly so... what's going on? How are you guys doing it merely by subtracting 'A'?
|
# ¿ Sep 16, 2015 13:57 |
|
I am well aware of that, and yet as I've pointed out last page, subtracting the value of 'A' from 'a' does not return 'A', you need to subtract the difference between 'a' and 'A'. Like I posted the previous page, the following code works: code:
code:
|
# ¿ Sep 16, 2015 14:09 |
|
|
# ¿ Apr 27, 2024 12:48 |
|
I have a follow up question concerning the instantiation of generic classes, I last asked about this question when I wanted to make a particle system for this game exercise I'm working on and I wound up using class.newInstance() as was suggested and it works but I thought it was really unelegant and I was trying to figure out how to use a Factory method to make things a little more straightforward (and possibly avoid the try catch loop), but I am somewhat confused about the whole thing. First here's my an edited snippet of what my abstract particleEmitter<K extends ParticleObject> currently does to produce new particlescode:
The way I work now I create a class which extends the particle emitter (say, Thruster) and this class calls the super() constructor sending the relevant class to the particle emitter, in which case the generic parameter is actually seemingly never used and I'm not sure why I even bother declaring it, ideally I would have preffered creating something like ParticleEmitter<FireParticle> and not have to put the class object in the constructor, looking at explanations about Factory methods or Supplier interfaces I don't think that will work either... so try to explain in pseudocode what I'm looking for, it would be something like this: code:
Looks like If I were to create a factory method I could do something like code:
Just for kicks and to show that things actually work fine now, here's what I got so far: https://www.youtube.com/watch?v=aI8gjAmrEG8 Fake edit: now you guys got me hankering to work with JSON to define my levels and objects as well.
|
# ¿ Sep 30, 2015 13:53 |