Nabend,
Ich wollte eine Funktion bestehend aus X -und Y-Punkten zeichnen.
Um dies möglichst genau zu machen wollte ich mit Double-Werten arbeiten, aber da ein Bildschirm ja nur aus ganzen Pixeln besteht.. Wie kann ich die Funktion genau zeichnen?
Es reicht im Bereich +/-10 - An Punkten kann ich belibieg viele erzeugen, aber viele machen ja nicht sein, bei ganzen Pixeln?
Wie krieg ich das hin?
Wenn ich meinetwegen solche Arrays hätte:
double xPoints = {0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0}
double yPoints = {0.5,1.0,1.9,2.1,2.1,2.9,3.8,4,9,6.1,7.0}
und wollte diese Zeichnen.
Dazu hab ich mir folgende Klasse angelegt:
Von dieser Klasse erzeuge ich ein objekt, mache es sichtbar, und rufe dann die paint-methode auf.
Meine Punkte sehen meist so aus. Wie muss ich diese noch zurechtstuzen um die Funktion zu zeichnen?
Formel: x * 0.5
X-Punkt: 0.14974999999999453 Y-Punkt: 0.07487499999999726
X-Punkt: 0.14979999999999452 Y-Punkt: 0.07489999999999726
X-Punkt: 0.14984999999999452 Y-Punkt: 0.07492499999999726
X-Punkt: 0.1498999999999945 Y-Punkt: 0.07494999999999725
Hier mal der Sourcecode zum erzeugen der Punkte.
Ich wollte eine Funktion bestehend aus X -und Y-Punkten zeichnen.
Um dies möglichst genau zu machen wollte ich mit Double-Werten arbeiten, aber da ein Bildschirm ja nur aus ganzen Pixeln besteht.. Wie kann ich die Funktion genau zeichnen?
Es reicht im Bereich +/-10 - An Punkten kann ich belibieg viele erzeugen, aber viele machen ja nicht sein, bei ganzen Pixeln?
Wie krieg ich das hin?
Wenn ich meinetwegen solche Arrays hätte:
double xPoints = {0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0}
double yPoints = {0.5,1.0,1.9,2.1,2.1,2.9,3.8,4,9,6.1,7.0}
und wollte diese Zeichnen.
Dazu hab ich mir folgende Klasse angelegt:
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package draw;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.Toolkit;
import java.awt.image.ImageObserver;
import java.text.AttributedCharacterIterator;
import javax.swing.JFrame;
/**
*
* @author Ocean-Driver
*/
public class FunctionDraw extends JFrame{
int[] xPoints;
int[] yPoints;
public FunctionDraw(double[] x, double[] y)
{
setSize(Toolkit.getDefaultToolkit().getScreenSize());
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
xPoints = new int[x.length];
yPoints = new int[y.length];
for(int i=0; i < x.length; i++)
{
xPoints[i] = (int) (x[i] / super.getSize().width);
yPoints[i] = (int) (x[i] / super.getSize().height);
}
}
public void paint()
{
new Graphics() {
@Override
public Graphics create() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void translate(int x, int y) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public Color getColor() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void setColor(Color c) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void setPaintMode() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void setXORMode(Color c1) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public Font getFont() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void setFont(Font font) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public FontMetrics getFontMetrics(Font f) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public Rectangle getClipBounds() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void clipRect(int x, int y, int width, int height) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void setClip(int x, int y, int width, int height) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public Shape getClip() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void setClip(Shape clip) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void copyArea(int x, int y, int width, int height, int dx, int dy) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void drawLine(int x1, int y1, int x2, int y2) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void fillRect(int x, int y, int width, int height) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void clearRect(int x, int y, int width, int height) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void drawOval(int x, int y, int width, int height) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void fillOval(int x, int y, int width, int height) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void drawString(String str, int x, int y) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void drawString(AttributedCharacterIterator iterator, int x, int y) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public boolean drawImage(Image img, int x, int y, ImageObserver observer) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public boolean drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void dispose() {
throw new UnsupportedOperationException("Not supported yet.");
}
}.drawPolyline(xPoints, yPoints, WIDTH);
}
}
Von dieser Klasse erzeuge ich ein objekt, mache es sichtbar, und rufe dann die paint-methode auf.
Meine Punkte sehen meist so aus. Wie muss ich diese noch zurechtstuzen um die Funktion zu zeichnen?
Formel: x * 0.5
X-Punkt: 0.14974999999999453 Y-Punkt: 0.07487499999999726
X-Punkt: 0.14979999999999452 Y-Punkt: 0.07489999999999726
X-Punkt: 0.14984999999999452 Y-Punkt: 0.07492499999999726
X-Punkt: 0.1498999999999945 Y-Punkt: 0.07494999999999725
Hier mal der Sourcecode zum erzeugen der Punkte.
Code:
String a = "x * 0.5";
double[] xPoints = new double[1000];
double[] yPoints = new double[1000];
xPoints[0] = 0.1;
yPoints[0] = 0.05;
for(int i=1; i < xPoints.length; i++)
{
xPoints[i] = xPoints[i-1] + 0.00005;
yPoints[i] = MathParser.getInstance().ParseFunction(a.replaceAll("x",String.valueOf(xPoints[i])));
System.out.print("X-Punkt: " + xPoints[i]);
System.out.println(" Y-Punkt: " + yPoints[i]);
}