|
Yhag posted:Hopefully a simple question: Well you are trying to access a non static method in a static way, Plot.repaint() is static why not try super.repaint(); you have already done that with the paintComponent.
|
# ? Sep 5, 2014 07:23 |
|
|
# ? May 17, 2024 09:58 |
|
TheresaJayne posted:Well you are trying to access a non static method in a static way, Thanks, that got rid of that error, it won't draw the line though, even if I just add something like g.drawline(0, 0, 100, 100);.
|
# ? Sep 5, 2014 07:55 |
|
To be specific since you said you're new to this, you're calling the redraw method of Plot, which is a class (note the capitalisation at the start of the name). By the sounds of it, redrawing is something that only makes sense in the context of an instance, which is when you actually create a Plot-type object with its own state and then do things to it, like giving it drawing data then asking it to redraw itself. Calling a method on the general class itself will either do some general purpose function, like performing some task in the program or returning a value (like when you call Math.sin() or whatever), or it will change something about the state of all Plot objects as a whole. It won't affect a single instance like you want it to - without passing it any info, it couldn't know which Plot object you wanted to mess with. It's like calling the head office. So what you want to do is tell a specific instance to redraw itself, by calling redraw() on that object. Since your code is running in one of those objects, which is subclassed from Plot, you can just call the method, and it'll run it on itself (Some of that may be technically squirrelly but I'm just trying to give a general idea here) And how come you're trying to use doubles? e- I'm just guessing here since I haven't used what you're using, but are you using two panels, one with the grid and one to draw a line on? (Since you've got two versions of the same method.) You're not calling redraw on the grid, which seems to work, why do you need it for the line - you're not clearing it after you've drawn it, are you? And are you sure the panel isn't beneath the first one? baka kaba fucked around with this message at 08:19 on Sep 5, 2014 |
# ? Sep 5, 2014 08:10 |
|
Thanks for the quick replies, made me realise I was calling on the wrong thing, it works now! I was using doubles because they were being used in some example I had, using ints now and actually calling the proper redraw now. Using this now for the grid panel: code:
code:
|
# ? Sep 5, 2014 08:33 |
|
If you're just using those p-variables as something to pass into your methods, you could just doJava code:
I'd be very tempted to turn all those set calls into one that passes an array, so you can do it all in a line or two, but it's not a huge deal if you're only doing it in one place
|
# ? Sep 5, 2014 14:29 |
|
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 |
|
emanresu tnuocca posted:More questions: 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?
|
# ? Sep 6, 2014 20:26 |
|
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 |
|
Why exactly does the JVM have to "warm-up"? Do any other languages have similar behavior at run-time?
|
# ? Sep 8, 2014 02:47 |
|
The JVM will compile frequently executed code paths to machine code and store that. It needs time to figure out which paths to compile and save, during which all instructions are JIT compiled, and therefore slower. I'm not sure specifically what other languages have this, but any with a compile-ahead capability activated based on the same reasoning would likely have a warm up period. Fun fact: This is where hotspot gets its name from. carry on then fucked around with this message at 02:59 on Sep 8, 2014 |
# ? Sep 8, 2014 02:51 |
|
the .net clr has similar behaviour
|
# ? Sep 9, 2014 07:10 |
|
Yhag posted:And this for the other one: Is there a particular reason why you're casting all those ints, storing them into ints, then passing those ints along to your setxx methods instead of just casting them and passing them straight? Like this: code:
pliable fucked around with this message at 11:26 on Sep 11, 2014 |
# ? Sep 11, 2014 10:03 |
|
pliable posted:Is there a particular reason why you're casting all those ints, storing them into ints, then passing those ints along to your setxx methods instead of just casting them and passing them straight? Well i have seen stuff like this in the past code:
|
# ? Sep 11, 2014 12:40 |
|
In C the cast is cheap (not dynamic_cast though in C++), in Java is not. Java always does dynamic_cast (that is it checks the type, and throws ClassCastException or something along those lines) if the types don't match. Therefore, don't cast (in Java) if you dont have to. Now, it could be that the compiler may compile that code away (or JIT at least), but still ... you don't have to, therefore don't. Even in C ... is dumb to cast an int to int (but there are rare cases where one needs to do crazy casts for some nefarious purposes).
|
# ? Sep 11, 2014 13:04 |
|
TheresaJayne posted:Well i have seen stuff like this in the past To be sure, that is completely unnecessary and you don't have to do that at all. Realistically in Java the vast majority of casting is to cast down to a subclass from a superclass to access subclass methods/fields. Occasionally you might see casting between float and int, but there are usually better/more accurate ways of doing even that (i=Math.round(f)).
|
# ? Sep 11, 2014 18:46 |
|
TheresaJayne posted:Well i have seen stuff like this in the past I'm a C coder as well and maybe I'm fortunate enough to have gone to a great university, but if any of my professors saw this after 101, they would fail us immediately.
|
# ? Sep 11, 2014 20:16 |
|
carry on then posted:To be sure, that is completely unnecessary and you don't have to do that at all. Realistically in Java the vast majority of casting is to cast down to a subclass from a superclass to access subclass methods/fields. Occasionally you might see casting between float and int, but there are usually better/more accurate ways of doing even that (i=Math.round(f)). int i=(int)(f+0.5f); if you're allergic to method calls and clarity.
|
# ? Sep 12, 2014 05:53 |
|
carry on then posted:To be sure, that is completely unnecessary and you don't have to do that at all. Realistically in Java the vast majority of casting is to cast down to a subclass from a superclass to access subclass methods/fields. Occasionally you might see casting between float and int, but there are usually better/more accurate ways of doing even that (i=Math.round(f)). Math.round doesn't return the same value as casting does though. Casting does round towards zero though. HFX fucked around with this message at 17:33 on Sep 12, 2014 |
# ? Sep 12, 2014 16:35 |
|
HFX posted:Math.round doesn't return the same value as casting does though. It does round towards zero though. That's what I meant. Casting is truncation but I know some rookies think it's the same as rounding, then wonder why their 4.9999 casted to 4 and not 5.
|
# ? Sep 12, 2014 16:41 |
|
HFX posted:Math.round doesn't return the same value as casting does though. It does round towards zero though. No, but Math.floor works if you want to explicitly replicate casting behavior. Unfortunately you then have to cast the result back to an int again, which is self-defeating!
|
# ? Sep 12, 2014 16:42 |
|
I just edited my post above as the way I wrote it would have been wrong assuming usual English usage. My post above needed to be more clear about the "it". Casting will always round towards 0 including negative values. This means the correct behavior would be to use Math.ceil for negative values and Math.floor for positive values. The (int)(f+0.5f) only works for positive values sadly. I had to learn this the hard way a few times while I was an undergrad. Learning about C and how numbers are represented helped a lot.
|
# ? Sep 12, 2014 17:38 |
|
CarrKnight posted:First of all, thank you for all the answers. If you want to do the codes in java for 2d game, and make it run in modern browser, use libgdx. With very little work you can compile it js and run in a browser. No need to implement client-server stuff. But you would need to rewrite a bunch of stuff, mostly the rendering/gui. Heres an old demo of my android game running in a browser, no plugins or anything required
|
# ? Sep 13, 2014 21:21 |
|
What's a good book or two for getting an understanding of Java 8 as well as patterns for writing java well? I'm guessing effective java and another book?
|
# ? Sep 15, 2014 16:28 |
|
FamDav posted:What's a good book or two for getting an understanding of Java 8 as well as patterns for writing java well? I'm guessing effective java and another book? Effective Java and Java Concurrency in Practice are my two go-to Java books. They're not going to cover 8, but honestly, there's not so much new stuff that you wouldn't be better off reading blog posts.
|
# ? Sep 16, 2014 04:11 |
|
evilentity posted:If you want to do the codes in java for 2d game, and make it run in modern browser, use libgdx. With very little work you can compile it js and run in a browser. No need to implement client-server stuff. But you would need to rewrite a bunch of stuff, mostly the rendering/gui. Heres an old demo of my android game running in a browser, no plugins or anything required That looks really cool. Let me take a look. You were very kind. Thanks.
|
# ? Sep 17, 2014 14:31 |
FamDav posted:What's a good book or two for getting an understanding of Java 8 as well as patterns for writing java well? I'm guessing effective java and another book? Also turn on the Java 8 inspections in your IDE, IntelliJ will let me know I'm not doing certain things the Java 8 way, it has been pretty handy.
|
|
# ? Sep 17, 2014 19:32 |
|
Yeah, libgdx seems super neat and easy to use? Makes me want to get an iOS test device.
|
# ? Sep 17, 2014 22:14 |
|
I want to know, is google GWT kind of like libgdx? Libgdx is cool, great. I like the easy life-cycle. But I need boring ui. With buttons and charts. Kind of like the GWT widgets! But is gwt just a client gui with the java part running on a server? Or is it like libgdx where the whole java is magically turned into javascript and run in browser?
|
# ? Sep 18, 2014 15:17 |
|
CarrKnight posted:I want to know, is google GWT kind of like libgdx? libgdx uses GWT when it builds for web
|
# ? Sep 18, 2014 15:43 |
|
You can make boring gui in libgdx just fine. spine 2d is made in libgdx.
evilentity fucked around with this message at 20:44 on Sep 18, 2014 |
# ? Sep 18, 2014 16:15 |
|
CarrKnight posted:I want to know, is google GWT kind of like libgdx? GWT just turns java code into javascript. you can have whatever you want on the server side (servlets in java, nodejs, python whatever floats your boat).
|
# ? Sep 18, 2014 19:26 |
|
evilentity posted:You can make boring gui in libgdx just fine. spine 2d is made in libgdx. That looks fantastic. But it is proprietary. Is there anything similar that is open-source enough for me to peek?
|
# ? Sep 19, 2014 00:15 |
|
I was trying to write a simple program that plays pool (badly) but I came across a problem. In my main function I have something likecode:
|
# ? Sep 19, 2014 03:46 |
|
Cool Dogs Only posted:I was trying to write a simple program that plays pool (badly) but I came across a problem. In my main function I have something like Threads are ... complicated (to put it mildly). Start here: http://docs.oracle.com/javase/tutorial/essential/concurrency/ and go from there. It should tell you all you need to know for basic needs.
|
# ? Sep 19, 2014 04:18 |
|
quote:My problem is that this loop never finishes even if the button is clicked (a print statement in the actionListener shows it works) and start is set to true unless I either print something to the terminal or use Thread.sleep(1) (or any number) within the while loop But i am curious, what happens in the //wait part?
|
# ? Sep 19, 2014 09:47 |
|
CarrKnight posted:For these things you really ought to use a semaphore. Funny you say that, I just learned about semaphores in class today. And nothing happens in the wait part, I just wanted to keep repeating the loop. I'll look into semaphores and the concurrency guide, thanks guys.
|
# ? Sep 19, 2014 19:15 |
|
Cool Dogs Only posted:Funny you say that, I just learned about semaphores in class today. And nothing happens in the wait part, I just wanted to keep repeating the loop. I'll look into semaphores and the concurrency guide, thanks guys. Just never block the main thread, and you won't worry about "huh, I wonder why my app locked up?" nearly as much.
|
# ? Sep 20, 2014 01:12 |
|
Cool Dogs Only posted:I was trying to write a simple program that plays pool (badly) but I came across a problem. In my main function I have something like It's because things that are happening in another thread may, as far as the first thread is concerned, happen in a completely different order or time unless some form of synchronisation is used. You hear about CPUs having out-of-order and predictive execution, hyperthreading, three levels of cache and whatnot -- all that is leveraged by the computer and the JVM to make things faster, at the cost of things happening in one thread being invisible or looking totally bizarre when viewed from another thread. For example there's a value at memory location X that is increased by thread A. The CPU reads the value from RAM, increments it, but stores it in cache for time being. Thread B may be looking for the increased value, but reading from X would yield the old value, since the CPU hadn't bothered to write A's new value down yet. Thread B may be writing something different into X, but sometime later the CPU may be flushing its cache and write the old, increased value from A back to X again, overwriting whatever B wrote in it meanwhile. Weird rear end poo poo would happen. There are some ways of restoring sanity to the universe and synchronizing the happened-beforeness of two threads. Simplest is to declare variables volatile. Suppose myGUI.getStart() returns the value of boolean field myGUI.start. If you then replaced the declaration of boolean start; with volatile boolean start;, then all changes to that variable would be guaranteed to be immediately visible to other threads, and changing start to false would immediately end your loop. Mind you, volatile variables can still be used in a non-threadsafe manner since in eg if (myGui.getStart==false) { myGui.setStart(true); } the start variable could be changed by another thread between checking it and setting it true. There are Atomic* wrapper types to help with that sort of thing. The other and most useful way is synchronization, but I really can't be bothered to write too much on the subject. Basically, if you need to share information between two threads, it usually needs to go through a synchronised block of code which could only be executed by one thread at a time, and the threads would synchronize their state when they arrive to that block of code. Writing to System.out, as far as I know, is synchronised, which means before they call that line your threads get their poo poo together enough for it to also help with seeing your getStart() value change.
|
# ? Sep 20, 2014 13:45 |
|
I'm confused about CancelledKeyExceptions and a SelectionKey when using NIO Selectors. I have a situation where an underlying socket channel may be closed by any thread. In a thread separate from where a channel is cancelled, I am calling select() with channels that are registered with OP_READ. Occasionally I get a CancelledKeyException when I test isReadable(). Clearly another thread is closing a socket channel after select() has returned but before I test isReadable(). I believe my best course of action is to catch a CancelledKeyException. My only concern is that this is an unchecked runtime exception, which would seem to indicate a unrecoverable condition or programming error on my part... Some references on stack overflow suggest testing isValid() && isReadable(), but this has an obvious time-of-check-time-of-use error. Other than catching an unchecked exception, I do not understand how else to detect and recover from this condition.
|
# ? Sep 21, 2014 07:50 |
|
|
# ? May 17, 2024 09:58 |
|
Nothing wrong with unchecked runtime exceptions, in fact they can be neater than checked exceptions as they don't clutter the interfaces so much. As long as you remember to catch them at one point.
|
# ? Sep 21, 2014 18:17 |