# Quadratische Gleichungen zeichen?



## Schatti (13. Dezember 2003)

Hi wenn ich z.b.

eine quadratische gleichung

y = x² + 3x + 4 habe,

wie kann ich diese mit java zeichnen?


----------



## SonicBe@m (14. Dezember 2003)

y = x² + 3x + 4
in java
double pow( double, double )
int y = Math.pow(x,2.0) + 3 * x  + 4;

Naja in java zeichnen  dazu gibs das AWT.
Nimm dir mal das zu herzen
http://java.sun.com/j2se/1.4.1/docs/api/


----------



## Thomas Darimont (14. Dezember 2003)

Servus!


```
package pack;

/**
 * @author Administrator
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
/*
 * Plotter.java
 *
 * Created on 14. Dezember 2003, 00:04
 */

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Plotter extends javax.swing.JFrame {

	private BufferedImage screen = null;
	private int scale = 0;
	private double a;
	private double b;
	private double c;
	/** Creates new form Plotter */
	public Plotter() {
		initComponents();
		pack();
		screen = (BufferedImage) this.createImage(500, 500);
		scale = 100;
		txt_a.setText("2");
		txt_b.setText("10");
		txt_c.setText("-35");
	}

	private void initComponents() {
		java.awt.GridBagConstraints gridBagConstraints;

		jPanel1 = new JPanel();
		jPanel2 = new JPanel();
		jLabel1 = new JLabel();
		txt_a = new JTextField();
		jLabel2 = new JLabel();
		txt_b = new JTextField();
		jLabel3 = new JLabel();
		txt_c = new JTextField();
		jButton1 = new JButton();

		getContentPane().setLayout(new GridBagLayout());

		setTitle("Plotter");
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent evt) {
				exitForm(evt);
			}
		});

		jPanel1.setPreferredSize(new Dimension(500, 500));
		getContentPane().add(jPanel1, new GridBagConstraints());

		jPanel2.setPreferredSize(new Dimension(400, 100));
		jLabel1.setText("f(x) = ");
		jPanel2.add(jLabel1);

		txt_a.setColumns(3);
		txt_a.setToolTipText("a");
		txt_a.setName("txt_x2");
		jPanel2.add(txt_a);

		jLabel2.setText(" * x^2 +");
		jPanel2.add(jLabel2);

		txt_b.setColumns(3);
		txt_b.setToolTipText("b");
		jPanel2.add(txt_b);

		jLabel3.setText(" * x +");
		jPanel2.add(jLabel3);

		txt_c.setColumns(3);
		txt_c.setToolTipText("c");
		jPanel2.add(txt_c);

		jButton1.setText("Draw");
		jButton1.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent evt) {
				jButton1MousePressed(evt);
			}
		});

		jPanel2.add(jButton1);

		gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 1;
		getContentPane().add(jPanel2, gridBagConstraints);

		pack();
	}

	private void jButton1MousePressed(MouseEvent evt) {
		// Add your handling code here:
		a = Double.parseDouble(txt_a.getText());
		b = Double.parseDouble(txt_b.getText());
		c = Double.parseDouble(txt_c.getText());

		drawGraph(a, b, c);

	}

	/** Exit the Application */
	private void exitForm(WindowEvent evt) {
		this.dispose();
		System.exit(0);
	}

	private void drawGraph(double a, double b, double c) {
		Graphics2D g2d = (Graphics2D) screen.getGraphics();
		g2d.setColor(Color.white);
		g2d.fillRect(0, 0, 500, 500);
		g2d.setColor(Color.black);
		drawCross(g2d);
		drawFunction(g2d, a, b, c);
		toScreen();

	}

	private void toScreen() {
		jPanel1.getGraphics().drawImage(screen, 0, 0, 500, 500, this);
	}

	private void drawFunction(Graphics2D g, double a, double b, double c) {
		int val[] = new int[500];
		int tmp = val.length / 2;
		for (int i = -tmp; i < tmp; i++) {

			val[i + tmp] =
				(int) ((a * Math.pow(i, 2)) / (scale)
					+ (b * i * -1) / (scale / 10)
					+ c);
			//System.out.println("X: " + i + " Y: " + val[i + tmp]);
		}

		int xNext, yNext;

		for (int x = 0; x < val.length; x++) {

			xNext = x + 1;
			yNext = x + 1 < val.length ? val[x + 1] : val[x];

			g.drawLine(x, 250 - val[x], xNext, 250 - yNext);
		}
	}

	private void drawCross(Graphics2D g) {
		Dimension d = jPanel1.getSize();

		g.drawLine(
			0,
			(int) (d.getHeight() / 2),
			(int) d.getWidth(),
			(int) (d.getHeight() / 2));
		g.drawLine(
			(int) (d.getWidth() / 2),
			0,
			(int) (d.getWidth() / 2),
			(int) d.getHeight());

	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		new Plotter().show();
	}

	public void paint(Graphics g) {
		super.paint(g);
		jPanel1.getGraphics().drawImage(screen, 0, 0, 500, 500, this);
	}

	// Variables declaration - do not modify
	private javax.swing.JButton jButton1;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JTextField txt_a;
	private javax.swing.JTextField txt_b;
	private javax.swing.JTextField txt_c;
	// End of variables declaration

}
```


----------



## SonicBe@m (14. Dezember 2003)

nice


----------



## Schatti (14. Dezember 2003)

ok danke 

wie heißt denn die main methode, damit ich das programm ausführen kann?


----------



## Thomas Darimont (14. Dezember 2003)

Servus!

hmmm?

javac Plotter.java
und ein
java Plotter 

sollten eigentlich genügen ...

Gruß Tom


----------

