• 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

Head First Java Page 479 SimpleGui3

 
kevin Abel
Ranch Foreman
Posts: 906
8
  • Number of slices to send:
    Optional 'thank-you' note:
I copied the code from the on line examples.  
I don't know what this error means:
MyDrawPanel drawPanel = new MyDrawPanel();  //<-- Cannot resolve symbol 'MyDrawPanel'

This import looks grayed out.  i don't know why
import java.awt.*;

Thanks,
Kevin








 
Carey Brown
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:
You'd have to create your own MyDrawPanel class. Even if you did the code you have creates a new one but does nothing with it. Typically it will be added to another JPanel or JFrame.
 
kevin Abel
Ranch Foreman
Posts: 906
8
  • Number of slices to send:
    Optional 'thank-you' note:
Carey,

I looked more closely at the book.  It shows an outline of the MyDrawPanel Class at the bottom of the page.  It tells the user to go to page 365 for the code but its actually on Page 472.  I added this to the Errata sheet for the book.  

I will add the Class to the code and run it.   I'll review the code to see how it works.

Thanks,

Kevin
 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:

kevin Abel wrote:. . . It tells the user to go to page 365 . . .

Which edition of the book have yu got? It looks as if that page number had been copied from an older edition and not corrected. You were right to report an erratum.
Moving you to our GUIs forum.
 
kevin Abel
Ranch Foreman
Posts: 906
8
  • Number of slices to send:
    Optional 'thank-you' note:
Campbell,
I have the Third edition
Covers Java 8-17

This thread will make more sense in the GUI area.  

The GUI chapter has been fun to read.  I don't understand it completely.  Maybe once I get the code to work I can look over the code some more.   This is my latest attempt.  It draws an empty window without any objects on it.   I put a break in the method after it uses Random to pick colors.  I hover over the variables that should have values.  Nothing shows on the hover.  Maybe I'm using debug wrong in Intelij.






 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:
Afraid there are various things about that code I don't like, apart from the lines too long.
Move the {...} so the DrawPanel class is entirely inside the GUI class, and give it private access. That is a minor point, but it means you can have a class with the same name elsewhere without causing yourself any confusion.
More serious: I don't like to see display classes implement XYZListener interfaces. Sometimes such interfaces are unavoidable and can be implemented correctly like that, but I think not an action listener. Delete the implements clause, and write the implementation inside the method call in line 21. Fortunately, action listener is a functional interface, even though the documentation doesn't mention it, so you can reduce that listener to a λ:-Less serious: Preface the paintComponent() with the @Override annotation, and change its access to protected. You should never call that method, but leave the JVM to call it. Insert the following as the first line of paintComponent() whenever you override it:- super.paintComponent(g); This will cause the panel to revert to its original appearance (like its superclass) so you don't end up with multiple pictures painted on top of one another.
The idea is, when you click the button, an event object (actually an ActionEvent) is sent from the button to its enclosing components, to see whether there is anything that can listen for it. If nothing is listening, the event keeps going until it reaches the (I think, not certain) ?glass pane? of its top‑level component, and if it still hasn't been listened for, it vanishes into some sort of cyber‑limbo never to be seen again.
But you have written something to listen for that event. I think you should change where it is listened for to the button, and remove the actionPerformed() method, but that won't make much difference in this simple case. The way I wrote it, the button will listen for, and intercept the event, and then fire the actionPerformed() and that calls repaint(). Now, repaint() calls all sorts of methods including paintComponent() on every component in sight, and in the case of the DrawPanel, it causes the coloured oval to be painted anew in a different colour. . . . or it would have if you had remembered to add the button and panel to the frame!
 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:
You can also delete the actionPerformed() method because it is called from the button only.
 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:
 
kevin Abel
Ranch Foreman
Posts: 906
8
  • Number of slices to send:
    Optional 'thank-you' note:
Campbell,

I always appreciate the time and effort you put into answering questions and improving code.  

I learning some main ideas in  the current GUI chapter.  

I didn't write any of the code my self.   I copied it from the book and the repository.  The incorrect page made it difficult to tell what code to use.  

Is the information you gave me needed to make it work or is it for improvements?  

The book mentions to ask questions on Code Ranch which I usually do before posting errata information.  

I have posted at least 16 errata's already.   A lot of the erratas from others don't look as if they were fixed for the May 22 Printing history.

This code example is not working for me.  I wonder if others also got lost.

Do you or someone else communicate with the authors?   Can you give them your input to make this better for the next edition?

I mostly want to learn Java, but I also have a QA mind and don't like to let things go.  I have even challenged the NYC DMV on a written test question.

I'll copy your code into Intellij and get familiar with it.

Thanks,

Kevin

 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:

kevin Abel wrote:Campbell,

I always appreciate . . .

That's a pleasure

Is the information you gave me needed to make it work or is it for improvements? . . .

Both.

This code example is not working for me. . . .

As long as it isn't one of the few incomplete code examples in HFJ; the Party class, for example, is incomplete.

I'll copy your code into Intellij and get familiar with it.

Try copying it onto JShell, but you will then have to pass SimpleGui3.main(null); (or similar) to get it to work.

Thanks,

Kevin

 
kevin Abel
Ranch Foreman
Posts: 906
8
  • Number of slices to send:
    Optional 'thank-you' note:
I see JShell on my laptop.  I don't remember if I installed it or if it comes with Java.

I read about the tool and watched an introduction video

https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/Java-JShell-Online-Commands-How-to-Tutorial-Exit

The important commands I now know are:

/open

/imports

/exit

The example in the video shows the name of the file with .JSH at the end

/open game.jsh

I'd like to run the code you supplied in JShell.  Do I put your code in a text file and give it a .JSH extension?

Jshell doesn't need the main method.  I don't know if I have to remove it or it is optional.

Thank you,

Kevin
 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:

kevin Abel wrote:. . . JShell . . . I don't remember if I installed it or if it comes with Java.

It has come with all versions of Java® since 9.

. . . Do I put your code in a text file and give it a .JSH extension?

I never do that. I simply write (or copy'n'paste) the code directly onto the terminal.

. . . the main method.  I don't know if I have to remove it or it is optional. . . .

Optional. It is possible to call Foo.main(null); on JShell, but that is easier if you declare it as public static void main(String... args). You can then write Foo.main();
 
kevin Abel
Ranch Foreman
Posts: 906
8
  • Number of slices to send:
    Optional 'thank-you' note:
I pasted your code into Jshell.   I want to get it to run first and then look at how the GUI part works.  

Is there a way to clear memory in Jshell so I can run from the start.  I can't tell if it is remembering objects and variable between runs.

Campbell wrote:Optional. It is possible to call Foo.main(null); on JShell, but that is easier if you declare it as public static void main(String... args). You can then write Foo.main();



Do you mean to use this call to the main method instead of the way shown in your code?

I added two attachments to show what your code looks like in my Jshell editor.

Thanks,

Kevin
Jhshell-question-part-2.png
jshell-question-part-1.png
 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:
I am afraid I can't understand what is going on. The screenshots are (as screenshots so often are) difficult to understand. Did that code run? It ought to. You simply need to feed SimpleGui3.main(null); to JShell. As I said, the bits you missed out were adding the two Components to the Frame.
Please look at ActionListener and work out why it is a Functional Interface, even though it isn't flagged with that annotation. Hint: you do it by counting methods. Then go to the Java™ Tutorials and find out how I converted the body of the addActionListener() call to a λ expression.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic
vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |