Results 1 to 11 of 11
  1. #1
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default how to make a simple panel??

    hey hey everyone... im sitting here following a book on how to build java programs... and i have reached a point where im learning how to make a panel and draw lines inside... but i have a problem that eclipse that im using is telling me that the exampel from the book is wrong and i cant find out how to make it work.... this is the small class that should give me a panel to work with:

    import java.awt.*;

    public class DrawLine {
    public static void main(String[] args) {
    DrawingPanel panel = new DrawingPanel(200, 100);

    Graphics g = panel.getGraphics();
    g.drawLine(25, 75, 175, 25);
    }
    }


    but when i try to run this and get my panel it sayes :
    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    DrawingPanel cannot be resolved to a type
    DrawingPanel cannot be resolved to a type

    at DrawLine2.main(DrawLine2.java:6)


    can anyone pleas help me get this example to work???

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default

    Did you read what the compiler had to say? It can't find a definition of a type DrawingPanel. Have you defined it anywhere?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default

    not that i know of... but im only following what my book is telling me...

    how would the line look if i had to define it???

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by stefandanielsen View Post
    not that i know of... but im only following what my book is telling me...

    how would the line look if i had to define it???
    I don't know; I don't know what a DrawingPanel is either. Read your book some more; if they want to create compilable examples they have to supply the definition of each and every class/type they use ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    @ the OP.
    What book is it? And (like Jos asked) how is the DrawingPanel defined?
    I'm curious, but (or because) I just don't think it works that way. Normally you paint inside a paintComponent() method: that way "Swing" has the code at its disposal to repaint the image every time that's needed. Your method suggests painting a line once and then "the system" remembering "magically" how to do that again when needed.
    So: provide all the code to convince me/us.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Never use getGraphics() of a component. Learn how to correctly Perform Custom Painting.

    db

  7. #7
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default

    the book im reading is : Building Java Programs
    by: Stuart Reges & Marty Stepp
    page.213.

    and i cant provide you with any more code since that all the book says is what i wrote to begin with... i cant understand it my self so that is why im seeking help in here

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by stefandanielsen View Post
    the book im reading is : Building Java Programs
    by: Stuart Reges & Marty Stepp
    page.213.

    and i cant provide you with any more code since that all the book says is what i wrote to begin with... i cant understand it my self so that is why im seeking help in here
    Search in the table of contents or the index for 'DrawingPanel'; any book worth the tree it's made from should mention crucial information in a nicely ordered way.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    I'm flabbergasted!
    I found the code for DrawingPanel on this page: Building Java Programs: A Back to Basics Approach, by Stuart Reges and Marty Stepp
    ready for download.
    It works, but it is nothing like a standard DrawingPanel, and in my first impression not something to confront a beginner with. It is hundreds of lines of code, with timers, Buffered Images and other tricks I'll need the rest of the week to study.
    Not for normal mortals!
    Last edited by Jodokus; 05-17-2011 at 01:45 PM. Reason: spelling

  10. #10
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default

    cool that you found it... can you then tell me what i am doing wrong since mine issent working??

  11. #11
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    I don't know. I did nothing special. Just dump your code and their DrawingPanel in a package.
    I only just don't understand anyone using this kind of code in a beginnersbook. To speek with Jos, I can't imagine this book being worth the tree it's made of, but I didn't see the rest of course ;>)
    Let me know when you get it working (and your opinion of the book's approach?).
    By the way, the link of DarrylBurke is very good and tells most you need to know about painting!
    Last edited by Jodokus; 05-17-2011 at 02:33 PM. Reason: curious

Similar Threads

  1. Can it be that hard to make simple chat?
    By Ruuhkis in forum Networking
    Replies: 2
    Last Post: 02-25-2011, 05:13 PM
  2. panel on a panel not visible
    By warchieflll in forum Advanced Java
    Replies: 2
    Last Post: 01-29-2011, 08:29 PM
  3. Replies: 7
    Last Post: 09-24-2010, 05:09 AM
  4. How to make simple dicitonary J2ME
    By batosai_fk in forum CLDC and MIDP
    Replies: 5
    Last Post: 06-15-2010, 06:16 AM
  5. make simple web service
    By BigBear in forum Java Servlet
    Replies: 2
    Last Post: 06-13-2010, 12:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •