Graph.java

Graph.java — Java source code, 7 kB (8.122 bytes)

Dateiinhalt

import java.awt.Color;
import java.awt.Event;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

//TODO Grafik nicht verschwinden lassen sondern aktualisieren 
// und rechts an Starframe andocken 

/**
 * Generates graphics
 * @author K�ster
 *
 */
public class Graph{
	static JFrame frame;
	static pic fr;// = new pic();

	static double x_min = 0;
	static double x_max = 40;
	static double y_min = 0;
	static double y_max = 40;
	
	/**
	 * Enth�lt einzuzeichnende Punkte <br>
	 * gerade (0,2,4,...) = xi <br>
	 * ungerade (1,3,5,...) = yi
	 */
	static Vector<Double> points;// = new Vector<Double>();
	
	//Konstruktoren:
	Graph(){
		
	}
	
	Graph(double x_min, double x_max, double y_min, double y_max){
		Graph.x_min = x_min;
		Graph.x_max = x_max;
		Graph.y_min = y_min;
		Graph.y_max = y_max;
	}
	
	Graph(double x_min, double x_max, double y_min, double y_max, Vector<Double> points){
		Graph.x_min = x_min;
		Graph.x_max = x_max;
		Graph.y_min = y_min;
		Graph.y_max = y_max;
		Graph.points = points;
	}
	
	Graph(double x_min, double x_max, double y_min, double y_max, double[] point){
		Graph.x_min = x_min;
		Graph.x_max = x_max;
		Graph.y_min = y_min;
		Graph.y_max = y_max;
		points = new Vector<Double>();
		Graph.points.add(point[0]);
		Graph.points.add(point[1]);
	}
	
	public void setPoints(Vector<Double> points){
		Graph.points = points;
	}
	
	public void setPoint(double[] point){
		points = new Vector<Double>();
		Graph.points.add(point[0]);
		Graph.points.add(point[1]);
	}
	
	
	/*
	 * Nur zu Testzwecken
	 * @param args
	 */
	/*
	public static void main(String [] args){
		Graph gr = new Graph();
		gr.draw();
	}*/
	
	public void draw(){
		frame = new JFrame();
		fr = new pic();
		
		JMenuBar meba = new JMenuBar();
		JMenu m_options = new JMenu("Options"); m_options.setMnemonic('o');
		meba.add(m_options);
		
		 JMenuItem m_print= new JMenuItem("Print Graphic"/*, new ImageIcon("Icons/printer.gif")*/);
		 m_print.setMnemonic('p');
	     m_print.addActionListener(new HandleMenu2());
	     m_print.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,Event.CTRL_MASK));
	     m_options.add(m_print);
	     
	     m_options.addSeparator();

	     JMenuItem m_close= new JMenuItem("Close"/*, new ImageIcon("Icons/cross.gif")*/);
		 m_close.setMnemonic('c');
	     m_close.addActionListener(new HandleMenu2());
	     m_close.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4,Event.CTRL_MASK));
	     m_options.add(m_close);
	     
		frame.setJMenuBar(meba);
		
		frame.add(fr);
		frame.setTitle("Graphics");
		frame.setSize(700,600);
		//frame.setMinimumSize(new Dimension(700, 600));
		frame.setBackground(Color.WHITE);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		//frame.setVisible(true);
		frame.pack();
		fr.paint(frame.getGraphics());
		//frame.setVisible(false);
	}
}


class HandleMenu2 implements ActionListener{
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("Print Graphic"))
			PrintUtilities.printComponent(Graph.fr);
		else if (e.getActionCommand().equals("Close"))
			Graph.frame.dispose();
	}
}
		

