• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to draw on Jwindow?

 
T.H. Ng
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 6127
74
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to add your panel to the window.
 
T.H. Ng
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm how can i do that? Sorry for the stupid question
 
Stephan van Hulst
Bartender
Pie
Posts: 6127
74
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried that as you said, but still no good.
 
Stephan van Hulst
Bartender
Pie
Posts: 6127
74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yh i have tried with JFrame, there is no problem so far, just cant understand whats happening here...
 
T.H. Ng
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also tried with g.fillRect(50,50,50,50); but it still doesnt show up
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have been given a link to the Swing tutorials. Go through the section titled Performing Custom Painting, where you will find example code.
 
T.H. Ng
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 6127
74
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well i only add "window.add(this);" betweens line 20 and 21
 
T.H. Ng
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 6127
74
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
now i understand problem solved! ty verymuch!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic