import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;

class VisualizzaGrafico extends JFrame {
	public static void main(String args[]) {
		VisualizzaGrafico mainFrame = new VisualizzaGrafico();
		mainFrame.setSize(200, 200);
		mainFrame.setTitle("VisualizzaGrafico");
		mainFrame.show();
	}
	
	public VisualizzaGrafico() {
		addWindowListener(new MyWindowListener());

		diagram=new PannelloGrafico();
		
		JPanel	rangePane=new JPanel();
		rangePane.add(new JLabel("Xmin"));
		rangePane.add(xmin=new JTextField(6));
		rangePane.add(new JLabel("Xmax"));
		rangePane.add(xmax=new JTextField(6));
		rangePane.add(new JLabel("Ymin"));
		rangePane.add(ymin=new JTextField(6));
		rangePane.add(new JLabel("Ymax"));
		rangePane.add(ymax=new JTextField(6));
		
		Container  contentPane=getContentPane();
		contentPane.add(diagram,"Center");
		contentPane.add(rangePane,"South");
		
		xmin.addActionListener(new TextFieldListener());
		xmax.addActionListener(new TextFieldListener());
		ymin.addActionListener(new TextFieldListener());
		ymax.addActionListener(new TextFieldListener());
		
	}


	private	JTextField	xmin,xmax,ymin,ymax;
	private double		xLeft=-20,xRight=20,yDown=-100,yUp=100;
	private double		maxx,maxy;
	private	PannelloGrafico	diagram;


	class PannelloGrafico extends JPanel {
			
		public void paintComponent(Graphics g) {	
			super.paintComponent(g);
			Graphics2D g2 = (Graphics2D)g;
			maxx = getWidth();
			maxy = getHeight();
			Rectangle2D.Double	sfondo=new Rectangle2D.Double(0,0,maxx-1,maxy-1);
			g2.setColor(Color.white);
			g2.fill(sfondo);
			g2.setColor(Color.black);
			
			final int npunti=10000;
			
			asseX(g2); 
			asseY(g2);
		
			double x = xLeft;
			double y;
			final int EPS = 1;
			double passo = (double)(xRight - xLeft) / (npunti - 1);
			while(x <= xRight) {
				y = f(x);
				Ellipse2D.Double punto = new Ellipse2D.Double(trasformaX(x) - EPS, 
			                               trasformaY(y) - EPS, 2*EPS+1, 2*EPS+1);
				g2.fill(punto);
				x += passo;
			}
		}
		
		private double f(double x) {
				return 1 + x*x / Math.sin(x);
		}

		private double trasformaX(double x) {
			return maxx * (x - xLeft) / (xRight - xLeft);
		}
		private double trasformaY(double y) {
			return maxy * (yUp - y) / (yUp - yDown);
		}
	
		private boolean asseY(Graphics2D g2) {
			if(xLeft * xRight <= 0) {
				Line2D.Double asse = new Line2D.Double(trasformaX(0), trasformaY(yDown), 
			                           trasformaX(0), trasformaY(yUp));
				g2.draw(asse);
				return true;
				} 
				else return false;
		}
			
		private boolean asseX(Graphics2D g2) {
			if(yDown * yUp <= 0) {
				Line2D.Double asse = new Line2D.Double(trasformaX(xLeft), trasformaY(0), 
			                           trasformaX(xRight), trasformaY(0));
				g2.draw(asse);
				return true;
			}
			else return false;
		}
	
	}

	private	class	TextFieldListener implements ActionListener	{
		public void	actionPerformed(ActionEvent event)	{
				xLeft=Double.parseDouble(xmin.getText());
				xRight=Double.parseDouble(xmax.getText());
				yDown=Double.parseDouble(ymin.getText());
				yUp=Double.parseDouble(ymax.getText());
				repaint();
		}
		
	}

	private class MyWindowListener extends WindowAdapter {
		public void windowClosing(WindowEvent e) {
			System.exit(0);
		}
	}

	
}



Questa la bacheca di Informatica Grafica, corso di laurea specialistica in Ing. Edile-architettura
by FdA
ultima modifica: 02/02/2005 11.30