class pic extends JPanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = 4954561588672770195L;
	int x0 = 50;
	int y0 = 30;
	double ScaleX;
	double ScaleY;
	
	double dx; //= 0.1;
	double dy;
	
	DecimalFormatSymbols locsym  = new DecimalFormatSymbols(Locale.ENGLISH);
	DecimalFormat df = new DecimalFormat( "0.0", locsym);
	
	// Zu plottende Funktionen:
	
	public void paint(Graphics g){
		
		//Skalierung:
		ScaleX = (super.getWidth() - x0)  /(Graph.x_max - Graph.x_min);
		ScaleY = (super.getHeight()- y0) /(Graph.y_max - Graph.y_min);
		dx = (Graph.x_max - Graph.x_min)/super.getWidth(); //Pixelstep
		dy = (Graph.y_max - Graph.y_min)/super.getHeight(); //Pixelstep
		
		//Skalierungstests:
		//System.out.println("super.getWidth(): " + super.getWidth());
		//System.out.println("x0: " + x0);
		//System.out.println("dx: " + dx);
		//g.drawLine(0, 0, 10, 10);
		//System.out.println("ScaleX: " + ScaleX);
		//System.out.println("ScaleY: " + ScaleY);
		//g.drawString("ScaleX: " + ScaleX, x0 + 4, 12);
		//g.drawString("ScaleY: " + ScaleY, x0 + 4, 24);
		//drawLine(g,0,10,10,10);
		
		
		//Achsen:
		g.drawLine(x0/2, super.getHeight() - y0, super.getWidth(), super.getHeight() - y0);
		g.drawLine(x0/2, super.getHeight() - y0 +1, super.getWidth(), super.getHeight() - y0 +1);
		g.drawLine(x0, 0, x0, super.getHeight() - y0/2);
		g.drawLine(x0-1, 0, x0-1, super.getHeight() - y0/2);
		
		//Achsenbezeichungen:
		g.setFont(new Font(Font.SERIF, Font.BOLD, 14));
		g.drawString("t", super.getWidth()/2, super.getHeight()-2);
		g.drawString("h", 2, super.getHeight()/2);
		
		g.setFont(new Font(Font.SERIF, Font.PLAIN, 12));
		
		//Achsenbeschriftung und Funktions-Linierung:
		g.setColor(Color.BLUE);
		for (double y = Graph.y_min; y <= Graph.y_max; y+= Math.max(0.5, 0.5*(int)(60*dy))){
			//drawLine(g, y,0,y,j_bsp1_test.f_mm(y));
			g.drawString(""+df.format(y), (int)x0/3, (int)(super.getHeight() - y0 - ScaleY * (y-Graph.y_min) + 5));
		}
		for (double x = Graph.x_min; x <= Graph.x_max; x+= Math.max(0.5, 0.5*(int)(80*dx))){
			drawLine(g, x,0,x,Sandwich.f_mm(x));
			g.drawString(""+df.format(x), (int)(x0 + ScaleX * (x-Graph.x_min) - 8), super.getHeight() - y0/2);
		}
		
		/*
		for (double y = Graph.y_min; y <= Graph.y_max; y+= 25*dy){
			//drawLine(g, y,0,y,j_bsp1_test.f_mm(y));
			g.drawString(""+df.format(y), (int)x0/3, (int)(super.getHeight() - y0 - ScaleY * (y-Graph.y_min) + 5));
		}
		for (double x = Graph.x_min; x <= Graph.x_max; x+= 40*dx){
			drawLine(g, x,0,x,Sandwich.f_mm(x));
			g.drawString(""+df.format(x), (int)(x0 + ScaleX * (x-Graph.x_min) - 8), super.getHeight() - y0/2);
		}*/
		
		
		//Funktionen:
		for(double x = Graph.x_min; x <= Graph.x_max; x += dx ){
			g.setColor(Color.BLUE);
			drawLine(g, x, Sandwich.f_mm(x), x+dx, Sandwich.f_mm(x+dx));
			//System.out.println("Mod: " + (x % 2));
			//Label x-Achse und Linierung:
			/*
			if ((x % 2) < 0.01){
				drawLine(g, x,0,x,j_bsp1_test.f_mm(x));
				g.drawString(""+(int)x, (int)(x0 + ScaleX * (x-Graph.x_min)), super.getHeight() - y0/2);
			}*/
			
			g.setColor(Color.MAGENTA);
			drawLine(g, x, Sandwich.f_n(x), x+dx, Sandwich.f_n(x+dx));
		}
		
		
		//Schnittpunkt:
		g.setColor(Color.BLACK);
		if (Graph.points != null) drawPoint(g, Graph.points);
	}
	
	/**
	 * Funktion �bernimmt Zeichnen inklusive Skalierung und Verschiebung
	 * @param g - Graphics
	 * @param x1
	 * @param y1
	 * @param x2
	 * @param y2
	 */
	public void drawLine(Graphics g, double x1, double y1, double x2, double y2){
		//Zeichnet nicht unterhalb der x-Achse:
		int tmp = (int)(-y0 + super.getHeight() - ScaleY * (y2-Graph.y_min));
		/* TODO
		if (y2 - Graph.y_min <= 0 ){
			y1 = 0;
			y2 = 0;
			tmp = (int)(-y0 + super.getHeight() - ScaleY * (-Graph.y_min));
		}*/
		//y1 = Math.max(0, y1-y0*ScaleY);
		//y2 = Math.max(0, y2-y0*ScaleY);
		
		g.drawLine((int)(x0 + ScaleX * (x1-Graph.x_min)), (int)(-y0 + super.getHeight() - ScaleY * (y1-Graph.y_min)),
				   (int)(x0 + ScaleX * (x2-Graph.x_min)), tmp);
	}
	
	public void drawPoint(Graphics g, Vector<Double> points){
		for (int i = 0; i < points.size(); i+=2){
			g.fillOval((int)(x0 + ScaleX * (points.get(i)-Graph.x_min))-3, (int)(-y0 + super.getHeight() - ScaleY * (points.get(i+1)-Graph.y_min))-3, 6, 6);
			//System.out.println("Punkt " + i + "," + (i+1) + " gezeichnet!");
		}
	
	}	
}