Results 1 to 5 of 5
  1. #1
    flypadre is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default Problem with my cast

    I've been working through a java book and I am stuck as to why I am getting a compiler error on my code.

    Here is my code and the error msg I am getting.
    It appears to me that the compiler doesn't like my cast of Graphics to Graphics2D, but this how my book is telling me to do it, so I'm guessing my problem is somewhere else, I just don't know where.

    Thanks for any help.
    Sean

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class CustomGradientGui {
    
            public static void main(String[] args) {
                    CustomGradientGui gui = new CustomGradientGui();
                    gui.build();
            }
    
            public void build() {
                    JFrame frame = new JFrame();
                    MyCircle circle = new MyCircle();
    
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setSize(300,300);
                    frame.setVisible(true);
                    frame.getContentPane().add(circle);
            }
    }
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    class MyCircle extends JPanel {
            public void paintComponent(Graphics g) {
                    Graphics2D 2dg = (Graphics2D) g;
    
                    GradientPaint gradient = new GradientPaint(70,70, Color.blue, 150,150, Color.orange);
    
                    2dg.setPaint(gradient);
                    2dg.fillOval(70,70,100,100);
            }
    }
    ./MyCircle.java:7: not a statement
    Graphics2D 2dg = (Graphics2D) g;
    ^
    ./MyCircle.java:7: ';' expected
    Graphics2D 2dg = (Graphics2D) g;
    ^
    ./MyCircle.java:11: not a statement
    2dg.setPaint(gradient);
    ^
    ./MyCircle.java:11: ';' expected
    2dg.setPaint(gradient);
    ^
    ./MyCircle.java:12: not a statement
    2dg.fillOval(70,70,100,100);
    ^
    ./MyCircle.java:12: ';' expected
    2dg.fillOval(70,70,100,100);
    ^
    ./MyCircle.java:11: cannot find symbol
    symbol : method setPaint(java.awt.GradientPaint)
    location: class java.awt.Graphics
    2dg.setPaint(gradient);
    ^
    7 errors

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

    Default

    Variable names can't start with a number.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Refer here for more details: Variables

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

    Default

    Quote Originally Posted by goldest View Post
    Refer here for more details: Variables

    Goldest
    Or here: Java Naming Conventions
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    flypadre is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default

    Thanks that was it. I figured that it had to be something simple like that.

Similar Threads

  1. Cast object to custom class problem
    By trader5050 in forum New To Java
    Replies: 7
    Last Post: 11-15-2010, 11:47 AM
  2. Dynamic cast
    By Vizualni in forum AWT / Swing
    Replies: 2
    Last Post: 08-17-2010, 07:21 PM
  3. Cast int to char
    By Chasingxsuns in forum New To Java
    Replies: 1
    Last Post: 02-05-2010, 07:27 AM
  4. Cannot cast JNLPAppletLauncher to itself
    By kingofearth in forum Java Applets
    Replies: 0
    Last Post: 01-19-2010, 08:37 PM
  5. How can I cast Object as an int
    By romina in forum New To Java
    Replies: 1
    Last Post: 07-18-2007, 12:20 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
  •