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

    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'.
    Thanks,
    eyebrow.

    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;

    @Override
    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.
    canvas.drawOval(halfFrameSize-radiusOfCircle,
    halfFrameSize-radiusOfCircle,
    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(("Stats:"),5,50);
    canvas.drawString(("Radius: " + radiusOfCircle),5,65);
    canvas.drawString(("Angle Degrees: " + degreeOfAngle),5,80);
    canvas.drawString("Angle Radians: " + String.format("%1.3f",
    radianOfAngle),5,95);
    canvas.drawString("Arc Length: " + String.format("%1.3f",
    arcLength),5,110);
    canvas.drawString("Slice Area: " + String.format("%1.3f",
    areaOfSlice),5,125);

    // Fills in arc on inside of circle using radius and degree input from
    // user.
    canvas.setColor(Color.RED);
    canvas.fillArc(fillArcX,fillArcY,radiusOfCircle * 2,radiusOfCircle * 2,
    0,degreeOfAngle);
    }// 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() {
    setSize(FRAME_SIZE, FRAME_SIZE);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    // 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();
    guiWindow.setVisible(true);
    }// end main
    }// end Jframe

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    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
    Posts
    2
    Rep Power
    0

    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,
    eyebrow.

Similar Threads

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