• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Stuck on Head First Java Chapter 12

 
Greenhorn
Posts: 14
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello my Dear Friend,

I just created this account earlier, hope i will learn from some fantastic java developers

I am on chapter 12 of Head First Java Second edition


I wrote this code in my notepad file and saved it as MyDrawPanel.java
it coudn't compile

as far as i am concerned there are few reasons causing this problem
1) my program doesnt have main method, but even when i put the main method, it compiles just fine but DOES NOT PRINT WHAT I INSTRUCTED IT TO.
2) since i write that MyDrawPanel extends JPanel, it might means that i should write the JPanel class first because MyDrawPanel is just the subclass of JPanel. I hope you are getting me here.
i think i am not paying attention to the logic of super and subclass and how to run them
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need a JFrame object to hold all your Swing objects, including you MyDrawPanel. You don't need to create a JPanel, that comes with Swing. The class that either extends JFrame or instantiates a JFrame must include a main() method.

It's best to post a simple self contained compilable program here on this website (even if it's not working as desired), this will help you get the most responses.
 
Rancher
Posts: 3324
32
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In addition to Carey's comments, your painting code is wrong. All Swing components are responsible for painting their own background. This is normally done by the default implementation of the pa;intComponent() method. So you need to invoke the default painting code before adding your custom painting.

See the section from the Swing tutorial on Custom Painting for the most basic example.

Start with that example and make changes.
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

it coudn't compile
... reasons causing this problem
1) my program doesnt have main method,


A class does NOT need a main method to compile.  There shouldn't be any compiler errors for a class because it does not have a main method.

A class usually needs a main method to be executed with the java command.  There are a few exceptions.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Also use the Override annotation whenever you are overriding a method. Gie the paaintComponent() method protected access, like its superclass. Start the method with a call to its superclass version, like this. It causes the component to revert to its blank state.:-Don't call that method directly. I prefer not to extend JFrame myself:-
 
ImDinesh Sharma
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:You need a JFrame object to hold all your Swing objects, including you MyDrawPanel. You don't need to create a JPanel, that comes with Swing. The class that either extends JFrame or instantiates a JFrame must include a main() method.

It's best to post a simple self contained compilable program here on this website (even if it's not working as desired), this will help you get the most responses.



Thanks @Carey for the response. after reading your first few lines of the response here what i came up with



Now it compiles fine but NO OUTPUT
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No,yoiu won't get any ouutput. Please read what I showed you last night. If you don't add the panel to the frame or make the frame appear, nothing will happen. Also please read what I told you about paintComponent()
I see I made a mistake in line 15, writing Frame instead of frame.
Don't call your class jframe. First, class names should always begin with Capital Letters (upper‑case) and second, that is a misleading name. Call it MyGUI, or think of a better name.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

here what i came up with



That looks nothing like the code from the tutorial link I provided you.

Start with a working example.

It shows you how to:

1. create components on the Event Dsipatch Thread (EDT)
2. create a custom panel for painting
3. create the frame and add the panel to the frame

All you have to do is copy/paste/complile/test.

Then make your custom changes.
 
ImDinesh Sharma
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:If you don't add the panel to the frame or make the frame appear, nothing will happen.[/tt]



I SEE

Oh my word!!, how could anything appear if i do not code for showing the panel.
In main method, i need to create the JFrame with argument "MyDrawPanel"
then i need to write a vital code that causes the panel Appear right? i m talking about the the dimensions, EXIT_CLOSE instruction, setVisible

Also i need to create the Object of "MyDrawPanel" in order to be able to add it to the frame

here i am

 
Rob Camick
Rancher
Posts: 3324
32
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No that code is still wrong.

Why do you refuse to read the tutorial???

You learn by reading and modifying working examples.
 
ImDinesh Sharma
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Camick wrote:No that code is still wrong.



As i m going through the next steps in book, i think i am improving on the logic. The code i just showed you is working just fine and i am getting the Y's and How's. I also did some experiments and it worked.

I went through the tutorial but it consists some new stuff that i must know in the first place. or you can also say that I am not getting it THE WAY IT EXPLAINED

I still believe that you are giving me a good advice though that's y i am curious to know what's wrong with my code?
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:


See line 126.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i am curious to know what's wrong with my code?



Did you not compare your code with the working code to see what the differences are?

I already told you two of the differences:

1. The components are not created on the Event Dispatch Thread.
2. You custom painting class is not the same as the example. How many methods did you implement?. How many methods does the example implement?

If you have a question about the differences, then ask us, but don't just ignore the code because it is different from what you have.

 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ImDinesh,

as you can read, making a Swing application according to the rules is not an easy thing! It takes knowledge, experience, time and much practising.

As Rob pointed out, reading the tutorials and playing around with the examples given is a big part of the learning process. And as Campbell and Carey remarked, you should use as the first line of  your 'paintComponent(Graphics g)' the line: super.paintComponent(g). To help you making the learning process a tad easier, I've written a simple version of how you could set up your MyDrawPanel-program.

But first: why all the critique, given that your latest code does run flawlessly? To address two of them:

Your code does not run from the EDT
Indeed, it does not, yet it should. But it works nevertheless? Two years long, since I myself started to learn and use Swing, I had my code just like you have it. I started my Frames from the 'main' method, without any further ado, and it worked! And more, this was the code I read from some tutorials. But then came a man here at this site, who explained why it was wrong what we did. I will try to find a link to his writings, it is a couple of years ago. Now, at that time I didn't understand much of what the man told us, but it sure sounded mighty convincing!

You do not use 'super.paintComponent(g)'!
Indeed, you do not invoke that method. But believe me or not, in this case it is not necessary and can be left out! All that 'super.paintComponent(g)' does is clearing the panel and filling it completely with the background of the panel (you do not specify a background, so the default is used). Then your own user drawings are executed (in your case drawing the orange rectangle). Since from one update of the panel to another update of the panel nothing changes, you can indeed do without the clearing of the panel to its background color.
When you do, say, animations in your panel, then it is indeed necessary to clear the contents before drawing the next animation, for if you don't, you will see that these old drawings remain visible.

As said, this is not applicable to your MyDrawPanel and so you get away with it. But make it a habit of using this line of code, necessary or not.

Well, now for the code that I talked about. It shows one of the possibilities, but there are sure more ways to do this.



 
reply
    Bookmark Topic Watch Topic
  • New Topic