Results 1 to 2 of 2
Thread: Help with images...
- 07-22-2007, 09:14 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 40
- Rep Power
- 0
Help with images...
I have a coding problem... and if someone can help me finish it.. it would be grateful...
I can't make the numbers to respond to the answers...... and follow the image says...
Code:
When i try to input the numbers into the textfield....Java Code:import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class TriangleArea extends Applet implements ActionListener { private Label lblInstructions, lblSideA, lblSideB, lblSideC, lblSelect, lblAngles, lblArea, lblRightAngle; private TextField txtSideA, txtSideB, txtSideC; private Button btnCM, btnM, btnKM; double[] angles = new double[3]; double radians; public void init() { setLayout(null); setupGUIComponents(); final int WIDTH = 470, HEIGHT = 220; setSize(WIDTH, HEIGHT); } public void actionPerformed(ActionEvent ae) { String command = ae.getActionCommand(); lblAngles.setText("Three angles are "+angles[0]+"degree, "+angles[1]+"degree and "+angles[2]+"degree"); lblArea.setText("Area of the triangle is "+command); lblRightAngle.setText("It is a "+command+" triangle"); } private void setupGUIComponents() { // initiaze the components lblInstructions = new Label("Enter the length of the sides of a triangle"); lblSideA = new Label("a"); txtSideA = new TextField(5); lblSideB = new Label("b"); txtSideB = new TextField(5); lblSideC = new Label("c"); txtSideC = new TextField(5); lblSelect = new Label("Select unit:"); btnCM = new Button("cm"); btnM = new Button("m"); btnKM = new Button("km"); lblAngles = new Label(); lblArea = new Label(); lblRightAngle = new Label(); // add action-listeners to the buttons btnCM.addActionListener(this); btnM.addActionListener(this); btnKM.addActionListener(this); // allign the components to the frame lblInstructions.setBounds(100,10,250,20); lblSideA.setBounds(100,35,20,20); txtSideA.setBounds(220,35,115,20); lblSideB.setBounds(100,55,20,20); txtSideB.setBounds(220,55,115,20); lblSideC.setBounds(100,75,20,20); txtSideC.setBounds(220,75,115,20); lblSelect.setBounds(130,100,70,20); btnCM.setBounds(210,100,30,20); btnM.setBounds(245,100,30,20); btnKM.setBounds(280,100,30,20); lblAngles.setBounds(20,140,410,20); lblArea.setBounds(20,165,410,20); lblRightAngle.setBounds(20,190,410,20); // add the components to the frame add(lblInstructions); add(lblSideA); add(txtSideA); add(lblSideB); add(txtSideB); add(lblSideC); add(txtSideC); add(lblSelect); add(btnCM); add(btnM); add(btnKM); add(lblAngles); add(lblArea); add(lblRightAngle); } private double[] getAngles(int a, int b, int c) { // create an array to store the 3 angles double[] angles = new double[3]; double radians; // cast all the int values to double double dA=(double)a, dB=(double)b, dC=(double)c; // angle A in radians = cos^-1 (b^2 + c^2 - a^2 / 2bc) // and then convert the radians to degrees radians = Math.acos((dB*dB + dC*dC - dA*dA) / (2.0*dB*dC)); angles[0] = radians * 180.0 / Math.PI; // angle B in radians = cos^-1 (a^2 + c^2 - b^2 / 2ac) // and then convert the radians to degrees radians = Math.acos((dA*dA + dC*dC - dB*dB) / (2.0*dA*dC)); angles[1] = radians * 180.0 / Math.PI; // angle C in degrees = 180 - degrees A - degrees B angles[2] = 180.0 - angles[0] - angles[1]; // return the 3 angles return angles; } }
No number came out to the output....
If anyway.... can help to complete the java code...??
Thanks
- 08-04-2007, 05:25 AM #2
Java Code:// <applet code="TA" width="400" height="400"></applet> import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.text.NumberFormat; public class TA extends Applet implements ActionListener { private Label lblSideA, lblSideB, lblSideC, lblAngles, lblArea, lblRightAngle; private TextField txtSideA, txtSideB, txtSideC; NumberFormat nf; public void init() { nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(2); setLayout(null); setupGUIComponents(); final int WIDTH = 470, HEIGHT = 220; setSize(WIDTH, HEIGHT); } public void actionPerformed(ActionEvent ae) { String command = ae.getActionCommand(); int a = Integer.parseInt(txtSideA.getText().trim()); int b = Integer.parseInt(txtSideB.getText().trim()); int c = Integer.parseInt(txtSideC.getText().trim()); double[] angles = getAngles(a, b, c); lblAngles.setText("Three angles are " + nf.format(angles[0]) + " degree, " + nf.format(angles[1]) + " degree and " + nf.format(angles[2]) + " degree"); lblArea.setText("Area of the triangle is " + nf.format(getArea(a, b, c)) + " " + command); String s = isRightTriangle(angles) ? "" : "not "; lblRightAngle.setText("It is " + s + "a right triangle"); } private void setupGUIComponents() { // initiaze the components Label lblInstructions = new Label("Enter the length of the sides of a triangle"); Label lblSideA = new Label("a"); txtSideA = new TextField(5); Label lblSideB = new Label("b"); txtSideB = new TextField(5); Label lblSideC = new Label("c"); txtSideC = new TextField(5); Label lblSelect = new Label("Select unit:"); Button btnCM = new Button("cm"); Button btnM = new Button("m"); Button btnKM = new Button("km"); lblAngles = new Label(); lblArea = new Label(); lblRightAngle = new Label(); // add action-listeners to the buttons btnCM.addActionListener(this); btnM.addActionListener(this); btnKM.addActionListener(this); // allign the components to the frame lblInstructions.setBounds(100,10,250,20); lblSideA.setBounds(100,35,20,20); txtSideA.setBounds(220,35,115,20); lblSideB.setBounds(100,55,20,20); txtSideB.setBounds(220,55,115,20); lblSideC.setBounds(100,75,20,20); txtSideC.setBounds(220,75,115,20); lblSelect.setBounds(130,100,70,20); btnCM.setBounds(210,100,30,20); btnM.setBounds(245,100,30,20); btnKM.setBounds(280,100,30,20); lblAngles.setBounds(20,140,410,20); lblArea.setBounds(20,165,410,20); lblRightAngle.setBounds(20,190,410,20); // add the components to the frame add(lblInstructions); add(lblSideA); add(txtSideA); add(lblSideB); add(txtSideB); add(lblSideC); add(txtSideC); add(lblSelect); add(btnCM); add(btnM); add(btnKM); add(lblAngles); add(lblArea); add(lblRightAngle); } private double[] getAngles(int a, int b, int c) { // create an array to store the 3 angles double[] angles = new double[3]; double radians; // cast all the int values to double double dA=(double)a, dB=(double)b, dC=(double)c; // angle A in radians = cos^-1 (b^2 + c^2 - a^2 / 2bc) // and then convert the radians to degrees radians = Math.acos((dB*dB + dC*dC - dA*dA) / (2.0*dB*dC)); angles[0] = radians * 180.0 / Math.PI; // angle B in radians = cos^-1 (a^2 + c^2 - b^2 / 2ac) // and then convert the radians to degrees radians = Math.acos((dA*dA + dC*dC - dB*dB) / (2.0*dA*dC)); angles[1] = radians * 180.0 / Math.PI; // angle C in degrees = 180 - degrees A - degrees B angles[2] = 180.0 - angles[0] - angles[1]; // return the 3 angles return angles; } private double getArea(int a, int b, int c) { // Heron's formula // s = (a + b + c)/2 // area = sqrt( s(s - a)(s - b)(s - c) ) double s = (a + b + c)/2.0; double area = Math.sqrt(s*(s-a)*(s-b)*(s-c)); return area; } private boolean isRightTriangle(double[] angles) { for(int j = 0; j < angles.length; j++) { if(Math.abs(angles[j] - 90.0) < 0.05) return true; } return false; } }
Similar Threads
-
Comparing Images
By shaungoater in forum Advanced JavaReplies: 0Last Post: 03-17-2008, 10:38 AM -
How to add Images to JPanels?
By Soda in forum New To JavaReplies: 3Last Post: 12-08-2007, 05:54 PM -
Images not displaying in JSP in IE7
By chadscc in forum Advanced JavaReplies: 0Last Post: 11-13-2007, 03:24 PM -
Help using images in Java
By toby in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:54 AM -
Images in JSP
By Daniel in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 06-05-2007, 06:01 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks