Results 1 to 6 of 6
- 10-19-2009, 03:32 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 6
- Rep Power
- 0
Help with calculating triangle program
I have a program that I am given the x and y coordinates and side lengths and need to calculate the perimeter, area, and angles of a triangle. I have written the calculations for the perimeter, area, and angles in the triangle code:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Line2D;
import javax.swing.JComponent;
public class Triangle extends JComponent implements MouseListener{
private double x1 = 0, x2 = 0, x3 = 0, y1 = 0, y2 = 0, y3 = 0;
private int i = 0;
private double side1, side2, side3;
private double perimeter;
private double area;
private double angle1, angle2, angle3;
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
if (i % 3 == 1) {
} else if (i % 3 == 2) {
Line2D.Double line1 = new Line2D.Double(x2,y1,x2,y2);
g2.setColor(Color.RED);
g2.draw(line1);
} else if (i % 3 == 0) {
Line2D.Double line1 = new Line2D.Double(x1,y1,x2,y2);
g2.setColor(Color.RED);
g2.draw(line1);
Line2D.Double line2 = new Line2D.Double(x2,y2,x3,y3);
g2.setColor(Color.RED);
g2.draw(line2);
Line2D.Double line3 = new Line2D.Double(x1,y1,x3,y3);
g2.setColor(Color.RED);
g2.draw(line3);
drawInformation(g2);
}
}
public void mouseClicked(MouseEvent e) {
if (i % 3 == 0){
x1 = e.getX();
y1 = e.getY();
} else if (i % 3 == 1){
x2 = e.getX();
y2 = e.getY();
} else if (i % 3 == 2){
x3 = e.getX();
y3 = e.getY();
}
i++;
repaint();
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void drawInformation(Graphics2D g2){
g2.setColor(Color.BLUE);
g2.drawString("<x and y coordinates>", 5,50);
g2.drawString("(x1,y1): (" + Double.toString(x1) + ", " + Double.toString(y1) + ")", 5,75);
g2.drawString("(x2,y2): (" + Double.toString(x2) + ", " + Double.toString(y2) + ")", 5,100);
g2.drawString("(x3,y3): (" + Double.toString(x3) + ", " + Double.toString(y3) + ")", 5,125);
g2.setColor(Color.BLACK);
calculateSideLength();
g2.drawString("<Side lengths>", 5, 150);
g2.drawString("Side 1: " + Double.toString(side1),5,175);
g2.drawString("Side 2: " + Double.toString(side2),5,200);
g2.drawString("Side 3: " + Double.toString(side3),5,225);
g2.setColor(Color.MAGENTA);
g2.setColor(Color.GREEN);
g2.setColor(Color.RED);
}
public void calculateSideLength(){
side1 = Math.sqrt((x2-x1)*(x2-x1)*(y2-y1)*(y2-y1));
side2 = Math.sqrt((x3-x2)*(x3-x2)*(y3-y2)*(y3-y2));
side3 = Math.sqrt((x3-x1)*(x3-x1)*(y3-y1)*(y3-y1));
}
public void calculateTrianglePerimeter(){
perimeter = side1+side2+side3;
}
public void calculateTriangleArea()
{
double p = (side1+side2+side3)/2;
area = Math.sqrt(p*(p-side1)*(p-side2)*(p-side3));
}
public void calculateAngles(){
angle1 = Math.acos((side2*side2+side3*side3-side1*side1)/2*side2*side3);
angle2 = Math.acos((side1*side1+side3*side3-side2*side2)/2*side1*side3);
angle3 = Math.acos((side1*side1+side2*side2-side3*side3)/2*side1*side2);
}
Now I need to make the code compute and print the perimeter, area, and angles in the TriangleTester code. This is what I have so far:
import javax.swing.JFrame;
public class TriangleTester {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new JFrame();
frame.setSize(800,600);
frame.setTitle("Triangle");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
Triangle component = new Triangle();
frame.add(component);
frame.setVisible(true);
component.addMouseListener(component);
}
If anyone has any ideas that can help me I would really appreciate it. Thanks!
-
Two problems here:
1) Your post doesn't use code tags making your code near impossible to read.
2) You never really ask a direct specific question, so none of us really know what help you need.
edit, sorry to be picky, but also:
3) You never replied to Masijade's 2nd helpful reply in this thread either: need help with implementing class
Much luck.Last edited by Fubarable; 10-19-2009 at 03:38 AM.
- 10-19-2009, 03:43 AM #3
Member
- Join Date
- Oct 2009
- Posts
- 6
- Rep Power
- 0
I have a program that I am given the x and y coordinates and side lengths and need to calculate the perimeter, area, and angles of a triangle. I have written the calculations for the perimeter, area, and angles in the triangle code:
What code would I put in the TriangleTester class to make the perimeter, area, and angles print when I run the program? This is the TriangleTester I have right now I just need to know what to add to it to print the calculations.Java Code:import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.geom.Line2D; import javax.swing.JComponent; public class Triangle extends JComponent implements MouseListener{ private double x1 = 0, x2 = 0, x3 = 0, y1 = 0, y2 = 0, y3 = 0; private int i = 0; private double side1, side2, side3; private double perimeter; private double area; private double angle1, angle2, angle3; public void paintComponent(Graphics g){ Graphics2D g2 = (Graphics2D) g; if (i % 3 == 1) { } else if (i % 3 == 2) { Line2D.Double line1 = new Line2D.Double(x2,y1,x2,y2); g2.setColor(Color.RED); g2.draw(line1); } else if (i % 3 == 0) { Line2D.Double line1 = new Line2D.Double(x1,y1,x2,y2); g2.setColor(Color.RED); g2.draw(line1); Line2D.Double line2 = new Line2D.Double(x2,y2,x3,y3); g2.setColor(Color.RED); g2.draw(line2); Line2D.Double line3 = new Line2D.Double(x1,y1,x3,y3); g2.setColor(Color.RED); g2.draw(line3); drawInformation(g2); } } public void mouseClicked(MouseEvent e) { if (i % 3 == 0){ x1 = e.getX(); y1 = e.getY(); } else if (i % 3 == 1){ x2 = e.getX(); y2 = e.getY(); } else if (i % 3 == 2){ x3 = e.getX(); y3 = e.getY(); } i++; repaint(); } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void drawInformation(Graphics2D g2){ g2.setColor(Color.BLUE); g2.drawString("<x and y coordinates>", 5,50); g2.drawString("(x1,y1): (" + Double.toString(x1) + ", " + Double.toString(y1) + ")", 5,75); g2.drawString("(x2,y2): (" + Double.toString(x2) + ", " + Double.toString(y2) + ")", 5,100); g2.drawString("(x3,y3): (" + Double.toString(x3) + ", " + Double.toString(y3) + ")", 5,125); g2.setColor(Color.BLACK); calculateSideLength(); g2.drawString("<Side lengths>", 5, 150); g2.drawString("Side 1: " + Double.toString(side1),5,175); g2.drawString("Side 2: " + Double.toString(side2),5,200); g2.drawString("Side 3: " + Double.toString(side3),5,225); g2.setColor(Color.MAGENTA); g2.setColor(Color.GREEN); g2.setColor(Color.RED); } public void calculateSideLength(){ side1 = Math.sqrt((x2-x1)*(x2-x1)*(y2-y1)*(y2-y1)); side2 = Math.sqrt((x3-x2)*(x3-x2)*(y3-y2)*(y3-y2)); side3 = Math.sqrt((x3-x1)*(x3-x1)*(y3-y1)*(y3-y1)); } public void calculateTrianglePerimeter(){ perimeter = side1+side2+side3; } public void calculateTriangleArea() { double p = (side1+side2+side3)/2; area = Math.sqrt(p*(p-side1)*(p-side2)*(p-side3)); } public void calculateAngles(){ angle1 = Math.acos((side2*side2+side3*side3-side1*side1)/2*side2*side3); angle2 = Math.acos((side1*side1+side3*side3-side2*side2)/2*side1*side3); angle3 = Math.acos((side1*side1+side2*side2-side3*side3)/2*side1*side2); } }
I'm sorry I'm new to this but I'm learning. Thank you for your help and patience.Java Code:import javax.swing.JFrame; public class TriangleTester { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub JFrame frame = new JFrame(); frame.setSize(800,600); frame.setTitle("Triangle"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E); Triangle component = new Triangle(); frame.add(component); frame.setVisible(true); component.addMouseListener(component); } }Last edited by ALH813; 10-19-2009 at 04:00 AM. Reason: I did not post correctly the first time. sorry
-
Please have a look at my signature as it tells you how code tags work. If it's still confusing, then my signature has a link that you can click for further information (the FAQ). Also, you don't have to repost anything as you can edit the posts that you already have made. Also, make sure that the code you paste is well formatted to begin with as code tags aren't magical and they can't format code that has lost its format (as if i need to even say this as it should be obvious).
Much luck.
Edit:
Oops. You posted unformatted code. Ah well, it seemed obvious at least to me. I still can't read your code, but perhaps someone else can. Much luck.Last edited by Fubarable; 10-19-2009 at 04:10 AM.
- 10-19-2009, 04:34 AM #5
Member
- Join Date
- Oct 2009
- Posts
- 6
- Rep Power
- 0
Sorry, I tried to post it correctly. Thank you for all your help and patience. I do apologize for not thanking the other post, I tried to send a message but it wouldn't allow me to. I really do appreciate all the help you give. Thank you again
-
Still it is not formatted, and it is very difficult to read. Note the difference:
I suggest that you just extend what you've done before: draw the Strings necessary to display the program state information in the drawInformation method. Just extend it to allow other data.Java Code:import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.geom.Line2D; import javax.swing.JComponent; public class Triangle extends JComponent implements MouseListener { private double x1 = 0, x2 = 0, x3 = 0, y1 = 0, y2 = 0, y3 = 0; private int i = 0; private double side1, side2, side3; private double perimeter; private double area; private double angle1, angle2, angle3; public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; if (i % 3 == 1) { } else if (i % 3 == 2) { Line2D.Double line1 = new Line2D.Double(x2, y1, x2, y2); g2.setColor(Color.RED); g2.draw(line1); } else if (i % 3 == 0) { Line2D.Double line1 = new Line2D.Double(x1, y1, x2, y2); g2.setColor(Color.RED); g2.draw(line1); Line2D.Double line2 = new Line2D.Double(x2, y2, x3, y3); g2.setColor(Color.RED); g2.draw(line2); Line2D.Double line3 = new Line2D.Double(x1, y1, x3, y3); g2.setColor(Color.RED); g2.draw(line3); drawInformation(g2); } } public void mouseClicked(MouseEvent e) { if (i % 3 == 0) { x1 = e.getX(); y1 = e.getY(); } else if (i % 3 == 1) { x2 = e.getX(); y2 = e.getY(); } else if (i % 3 == 2) { x3 = e.getX(); y3 = e.getY(); } i++; repaint(); } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void drawInformation(Graphics2D g2) { g2.setColor(Color.BLUE); g2.drawString("<x and y coordinates>", 5, 50); g2.drawString("(x1,y1): (" + Double.toString(x1) + ", " + Double.toString(y1) + ")", 5, 75); g2.drawString("(x2,y2): (" + Double.toString(x2) + ", " + Double.toString(y2) + ")", 5, 100); g2.drawString("(x3,y3): (" + Double.toString(x3) + ", " + Double.toString(y3) + ")", 5, 125); g2.setColor(Color.BLACK); calculateSideLength(); g2.drawString("<Side lengths>", 5, 150); g2.drawString("Side 1: " + Double.toString(side1), 5, 175); g2.drawString("Side 2: " + Double.toString(side2), 5, 200); g2.drawString("Side 3: " + Double.toString(side3), 5, 225); g2.setColor(Color.MAGENTA); g2.drawString("Angle 1: " , /* add the angle 1 code here */ 5, 275); g2.setColor(Color.GREEN); g2.setColor(Color.RED); } public void calculateSideLength() { side1 = Math.sqrt((x2 - x1) * (x2 - x1) * (y2 - y1) * (y2 - y1)); side2 = Math.sqrt((x3 - x2) * (x3 - x2) * (y3 - y2) * (y3 - y2)); side3 = Math.sqrt((x3 - x1) * (x3 - x1) * (y3 - y1) * (y3 - y1)); } public void calculateTrianglePerimeter() { perimeter = side1 + side2 + side3; } public void calculateTriangleArea() { double p = (side1 + side2 + side3) / 2; area = Math.sqrt(p * (p - side1) * (p - side2) * (p - side3)); } public void calculateAngles() { angle1 = Math.acos((side2 * side2 + side3 * side3 - side1 * side1) / 2 * side2 * side3); angle2 = Math.acos((side1 * side1 + side3 * side3 - side2 * side2) / 2 * side1 * side3); angle3 = Math.acos((side1 * side1 + side2 * side2 - side3 * side3) / 2 * side1 * side2); } }
Similar Threads
-
need help with calculating something
By mikec420 in forum New To JavaReplies: 13Last Post: 09-29-2011, 09:14 PM -
Errors in Program (Right Triangle)
By SupaStudy in forum New To JavaReplies: 3Last Post: 03-26-2009, 10:42 AM -
Triangle
By jkswebsite in forum New To JavaReplies: 8Last Post: 01-10-2009, 02:08 PM -
Triangle Sides program
By jamesov89 in forum New To JavaReplies: 6Last Post: 10-06-2008, 03:36 AM -
Program for calculating area and hypotenus in a triangel
By Legoland in forum New To JavaReplies: 4Last Post: 08-12-2008, 01:41 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks