Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Shape error?

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Shape error?

    Can someone take a look at my code and see why my SetShape method is not taking in/reading my shapes. It's not taking in Circle, Rectangle, or Triangle, and I have no clue why. Thanks. What I mean is on line 72, aShape.equals(Circle); does not recognize Circle even though Circle passes through all previous methods above.


    import java.awt.*;
    2 import java.awt.event.MouseEvent;
    3 import java.awt.event.MouseListener;
    4 import java.awt.Color;
    5 import javax.swing.*;
    6
    7 public class CoolDrawPanel extends JPanel
    8 {
    9 //declare variables to use
    10 private int x = 0, y = 0; //trackers
    11 public static Shape aShape; //instance of a new shape
    12 public static Color aColor = Color.green; //instance of a new color
    13 public static Shape Rectangle = null, Triangle= null, Circle = null; //create the shapes Rectangle, Triangle, Circle
    14
    15 public CoolDrawPanel()
    16 {
    17 //add mouse listener
    18 addMouseListener (new CoordinatesListener());
    19
    20 //create an instance of the shapes
    21 Circle = new CircleShape();
    22 Rectangle = new RectangleShape();
    23 Triangle = new TriangleShape();
    24
    25 //set the window dimensions to 500, 500
    26 setPreferredSize (new Dimension(500, 500));
    27
    28 //set the background color to white
    29 setBackground (Color.white);
    30 }
    31
    32 //create methods to draw the selected shape on the panel
    33 public void paintComponent (Graphics g, Point q)
    34 {
    35 g.setColor (aColor); //sets the shape to the current color
    36 aShape.draw (aColor, g, q); //takes the shape, and sets the color at point q
    37 }
    38
    39 //create the mouse listener events
    40 private class CoordinatesListener implements MouseListener
    41 {
    42 //provides the definition for unused mouse events, must be declared
    43 public void mouseEntered(MouseEvent event)
    44 {}
    45 public void mouseExited(MouseEvent event)
    46 {}
    47 public void mouseClicked(MouseEvent event)
    48 {}
    49 public void mouseReleased(MouseEvent event)
    50 {}
    51
    52 //adds the current point to the list of points and redraws the shape whenever the mouse event occurs
    53 public void mousePressed (MouseEvent event)
    54 {
    55 x = event.getX();
    56 y = event.getY();
    57 repaint(); //repaints whenever the menu bar is used, if there are already shapes in the panel
    58 }
    59 }
    60
    61 //instantiates the color that the user chooses to the shape
    62 public static void setColor (Color color)
    63 {
    64 aColor = color;
    65 }
    66
    67 //instantiates the shape that the user chooses to the panel
    68 public static void SetShape (String shape)
    69 {
    70 if (shape.equalsIgnoreCase("Circle"))
    71 {
    72 aShape.equals(Circle);
    73 }
    74 if (shape.equalsIgnoreCase("Rectangle"))
    75 {
    76
    77 aShape.equals(Rectangle);
    78 }
    79 if (shape.equalsIgnoreCase("Triangle"))
    80 {
    81 aShape.equals(Triangle);
    82 }
    83 }
    84 }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Shape error?

    First, please use the code tags. Second, I don't understand the question - define 'does not recognize'. The line you point to does nothing (equals returns a boolean)...

Similar Threads

  1. Shape Calculation Program
    By TH1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 29th, 2011, 03:54 PM
  2. How to drag the shape to move?
    By ice in forum AWT / Java Swing
    Replies: 21
    Last Post: December 15th, 2010, 06:45 PM
  3. Replies: 1
    Last Post: October 16th, 2010, 03:32 PM
  4. how do i draw a shape with nested loops?
    By Kilowog in forum Loops & Control Statements
    Replies: 1
    Last Post: September 25th, 2009, 12:14 AM