Results 1 to 3 of 3
Thread: Strange extra shape drawn
- 10-25-2011, 10:34 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 2
- Rep Power
- 0
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
-
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.
- 10-25-2011, 11:18 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Reading extra /n on file.
By gicp89 in forum New To JavaReplies: 9Last Post: 02-08-2011, 12:48 PM -
Extra spaces in JLabel...Please help..
By sohamde in forum New To JavaReplies: 6Last Post: 12-06-2010, 03:53 AM -
how to change the shape of the JFrame to a oval shape
By kiki2009 in forum Java 2DReplies: 1Last Post: 04-02-2010, 12:48 PM -
Extra Sun Voucher available
By PriyaK in forum Reviews / AdvertisingReplies: 4Last Post: 01-15-2010, 06:41 AM -
Extra bracket
By CrazyShells Slam in forum New To JavaReplies: 5Last Post: 05-16-2008, 06:12 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks