coding UTF-8; // Load libraries. import GUI; import Graphics; import Graphics2D; import Color; import Math; // The size of the window. const int WINDOW_WIDTH = 450; const int WINDOW_HEIGHT = 570; // The size of the angle display. const int DISPLAY_WIDTH = 450; const int DISPLAY_HEIGHT = 410; const int DISPLAY_Y = 80; // The radius of the circle of the angle display. const int CIRCLE_RADIUS = 175; // The variables for storing IDs of GUI components. int window = NULL; int radianInputField = NULL; int degreeInputField = NULL; int radianToDegreeButton = NULL; int degreeToRadianButton = NULL; int radianOverPiLabel = NULL; int angleSlider = NULL; int displayLabel = NULL; // The variables for storing IDs of the 2D renderer and the graphics resource. int displayRenderer = NULL; int displayGraphics = NULL; // "main" function, which will be called automatically when the program is executed. void main() { // Initializes the window, and GUI components on it. initializeWindow(); // Draw the content of the angle display. updateDisplay(0.0); // Hide the console, because it is not necessary for GUI apps. hide(); } // Converts the angle in degrees into the angle in radians. // ----- // * The parameter "degree": The angle in degrees. float degreeToRadian(float degree) { // The conversion ratio of degrees into radians. float radianPerDegree = (2.0 * PI) / 360.0; // Compute the angle in radians, by multiplying the above ratio to the angle in degrees. float radian = radianPerDegree * degree; return radian; } // Converts the angle in radians into the angle in degrees. // ----- // * The parameter "radian": The angle in radians. float radianToDegree(float radian) { // The conversion ratio of radians into degrees. float degreePerRadian = 360.0 / (2.0 * PI); // Compute the angle in degrees, by multiplying the above ratio to the angle in radians. float degree = degreePerRadian * radian; return degree; } // Initializes the window, the GUI components, and so on. void initializeWindow() { // Create a window. The GUI components will be mounted on this window. window = newWindow(50, 50, WINDOW_WIDTH, WINDOW_HEIGHT, "Radian Converter"); // Create a text label, and put it above of the text field for inputting an angle in degrees. int degreeLabel = newTextLabel(10, 5, 140, 20, "Angle in Degrees:"); mountComponent(degreeLabel, window); // Create the text field for inputting an angle in degrees. degreeInputField = newTextField(10, 25, 140, 30, "0.0"); mountComponent(degreeInputField, window); // Create a text label, and put it above of the text field for inputting an angle in radians. int radianLabel = newTextLabel(280, 5, 140, 20, "Angle in Radians:"); mountComponent(radianLabel, window); // Create the text field for inputting an angle in radians. radianInputField = newTextField(280, 25, 140, 30, "0.0"); mountComponent(radianInputField, window); // Create a text label for displaying how much times the angle in radians is, than pi. radianOverPiLabel = newTextLabel(280, 55, 140, 15, "(0.0 π)"); mountComponent(radianOverPiLabel, window); // Create a button for performing the conversion of degrees into radians. degreeToRadianButton = newButton(190, 10, 50, 30, ">>"); mountComponent(degreeToRadianButton, window); // Create a button for performing the conversion of radians into degrees. radianToDegreeButton = newButton(190, 40, 50, 30, "<<"); mountComponent(radianToDegreeButton, window); // Create a 2D renderer and graphics resource, for drawing contents of the angle display. displayGraphics = newGraphics(); displayRenderer = newGraphics2DRenderer(DISPLAY_WIDTH, DISPLAY_HEIGHT, displayGraphics); // Create an image label of the angle display. displayLabel = newImageLabel(0, DISPLAY_Y, DISPLAY_WIDTH, DISPLAY_HEIGHT, displayGraphics); mountComponent(displayLabel, window); // Create a slider for changing the angle continuously. angleSlider = newHorizontalSlider(10, DISPLAY_Y + DISPLAY_HEIGHT + 10, WINDOW_WIDTH - 35, 30, 0, 0, 360); mountComponent(angleSlider, window); } // Updates/draws contents of the angle display. // ----- // * The parameter "degree": The angle in degrees. void updateDisplay(float degree) { // Convert the angle in decrees into the angle in radians. float radian = degreeToRadian(degree); // Compute the coord of the center of the circle, // the coord of the left-top of the circumscription rectangle of the circle, and so on. int circleCenterX = DISPLAY_WIDTH / 2; int circleCenterY = DISPLAY_HEIGHT / 2; int circleLeftTopX = circleCenterX - CIRCLE_RADIUS; int circleLeftTopY = circleCenterY - CIRCLE_RADIUS; int circleSize = CIRCLE_RADIUS * 2; // Compute the coord of the point on the circumference, corresponding with the specified angle. int anglePointX = round(circleCenterX + CIRCLE_RADIUS * cos(radian), 8, HALF_UP); int anglePointY = round(circleCenterY - CIRCLE_RADIUS * sin(radian), 8, HALF_UP); // Clear the current content of the angle display. setGraphics2DColor(displayRenderer, WHITE); clearGraphics2D(displayRenderer); // Draw the circle. setDrawColor(displayRenderer, BLACK); drawEllipse(displayRenderer, circleLeftTopX, circleLeftTopY, circleSize, circleSize, false); // Draw two lines from the center of the circle, // to points on the circumference corresponding with the specified angle and 0. drawLine(displayRenderer, circleCenterX, circleCenterY, circleCenterX + CIRCLE_RADIUS, circleCenterY); drawLine(displayRenderer, circleCenterX, circleCenterY, anglePointX, anglePointY); // Draw the small arc between the above two lines. float arcR = 40; for (float arcBeginRadian=0.0; arcBeginRadian < radian; arcBeginRadian += 0.03) { float arcEndRadian = arcBeginRadian + 0.03; if (radian < arcEndRadian) { arcEndRadian = radian; } int arcBeginX = round(circleCenterX + arcR * cos(arcBeginRadian), 8, HALF_UP); int arcBeginY = round(circleCenterY - arcR * sin(arcBeginRadian), 8, HALF_UP); int arcEndX = round(circleCenterX + arcR * cos(arcEndRadian), 8, HALF_UP); int arcEndY = round(circleCenterY - arcR * sin(arcEndRadian), 8, HALF_UP); setDrawColor(displayRenderer, BLACK); drawLine(displayRenderer, arcBeginX, arcBeginY, arcEndX, arcEndY); } // Draw the point on the circumference, corresponding with the specified angle. setDrawColor(displayRenderer, RED); drawPoint(displayRenderer, anglePointX, anglePointY, 5, true); // Draw scale numbers on the circumference: 0, 90, 180, 270, and 360. setDrawColor(displayRenderer, BLACK); setDrawFontSize(displayRenderer, 16); drawText(displayRenderer, circleCenterX + CIRCLE_RADIUS + 10, circleCenterY + 5, "0"); drawText(displayRenderer, circleCenterX - 10, circleCenterY - CIRCLE_RADIUS - 10, "90"); drawText(displayRenderer, circleCenterX - CIRCLE_RADIUS - 40, circleCenterY + 5, "180"); drawText(displayRenderer, circleCenterX - 12, circleCenterY + CIRCLE_RADIUS + 22, "270"); // Repaint the image label of the angle display, and the window. paintComponent(displayLabel); paintComponent(window); } // The event handler function whicn will be called automaticaly when any buttons are clicked. // ----- // * The parameter "componentID": The ID of the clicked button. // * The parameter "buttonText": The label text of the clicked button. void onButtonClick(int componentID, string buttonText) { // If the clicked button is the button for converting the angle in degrees to the angle in radians: if (componentID == degreeToRadianButton) { // Check the inputted value or expression is valid. string degreeExpression = getComponentText(degreeInputField); if (!evaluable(degreeExpression, 0.0)) { alert("The incorrect value or expression is inputted to the angle in degrees."); return; } // Get the inputted value, or compute the value of the inputted expression. float degree = eval(degreeExpression, 0.0); // Convert the inputted angle in degrees into the angle in radians. float radian = degreeToRadian(degree); // Compute how much times the angle in radians is, than pi. float radianOverPi = radian / PI; // Update (repaint) the content of the angle display. updateDisplay(degree); // Round the computed values, and set them to the text fields/labels. radian = round(radian, 8, HALF_UP); radianOverPi = round(radianOverPi, 8, HALF_UP); setComponentText(radianInputField, (string)radian); setComponentText(radianOverPiLabel, "(" + radianOverPi + " π)"); return; } // If the clicked button is the button for converting the angle in radians to the angle in degrees: if (componentID == radianToDegreeButton) { // Check the inputted value or expression is valid. string radianExpression = getComponentText(radianInputField); if (!evaluable(radianExpression, 0.0)) { alert("The incorrect value or expression is inputted to the angle in radians."); return; } // Get the inputted value, or compute the value of the inputted expression. float radian = eval(radianExpression, 0.0); // Compute how much times the angle in radians is, than pi. float radianOverPi = radian / PI; // Convert the inputted angle in radians into the angle in degrees. float degree = radianToDegree(radian); // Update (repaint) the content of the angle display. updateDisplay(degree); // Round the computed values, and set them to the text fields/labels. degree = round(degree, 8, HALF_UP); radianOverPi = round(radianOverPi, 8, HALF_UP); setComponentText(degreeInputField, (string)degree); setComponentText(radianOverPiLabel, "(" + radianOverPi + " π)"); return; } } // The event handler function whicn will be called automaticaly when any sliders are moved. // ----- // * The parameter "componentID": The ID of the moved slider. // * The parameter "value": The current value of the moved slider. void onSliderMove(int componentID, int value) { // The slider in this program takes integer values from 0 to 360, // so regart the value as the angle in degrees. float degree = (float)value; // Convert the above value to the angle in radians. float radian = degreeToRadian(degree); // Compute how much times the angle in radians is, than pi. float radianOverPi = radian / PI; // Update (repaint) the content of the angle display. updateDisplay(degree); // Round the computed values, and set them to the text fields/labels. degree = round(degree, 8, HALF_UP); radian = round(radian, 8, HALF_UP); radianOverPi = round(radianOverPi, 8, HALF_UP); setComponentText(degreeInputField, (string)degree); setComponentText(radianInputField, (string)radian); setComponentText(radianOverPiLabel, "(" + radianOverPi + " π)"); } // The event handler function whicn will be called automaticaly when any windows are closed. // ----- // * The parameter "componentID": The ID of the closed window. void onWindowClose(int componentID) { // Terminate this program. exit(); }