Results 1 to 3 of 3
  1. #1
    eyebrow is offline Member
    Join Date
    Oct 2011
    Rep Power

    Default Strange extra shape drawn

    The program below has a strange (to me) error.
    The program is supposed to ask the user for a circle's radius and then for a number of degrees.
    It will then draw the circle with a filled red arc using the degrees entered (along with some stat calculations that are shown as strings).
    The strange thing is that if you enter a radius larger than 35 or so and a number of degrees greater than 270, an extra bit of filled arc shows up at the top-left of the frame. It appears that a second, unasked for filled arc is being drawn at the frame's 'origin'.

    import java.awt.Color;
    import javax.swing.JFrame;
    import java.awt.Graphics;
    import javax.swing.JOptionPane;

    public class P02 extends JFrame {

    // Declare global constants
    private static final int FRAME_SIZE = 400;
    // Declare class level variables
    int halfFrameSize;
    int radiusOfCircle;
    int degreeOfAngle;
    int fillArcX;
    int fillArcY;
    double radianOfAngle;
    double arcLength;
    double areaOfSlice;
    int diameterOfCircle;

    public void paint(Graphics canvas) {

    // Math for canvas output.
    fillArcX = halfFrameSize - radiusOfCircle;
    fillArcY = halfFrameSize - radiusOfCircle;
    halfFrameSize = FRAME_SIZE / 2;
    diameterOfCircle = radiusOfCircle * 2;
    radianOfAngle = (degreeOfAngle * (Math.PI / 180));
    arcLength = radianOfAngle * radiusOfCircle;
    areaOfSlice = Math.pow(radiusOfCircle,2) * 0.5 * radianOfAngle;

    // Draws circle with radius input from user.
    diameterOfCircle, diameterOfCircle);

    // Draws X and Y axes
    canvas.drawLine(halfFrameSize, 0, halfFrameSize, FRAME_SIZE);
    canvas.drawLine(0, halfFrameSize, FRAME_SIZE, halfFrameSize);

    // Draws output strings
    canvas.drawString(("Radius: " + radiusOfCircle),5,65);
    canvas.drawString(("Angle Degrees: " + degreeOfAngle),5,80);
    canvas.drawString("Angle Radians: " + String.format("%1.3f",
    canvas.drawString("Arc Length: " + String.format("%1.3f",
    canvas.drawString("Slice Area: " + String.format("%1.3f",

    // Fills in arc on inside of circle using radius and degree input from
    // user.
    canvas.fillArc(fillArcX,fillArcY,radiusOfCircle * 2,radiusOfCircle * 2,
    }// end paint

    // The P02 method sets up the frame (window) size and makes sure the frame
    // is erased properly when the program is closed.
    public P02() {
    // JOptionPanes asking for user input.
    String radiusString = JOptionPane.showInputDialog(
    "Enter the radius of the circle (10..150)");
    radiusOfCircle = Integer.parseInt(radiusString);
    String angleString = JOptionPane.showInputDialog(
    "Enter the angle in degrees (0..360)");
    degreeOfAngle = Integer.parseInt(angleString);
    }// end P02

    // Creates the frame and makes it visible.
    public static void main(String[] args) {
    P02 guiWindow = new P02();
    }// end main
    }// end Jframe

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power

    Default Re: Strange extra shape drawn

    I don't get your problem but get other issues with your code. I recommend:
    • Draw in the paintComponent method of a class that extends JPanel, and then either place that JPanel in the JFrame's contentPane or set it as the JFrame's contentPane.
    • You usually want to call the class's super's paint or paintComponent method (depending on if it's a paint or paintComponent override method) as the first method call in the overridden method.
    • Be sure to call your Swing code on the event thread. This can be done by putting the two lines of code in your main method inside of a Runnable's run method and adding the Runnable to the event queue using SwingUtilities.invokeLater(...) method.

  3. #3
    eyebrow is offline Member
    Join Date
    Oct 2011
    Rep Power

    Default Re: Strange extra shape drawn

    Ok. Thanks for the feedback.
    When I called the super's paint, the problem went away. It looks like I'll have to read up on what exactly is going on, but your observation really helped.
    Thanks again,

Similar Threads

  1. Reading extra /n on file.
    By gicp89 in forum New To Java
    Replies: 9
    Last Post: 02-08-2011, 12:48 PM
  2. Extra spaces in JLabel...Please help..
    By sohamde in forum New To Java
    Replies: 6
    Last Post: 12-06-2010, 03:53 AM
  3. Replies: 1
    Last Post: 04-02-2010, 12:48 PM
  4. Extra Sun Voucher available
    By PriyaK in forum Reviews / Advertising
    Replies: 4
    Last Post: 01-15-2010, 06:41 AM
  5. Extra bracket
    By CrazyShells Slam in forum New To Java
    Replies: 5
    Last Post: 05-16-2008, 06:12 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