The Big Programming Thread - Page 337
| Forum Index > General Forum |
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. | ||
|
CecilSunkure
United States2829 Posts
| ||
|
Millitron
United States2611 Posts
I'm trying to use Java to create a simple coordinate plane in an image file. Basically I want a jpeg that looks like graph paper. I can't simply grab one off google images because I don't know exactly what size I need yet or how big each grid will be, and I'd like to be able to change these easily. My current code is as follows: import java.awt.image.*; I've got a blank bitmap image of the right size and name in the package in eclipse, and the print statement triggers exactly as it should. I get no exceptions thrown or anything like that. But the image is still blank after I run the code. "image.setRGB()" doesn't seem to be doing anything. | ||
|
JeanLuc
Canada377 Posts
On August 08 2013 13:16 Millitron wrote: Alright, my google-fu has failed me, I'm pretty stumped on this one. I'm trying to use Java to create a simple coordinate plane in an image file. Basically I want a jpeg that looks like graph paper. I can't simply grab one off google images because I don't know exactly what size I need yet or how big each grid will be, and I'd like to be able to change these easily. My current code is as follows: import java.awt.image.*; I've got a blank bitmap image of the right size and name in the package in eclipse, and the print statement triggers exactly as it should. I get no exceptions thrown or anything like that. But the image is still blank after I run the code. "image.setRGB()" doesn't seem to be doing anything. See if you can draw something to the image in the simplest way possible, then work outwards from there. | ||
|
Millitron
United States2611 Posts
On August 08 2013 13:29 JeanLuc wrote: See if you can draw something to the image in the simplest way possible, then work outwards from there. I have. I can't change a single pixel. If I println one of the BufferedImage's pixels, I can see that I AM changing the pixel color in the BufferedImage, I just can't seem to get the file to reflect that change. | ||
|
JeanLuc
Canada377 Posts
On August 08 2013 13:34 Millitron wrote: I have. I can't change a single pixel. If I println one of the BufferedImage's pixels, I can see that I AM changing the pixel color in the BufferedImage, I just can't seem to get the file to reflect that change. Try looking at this: http://stackoverflow.com/questions/12674064/how-to-save-a-bufferedimage-as-a-file | ||
|
Rannasha
Netherlands2398 Posts
On August 08 2013 13:16 Millitron wrote: Alright, my google-fu has failed me, I'm pretty stumped on this one. I'm trying to use Java to create a simple coordinate plane in an image file. Basically I want a jpeg that looks like graph paper. I can't simply grab one off google images because I don't know exactly what size I need yet or how big each grid will be, and I'd like to be able to change these easily. My current code is as follows: import java.awt.image.*; I've got a blank bitmap image of the right size and name in the package in eclipse, and the print statement triggers exactly as it should. I get no exceptions thrown or anything like that. But the image is still blank after I run the code. "image.setRGB()" doesn't seem to be doing anything. Unless this isn't your entire code, you're not writing the image to a file. You create an empty .bmp, read it in, then manipulate the image in memory and then... nothing. Instead, try creating the image object, manipulate it and then write it to a file. edit: Changing your loops to for (int i = 10; i < 2000; i += 10) (and similar for j) will reduce the number of iterations by a factor 100 and allows you to also remove the if-statement, giving an additional small speedup. | ||
|
spinesheath
Germany8679 Posts
On August 08 2013 07:37 RoyGBiv_13 wrote: protip, inline this function... or it could be that the compiler already did so, and that was why it was so fast... In C++, the inline keyword is just a hint for the compiler. It doesn't enforce anything. Usually it's just ignored. There is __forceinline for in microsoft's compiler which should actually force it (though I wouldn't be too sure about that), and other compilers might have similar language extensions. But in general you can and should just rely on the compiler, especially when it comes to templates. The inlining for such small templates works really well in solid C++ compilers anyways. I've used much more complicated template code in the inner loop of a simulation program that needs every little bit of performance and got identical performance compared to the copy-and-paste-inlined version (and the copy-and-paste even had minor optimizations depending on the context it was pasted in). | ||
|
CecilSunkure
United States2829 Posts
| ||
|
nunez
Norway4003 Posts
i would inline that swap, but would rather use std::swap (which is inline). | ||
|
n0ise
3452 Posts
Any quick tips/pointers or a particular piece worth reading? Much appreciated | ||
|
Millitron
United States2611 Posts
On August 08 2013 14:18 Rannasha wrote: Unless this isn't your entire code, you're not writing the image to a file. You create an empty .bmp, read it in, then manipulate the image in memory and then... nothing. Instead, try creating the image object, manipulate it and then write it to a file. edit: Changing your loops to for (int i = 10; i < 2000; i += 10) (and similar for j) will reduce the number of iterations by a factor 100 and allows you to also remove the if-statement, giving an additional small speedup. Thanks. I got it to work, though I couldn't do your idea for the for-loops. That only colored the corners of every, it did not draw the edges. I don't mind the minor efficiency cost. I doubt the images will ever be much bigger than 2000x2000, and they're not used in anything time-critical. If anyone's curious, I'm making a simple map-editor tool so my less tech-savvy friends can help make maps for a game we'd like to make. | ||
|
Shield
Bulgaria4824 Posts
| ||
|
DeltaX
United States287 Posts
Would be one way. You could also use the normal for loop for(i = 0; i < 2; i++){stuff} as long as it has at least 2 elements so you don't get null pointer errors. I think typically you would use your method when you want to iterate over everything in the array, but use a normal for loop if you need them in a specific order, or a specific number of elements. | ||
|
Shield
Bulgaria4824 Posts
But it prints only the 1st element. It seems like "E element : inputArray" loop condition doesn't exactly iterate like normal loops we are used to. I tried the normal loop as you said and this one works.
I think elements are printed as strings here. Unfortunately, there's no array boundary checking which reminds me of a question that I want to ask. Is it a good idea to always use try/catch{} when you deal with arrays? Edit: I think loop condition int i = 0; i < inputArray.length; i++is much more readable than E element : inputArraywhich I find in some tutorials. Agree? I'm reading about generics on this website: http://www.tutorialspoint.com/java/java_generics.htm Edit #2: The code still works for me when I replace 'E' with 'T'. Now I'm lost at the difference. When do you use T, E, K, V, etc? | ||
|
phar
United States1080 Posts
On August 09 2013 11:50 darkness wrote: Your code didn't work as I expected when I tried to execute it. I tried to make it like:
But it prints only the 1st element. It seems like "E element : inputArray" loop condition doesn't exactly iterate like normal loops we are used to. Step through the code manually, you should be able to see the error. On August 09 2013 11:50 darkness wrote: I think elements are printed as strings here. Unfortunately, there's no array boundary checking which reminds me of a question that I want to ask. Is it a good idea to always use try/catch{} when you deal with arrays? You should be able to do this without try/catch. try/catch introduces unnecessary overhead here, and makes things harder to read. Just manually do the bounds checking. Alternatively, you could be using lists instead.
| ||
|
WolfintheSheep
Canada14127 Posts
On August 09 2013 11:50 darkness wrote: Your code didn't work as I expected when I tried to execute it. I tried to make it like:
But it prints only the 1st element. It seems like "E element : inputArray" loop condition doesn't exactly iterate like normal loops we are used to. Two problems here. One, your initializing i as 0 on every single iteration, so it will never increment. Second, you're ending the loop when i < 2...so when i is less than 2. | ||
|
Shield
Bulgaria4824 Posts
On August 09 2013 12:52 WolfintheSheep wrote: Two problems here. One, your initializing i as 0 on every single iteration, so it will never increment. Second, you're ending the loop when i < 2...so when i is less than 2. Correct! Thanks everyone. I need to turnBrainOn() more often. :D | ||
|
Ilikestarcraft
Korea (South)17731 Posts
In this code while (*s++ = *t++); I know that its copying a string but what confuses me is how does the loop actually work without what it seems to me a test condition? | ||
|
Blisse
Canada3710 Posts
int main(int argc, char const *argv[]) This prints false. I believe it will take the last value of the assignment operation, which is the '\0' character in a string, and cast that to a boolean. The boolean cast of the nullchar '\0' is false. For all other characters, casting to an int is treated like a non-zero int, which evaluates to true, so the while loop continues. edit: If we're talking about C++ strings, I don't think that will work 100% of the time because if the string pointer has gone past the string the behavior is undefined. It might just work because the space after the string is null, but if it's used, something's going to crash. | ||
|
Yoshi-
Germany10227 Posts
On August 10 2013 01:08 Ilikestarcraft wrote: Have a quick question In this code while (*s++ = *t++); I know that its copying a string but what confuses me is how does the loop actually work without what it seems to me a test condition? At one point the pointer *t will not point at anything anymore, this will return some from of NULL, and this most probably gets interpreted as false, and the loop ends | ||
| ||