import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.*;
import javax.swing.JPanel;
import java.awt.event.*;
import java.util.*;
public class AreaDisegno extends JPanel{
private static final int BASE = 8;
private int x=-1;
private int y=-1;
private int x1=-1;
private int y1=-1;
private int x2=-1;
private int y2=-1;
private Ellipse2D.Double r1;
private Ellipse2D.Double r2;
private Ellipse2D.Double r3;
private Line2D.Double l1;
private Line2D.Double l2;
private Line2D.Double l3;
private Point2D.Double p1;
private Point2D.Double p2;
private Point2D.Double p3;
private double a;
private double b;
private double c;
public AreaDisegno(){
this.addMouseListener(new MouseClickListener());
}
class MouseClickListener extends MouseAdapter{
public void mouseClicked (MouseEvent e){
if(x>-1)
{
if(x1>-1)
{
if(x2>-1)
{
}
else
{
x2=e.getX();
y2=e.getY();
AreaDisegno tri= (AreaDisegno)e.getSource();
disegna(tri);
}
}
else
{
x1=e.getX();
y1=e.getY();
}
}
else
{
x=e.getX();
y=e.getY();
}
}
}
public void disegna(AreaDisegno tri)
{
Graphics g = tri.getGraphics();
Graphics2D g2 = (Graphics2D) g;
Ellipse2D.Double r1 = new Ellipse2D.Double(x,y,BASE, BASE);
Ellipse2D.Double r2 = new Ellipse2D.Double(x1,y1,BASE, BASE);
Ellipse2D.Double r3 = new Ellipse2D.Double(x2,y2,BASE, BASE);
Point2D.Double p1= new Point2D.Double(x,y);
Point2D.Double p2= new Point2D.Double(x1,y1);
Point2D.Double p3= new Point2D.Double(x2,y2);
Line2D.Double l1 = new Line2D.Double(p1,p2);
Line2D.Double l2 = new Line2D.Double(p2,p3);
Line2D.Double l3 = new Line2D.Double(p3,p1);
g2.draw(r1);
g2.fill(r1);
g2.draw(r2);
g2.fill(r2);
g2.draw(r3);
g2.fill(r3);
g2.draw(l1);
g2.draw(l2);
g2.draw(l3);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.draw(r1);
g2.fill(r1);
g2.draw(r2);
g2.fill(r2);
g2.draw(r3);
g2.fill(r3);
}
public double perimetro(double a, double b, double c){
a=p1.distance(p2);
b=p2.distance(p3);
c=p1.distance(p3);
double p;
return p = a+b+c;
}
public void cancella(){
x=x1=x2=y=y1=y2=-1;
updateUI();
return;
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Finestra extends JFrame
{
private final AreaDisegno disegno;
private JButton calcola_area;
private JButton perimetro;
private JButton mediane;
private JButton baricentro;
private JButton cancella;
private JPanel pannello_pulsanti;
public Finestra(){
disegno = new AreaDisegno();
disegno.setBackground(Color.white);
calcola_area= new JButton("Calcola l'area");
mediane= new JButton("Trova mediane");
baricentro= new JButton("Trova baricentro");
perimetro= new JButton("Calcola il perimetro");
perimetro.addActionListener (new PerimetroActionListener());
cancella= new JButton("Cancella");
cancella.addActionListener (new CancellaActionListener());
pannello_pulsanti = new JPanel();
pannello_pulsanti.add(calcola_area);
pannello_pulsanti.add(perimetro);
pannello_pulsanti.add(mediane);
pannello_pulsanti.add(baricentro);
pannello_pulsanti.add(cancella);
this.add(BorderLayout.CENTER,disegno);
this.add(BorderLayout.NORTH,pannello_pulsanti);
this.setTitle("Triangoli");
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setExtendedState(MAXIMIZED_BOTH);
this.setVisible(true);
}
class PerimetroActionListener implements ActionListener {
public void actionPerformed (ActionEvent evt){
perimetro();
}
public void perimetro(){
JOptionPane.showMessageDialog(null, "Devi prima disegnare un triangolo!");
perimetro();
JOptionPane.showMessageDialog(null, "Il perimetro è" );
}
}
class CancellaActionListener implements ActionListener {
public void actionPerformed (ActionEvent evt){
cancella();
}
public void cancella(){
disegno.cancella();
disegno.updateUI();
return;
}
}
}
public class Main
{
public static void main(String[] args)
{
Finestra finestra= new Finestra();
}
}
Ecco il progetto, ora devo creare i metodi che calcolano il perimetro, l'area, le mediane, e il baricentro e poi far si ke vengano invocati ogni volta che premo il pulsante corrispondente. Come fare?