Results 1 to 7 of 7

Thread: Simple Graphics

  1. #1
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Simple Graphics

    Ok. So I have decided that taking a step back is better than trying to go forward blind. I am trying to come to an understanding of how java draws simple shapes, but continue to run into problems. I have now gone ahead and directly copied code from a book just to see if it will work. Alas, it does not. The code is just supposed to make a smiley face, but when I run it I get a bunch of errors. If anyone has any suggestions, absolutely any insights or ideas, I would really appreciate it. I have attached the code that I am trying to run and the error report at the bottom.

    Java Code:
    import javax.swing.JFrame;
    
    public class DrawSmileyTest
    {
      public static void main( String[] args );
        
        DrawSmiley panel = new DrawSmiley();
        JFrame application = new JFrame();
        
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.add( panel );
        application.setSize( 230, 250 );
        application.setVisible( true );
    }
    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JPanel;    
    
    public class DrawSmiley extends JPanel
    {
      public void paintComponent( Graphics g )
      {
        super.paintComponent( g );
        g.setColor( Color.YELLOW );
        g.fillOval( 10, 10, 200, 200 );
        g.setColor( Color.BLACK );
        g.fillOval( 55, 65, 30, 30 );
        g.fillOval( 135, 65, 30, 30 );
        g.fillOval( 50, 110, 120, 60 );
        g.setColor( Color.YELLOW );
        g.fillRect( 50, 110, 120, 30 );
        g.fillOval( 50, 120, 120, 40 );
      }
    }

    9 errors found:
    File: /Users/brad/Documents/Courses/Mobile App Development/Java Intermediate/Assignments/Assignment 1/DrawSmileyTest.java [line: 10]
    Error: /Users/brad/Documents/Courses/Mobile App Development/Java Intermediate/Assignments/Assignment 1/DrawSmileyTest.java:10: <identifier> expected
    File: /Users/brad/Documents/Courses/Mobile App Development/Java Intermediate/Assignments/Assignment 1/DrawSmileyTest.java [line: 10]
    Error: /Users/brad/Documents/Courses/Mobile App Development/Java Intermediate/Assignments/Assignment 1/DrawSmileyTest.java:10: <identifier> expected
    File: /Users/brad/Documents/Courses/Mobile App Development/Java Intermediate/Assignments/Assignment 1/DrawSmileyTest.java [line: 11]
    Error: /Users/brad/Documents/Courses/Mobile App Development/Java Intermediate/Assignments/Assignment 1/DrawSmileyTest.java:11: <identifier> expected
    File: /Users/brad/Documents/Courses/Mobile App Development/Java Intermediate/Assignments/Assignment 1/DrawSmileyTest.java [line: 11]
    Error: /Users/brad/Documents/Courses/Mobile App Development/Java Intermediate/Assignments/Assignment 1/DrawSmileyTest.java:11: <identifier> expected
    File: /Users/brad/Documents/Courses/Mobile App Development/Java Intermediate/Assignments/Assignment 1/DrawSmileyTest.java [line: 12]
    Error: /Users/brad/Documents/Courses/Mobile App Development/Java Intermediate/Assignments/Assignment 1/DrawSmileyTest.java:12: <identifier> expected
    File: /Users/brad/Documents/Courses/Mobile App Development/Java Intermediate/Assignments/Assignment 1/DrawSmileyTest.java [line: 12]
    Error: /Users/brad/Documents/Courses/Mobile App Development/Java Intermediate/Assignments/Assignment 1/DrawSmileyTest.java:12: illegal start of type
    File: /Users/brad/Documents/Courses/Mobile App Development/Java Intermediate/Assignments/Assignment 1/DrawSmileyTest.java [line: 12]
    Error: /Users/brad/Documents/Courses/Mobile App Development/Java Intermediate/Assignments/Assignment 1/DrawSmileyTest.java:12: illegal start of type
    File: /Users/brad/Documents/Courses/Mobile App Development/Java Intermediate/Assignments/Assignment 1/DrawSmileyTest.java [line: 13]
    Error: /Users/brad/Documents/Courses/Mobile App Development/Java Intermediate/Assignments/Assignment 1/DrawSmileyTest.java:13: <identifier> expected
    File: /Users/brad/Documents/Courses/Mobile App Development/Java Intermediate/Assignments/Assignment 1/DrawSmileyTest.java [line: 13]
    Error: /Users/brad/Documents/Courses/Mobile App Development/Java Intermediate/Assignments/Assignment 1/DrawSmileyTest.java:13: illegal start of type

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,030
    Rep Power
    6

    Default Re: Simple Graphics

    Hmm. If you copied this exactly, then the book is wrong. A bad semicolon at the end of your main method declaration.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: Simple Graphics

    Damn. Sorry for wasting your time. I was actually hoping that there was something more.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,030
    Rep Power
    6

    Default Re: Simple Graphics

    I actually like my smiley better. Paste this into the paintComponent.

    Java Code:
    super.paintComponent(g);                                    
    Graphics2D g2d = (Graphics2D) g;                            
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,       
        RenderingHints.VALUE_ANTIALIAS_ON);                   
    g2d.setColor(Color.YELLOW);                                 
    g2d.fillOval(10, 10, 200, 200);                             
    g2d.setColor(Color.BLACK);                                  
    g2d.setStroke(new BasicStroke(3));                          
    g2d.drawOval(10, 10, 200, 200);                             
    g2d.fillOval(65, 60, 20, 30);                               
    g2d.fillOval(135, 60, 20, 30);                              
    g2d.drawArc(60, 70, 100, 100, -15, -150);
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,358
    Rep Power
    6

    Default Re: Simple Graphics

    Quote Originally Posted by jim829 View Post
    Hmm. If you copied this exactly, then the book is wrong.
    I wish I could say that a book containing bad code is a rare occurrence. Alas, it is more a rule than an exception. But I actually don't mind that at all; it is a test to see if you understood what the book was trying to teach you or not. So...

    Ok. So I have decided that taking a step back is better than trying to go forward blind
    Very good! But you're not committing to it yet.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: Simple Graphics

    Ok, so first my apologies to all of the people here who have been kind enough to help me.

    Now I am able to create some simple shapes, and I am able to run a program. What I need to do is run two things. The first that I need to do is have a dialogue box present some information, which works just fine. But I then need to have a new frame draw some simple shapes. The problem is that I cannot seem to figure out how to instantiate the draw method located in a separate class from the main methods class. I do not know if this makes sense or not.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,030
    Rep Power
    6

    Default Re: Simple Graphics

    You invoke the draw method in the paintComponent method which is then called by the JRE when you issue invoke repaint() on your frame or panel. You may create any methods to assist in the painting process but those methods would be called from the paintComponent() method too. Check out the Oracle tutorials in my signature for many examples of painting. It also covers dialogue boxes and just about anything else you would like to understand.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Centering graphics on the graphics window
    By janyc71876 in forum New To Java
    Replies: 5
    Last Post: 11-25-2013, 08:25 PM
  2. Simple Graphics program not working.
    By forms in forum New To Java
    Replies: 7
    Last Post: 03-11-2012, 04:44 PM
  3. Replies: 4
    Last Post: 12-03-2011, 05:38 PM
  4. Drawing simple graphics in NetBeans
    By arifin in forum AWT / Swing
    Replies: 1
    Last Post: 11-12-2010, 11:01 AM
  5. Simple Graphics
    By Freakzoyd in forum New To Java
    Replies: 6
    Last Post: 10-05-2010, 03:47 AM

Posting Permissions

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