| Author |
how to draw on Jwindow?
|
T.H. Ng
Greenhorn
Joined: Nov 12, 2010
Posts: 9
|
|
I'm starting to learn how to use GUI and this is my first Problem: The line that i want to draw never show up. What am I doing wrong?
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3044
|
|
|
You might want to add your panel to the window.
|
 |
T.H. Ng
Greenhorn
Joined: Nov 12, 2010
Posts: 9
|
|
hmm how can i do that? Sorry for the stupid question
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3044
|
|
window.add(this);
Have you ever worked with GUIs before? I suggest getting familiar with regular JFrames first. Check out the Swing tutorials: click
|
 |
T.H. Ng
Greenhorn
Joined: Nov 12, 2010
Posts: 9
|
|
|
i tried that as you said, but still no good.
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3044
|
|
|
Are you sure? Your current code only draws a single pixel. Try changing the arguments to the drawLine method to something like 50, 50, 100, 50.
|
 |
T.H. Ng
Greenhorn
Joined: Nov 12, 2010
Posts: 9
|
|
|
yh i have tried with JFrame, there is no problem so far, just cant understand whats happening here...
|
 |
T.H. Ng
Greenhorn
Joined: Nov 12, 2010
Posts: 9
|
|
|
I also tried with g.fillRect(50,50,50,50); but it still doesnt show up
|
 |
Darryl Burke
Bartender
Joined: May 03, 2008
Posts: 4163
|
|
|
You have been given a link to the Swing tutorials. Go through the section titled Performing Custom Painting, where you will find example code.
|
luck, db
There are no new questions, but there may be new answers.
|
 |
T.H. Ng
Greenhorn
Joined: Nov 12, 2010
Posts: 9
|
|
sorry i tried but all i found out is the component doesn's show up Can you please give me the solution for those codes? Thank you very much!
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3044
|
|
Your program (when modified to add the panel to the window) runs just fine for me.
Can you post your modified code?
|
 |
T.H. Ng
Greenhorn
Joined: Nov 12, 2010
Posts: 9
|
|
|
well i only add "window.add(this);" betweens line 20 and 21
|
 |
T.H. Ng
Greenhorn
Joined: Nov 12, 2010
Posts: 9
|
|
oh ic, i should put window.setVisible(true); before window.add(this); (i dont know why tho:P). Thank you all for helping me
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3044
|
|
No, you should add the panel *before* you show the window. This can be as early as right after you initialize window.
I don't know why it didn't work for you earlier (probably because your IDE needed to clean the build, or you forgot to compile or something), but it wasn't a problem with the code.
|
 |
T.H. Ng
Greenhorn
Joined: Nov 12, 2010
Posts: 9
|
|
now i understand problem solved! ty verymuch!
|
 |
 |
|
|
subject: how to draw on Jwindow?
|
|
|