The Big Programming Thread - Page 818
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. | ||
Nesserev
Belgium2760 Posts
| ||
Manit0u
Poland17187 Posts
I remember that not so long ago people could barely compile anything in it and now dozens of big companies are using it for some critical parts of their operations. Mozilla is pretty big into it - not just taking it under its wings but also moving Firefox to it and developing new browser engine in it (to be released late 2017). Videos posted by Mozilla Foundation show that some of their most senior devs consider it to be the new C++ but better. Pretty amazing. | ||
RoomOfMush
1296 Posts
On December 21 2016 00:58 Manit0u wrote: Holy crap! When did Rust get so much traction? I remember that not so long ago people could barely compile anything in it and now dozens of big companies are using it for some critical parts of their operations. Mozilla is pretty big into it - not just taking it under its wings but also moving Firefox to it and developing new browser engine in it (to be released late 2017). Videos posted by Mozilla Foundation show that some of their most senior devs consider it to be the new C++ but better. Pretty amazing. Although I really like many of the concepts of that language I absolutely hate the way it looks like. Its fugly in my opinion. | ||
Deleted User 3420
24492 Posts
All I want to do is draw a circle wherever I click on my jpanel. Here is my code: + Show Spoiler +
I have no idea what exactly validate and revalidate do. But I've figured out I need them. It's all very confusing. I've figured out my problem here is: g.drawOval(x, y, 50, 50); It's the x and y variables. For some reason paintComponent isn't using an updated x and y (it should change when mouseclick happens..). I know this because if I write g.drawOval(50, 50, 50, 50) then there is no problem and it draws my circle at 50,50. So that's cool and all but I want to draw the damn circle where I click the mouse. So how in the world do I do that? | ||
Aerisky
United States12128 Posts
| ||
Deleted User 3420
24492 Posts
| ||
Blisse
Canada3710 Posts
You're doing a weird thing where you keep adding GraphGUI/JPanels inside your JFrame. This should be a memory leak but still bad practice. You should just be updating an existing GraphGUI (if you're doing it like that) by revalidating it. You shouldn't be referencing the static window like this. You can just add the GraphGUI to your JFrame once in the main function. Use a Point2D class if you want to represent a point in space. This lets you be more descriptive. You're calling setSize inside the paintComponent which generally makes no sense. paintComponent should just paint the component. The size of the GraphGUI should just be the size of your window here. You would attempt to resize the window every time it paints. Do the setSize once wherever you initialize the GraphGUI. You shouldn't have a static MouseListener. The MouseListener should be in the GraphGUI/JPanel, added in its constructor. You can also use a MouseAdapter to not have the empty Overrides. Your specific problem is that revalidating the outer window doesn't propogate the invalidation to its inner windows. GUI systems work the other way. When you invalidate inner windows the invalidation propogate upwards to its parents. I believe you also also want to call repaint, not just revalidate. The terminology is confusing but they affect layout vs redraw. Lmk if that fixes things. | ||
Aerisky
United States12128 Posts
Blisse seems like he knows what he's talking about here though >.< | ||
mantequilla
Turkey775 Posts
| ||
RoomOfMush
1296 Posts
First of all, you are creating a new JPanel with each click and you try to add it to your JFrame. This is a really bad idea. By default the content pane of a JFrame is a JPanel with a BorderLayout. The BorderLayout can only take 5 components to lay out, after that I would assume it will crash but perhaps the error will be silently ignored. The next problem is that your paintComponent method in your JPanel mutates (your setSize call) the JPanel itself which is illegal in Swing. You are also not running your application on the EDT (EventDispatcherThread) which you are supposed to do in Swing. Here is a working example: + Show Spoiler + import java.awt.Color; | ||
RoomOfMush
1296 Posts
On December 21 2016 13:53 Blisse wrote: You're doing a weird thing where you keep adding GraphGUI/JPanels inside your JFrame. This should be a memory leak but still bad practice. There are no memory leaks in java (at least not at travis level). He is adding more components to his frame though and this WILL cause problems with his SceneGraph / GUI-Tree / DOM (whatever you want to call it in Swing). On December 21 2016 13:53 Blisse wrote: You should just be updating an existing GraphGUI (if you're doing it like that) by revalidating it. Almost but not quite. Revalidating will do nothing in that case. What you want to do is repaint because the graphics have changed. Revalidation is only necessary when changing the GUI-Tree on the fly without using well-defined methods to do it. Its basically only needed when doing low-level manipulations. Even in that case an invalidate() is probably the better idea than a revalidate() but at that point even I get sometimes confused. | ||
mantequilla
Turkey775 Posts
my room is pretty far away from the door so often I can't hear the doorbell. Currently I'm abusing Skype to call from my cellphone near the door to my pc in my room when I am expecting someone. But it's not an optimal solution since it occupies my phone. I borrowed a bare arduino uno from someone to fiddle with it. And found a simple project on the net that connects a mic to arduino, but it can only 'detect' a presence of sound, not transmit it. what I need is a simplest circuit that can convert sound to analog signal to be supplied to arduino's analog inputs. From there I can figure out on my own the programming of arduino and a java program that will listen on my pc. All will run on lan ![]() | ||
phar
United States1080 Posts
https://www.google.com/amp/www.instructables.com/id/Arduino-Basics-Making-Sound/?amp_page=true?client=ms-android-google Ahh fucking AMP links sorry. Let me know if I'm misunderstanding something about your question. | ||
Cyx.
Canada806 Posts
On December 21 2016 00:58 Manit0u wrote: Holy crap! When did Rust get so much traction? I remember that not so long ago people could barely compile anything in it and now dozens of big companies are using it for some critical parts of their operations. Mozilla is pretty big into it - not just taking it under its wings but also moving Firefox to it and developing new browser engine in it (to be released late 2017). Videos posted by Mozilla Foundation show that some of their most senior devs consider it to be the new C++ but better. Pretty amazing. Mozilla were the people who started Rust if I'm not mistaken, though it's a lot bigger than them now. I kind of agree with the people who think it's basically a better C++, in the sense that it fills the same systems-programming niche as C++ but in a much more modern and safer way. Though I haven't used it for anything serious at all so take that with a grain of salt. | ||
Blisse
Canada3710 Posts
On December 21 2016 20:26 RoomOfMush wrote: There are no memory leaks in java (at least not at travis level). He is adding more components to his frame though and this WILL cause problems with his SceneGraph / GUI-Tree / DOM (whatever you want to call it in Swing). Almost but not quite. Revalidating will do nothing in that case. What you want to do is repaint because the graphics have changed. Revalidation is only necessary when changing the GUI-Tree on the fly without using well-defined methods to do it. Its basically only needed when doing low-level manipulations. Even in that case an invalidate() is probably the better idea than a revalidate() but at that point even I get sometimes confused. Yeah I kind of mixed up "memory leak" with "continually adding useless objects to the tree". Also I realized my mistake at the end of the post but didn't fix the mistake in the middle. :D Totally untested code and not to standards like mush + Show Spoiler +
| ||
Deleted User 3420
24492 Posts
and mush, that tutorial was very clear, thank you so much. the one thing in it I have questions about though is paintComponent and Graphics. So, I believe paintComponent is a method that components have that we are overriding here to specify what it should do But what is Graphics? Is it just some parameter that repaint is passing to paintComponent behind the scenes? What is the point of Graphics? Like, why doesn't paintComponent look like this: (i know this doesn't work, I just don't understand what the role of Graphics is)
| ||
mantequilla
Turkey775 Posts
On December 22 2016 02:15 phar wrote: The generic tech for converting a digital signal to audio is a DAC, and to sound it's, well, it's called a speaker, but you probably already knew that. Here's a simple ish guide: https://www.google.com/amp/www.instructables.com/id/Arduino-Basics-Making-Sound/?amp_page=true?client=ms-android-google Ahh fucking AMP links sorry. Let me know if I'm misunderstanding something about your question. noo noo I won't be making sound using arduino ![]() it will be like this microphone circuit sends analog signal to arduino arduino processes it if necessary and sends data over lan to my pc a program on my pc will play the sound I'm only asking about the first part (listening to sound). Rest I can figure out. | ||
RoomOfMush
1296 Posts
On December 22 2016 05:51 travis wrote: wow thanks guys and mush, that tutorial was very clear, thank you so much. the one thing in it I have questions about though is paintComponent and Graphics. So, I believe paintComponent is a method that components have that we are overriding here to specify what it should do But what is Graphics? Is it just some parameter that repaint is passing to paintComponent behind the scenes? What is the point of Graphics? Like, why doesn't paintComponent look like this: (i know this doesn't work, I just don't understand what the role of Graphics is) public void paintComponent() { this.setColor(blah blah) this.drawOval(blah blah) } Graphics is an interface used to render all kinds of graphics to screen. There can be different implementations of graphics which could even be implemented with native code; perhaps in C or C++. Conceptually a Graphics object is similar to an OpenGL context. It saves state like clipping, colors, blend modes, etc and offers methods of drawing primitive shapes or text. In Swing each component can call the repaint() method which will notify the EventDispatcherThread (EDT) that the component would like to be repainted. The EDT will then, at some point in the near future, call the paintComponent method of the component and pass a valid Graphics object for the parameter. The paintComponent method is not necessarily called once for every call to repaint. Its perfectly possible (and generally the case), that the EDT will ignore successive calls to repaint() before paintComponent has been called. How the Graphics object is constructed, whether its always the same, whether different Components will get the different Graphics objects or not, etc are implementation details that you should not care about. They may differ in future versions of Swing (although it is highly unlikely that Swing will ever get any major updates since JavaFX was just released). In general you should never save the Graphics object you get in paintComponent in an attribute. You should also never use the Graphics object outside of the paintComponent method for that particular component. Consider it to be temporary and only used within that one painting action. There is also an option of doing the usual while-true loop and rendering to the graphics context directly if you prefer the old-school style. You will lose much of Swings functionality if you do this though. | ||
Deleted User 3420
24492 Posts
Is the best way to do that to save my x and y coordinates into a 2d array, and then iterate through them in paintComponent to paint all the circles? (seems a bit awkward). | ||
RoomOfMush
1296 Posts
On December 22 2016 05:39 Blisse wrote: Yeah I kind of mixed up "memory leak" with "continually adding useless objects to the tree". Also I realized my mistake at the end of the post but didn't fix the mistake in the middle. :D Totally untested code and not to standards like mush + Show Spoiler +
A few suggestions: Instead of doing: circleLocation = new Point(e.getX(), e.getY()); you can do: circleLocation = e.getPoint(); I would highly recommend doing: window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); after creating your JFrame. The default value is JFrame.HIDE_ON_CLOSE which will not dispose the frame and not end any timers. This can turn out really bad when your JVM keeps running in the background without any visible frames. In that situation you would need to kill the process manually to end the program. The call to window.setVisible(true); should also be the very last thing you do in your code. If you do it this way you will never need to pack, validate, revalidate or invalidate your JFrame. The frame will automatically validate when it is set to visible. Instead of adding teh GraphGUI to your frame you can also set it to be the contentPane of the frame. The add-method will simply add your new JPanel to the current contentPane which means you have 2 panels but only one is really used. And remember to start Swing on the EventDispatcherThread (EDT) in your main-method. If you dont do it you will eventually run into BIG trouble when developing a larger application. I didnt do it once and I ran into random bugs that could never be reproduced but kept happening every now and then. It was usually graphical glitches or lost inputs. I later found out that I didnt run the application on the EDT and the errors were due to undefined behavior of Swing when not running on the EDT. You certainly dont want that to happen in later stages of developement. On December 22 2016 06:36 travis wrote: So what I want to do now is paint a new circle at each spot i click, but keep the old ones too. Is the best way to do that to save my x and y coordinates into a 2d array, and then iterate through them in paintComponent to paint all the circles? (seems a bit awkward). Dont use an array, use an ArrayList instead since it will automatically resize as needed. The iterating in paintComponent is the correct thing to do. | ||
| ||