Hello Everyone -
So I'm pretty new to the java language, but I've been coding in C/C++ and C# for awhile now. I know C# is basically a rip-off clone of java so it hasn't been to difficult to pick things up. The problem I am having is drawing 2D objects in a JPanel. I've read the differnt tutorials online about drawing stuff, basically extending from the JPanel and overwriting the paint() function, however when I use NetBeans to add in JPanels to the project it is just a JPanel and I don't see how I can overwritte the paint function.
With a little trial and error I've basically done something like this:
jPanel viewer;
public void SomeFunction() {
viewer.getGraphics().setColor(new Color(255, 0, 0));
viewer.getGraphics().fillRect(10, 0, 20, 10);
viewer.getGraphics().setColor(new Color(0, 0, 0));
viewer.getGraphics().drawString("Test", 10, 20);
}
Yes I do have the right imports included to do what I need. So I tried 2 things. I called this function in the main function and nothing happens. I then tried attaching a listener to the canvas on mouse click and I do see a flicker and after a couple of clicks it stays on the panel. The thing is the color for the rectangle is black and not red.
I'm pretty sure I'm doing things totally wrong. If anyone has used NetBeans GUI stuff I would love tutorials on doing it right and information on what I'm doing wrong.
1- Basically is it possible to overwrite the paint function for the JPanel through the NetBeans GUI application (I'm just dragging the panel into the
frame).
2- How do i get it so the canvas is always drawing so I don't have to do a listener on the click.
3- All the tutorials I've seen basically has you extending the JPanel and overwritting the paint function. Can I specifiy what the body of the paint function for the JPanel I dragged into the Frame without extending a new class?
I apologize if my wording is a bit wierd and confusing; I'm not sure myself if I am asking the write questions.
Thanks In Advance
Gatts79