WCM - Das österreichische Computer Magazin Forenübersicht
 

Zurück   WCM Forum > Rat & Tat > Programmierung

Programmierung Rat & Tat für Programmierer

Microsoft KARRIERECAMPUS

Antwort
 
Themen-Optionen Ansicht
Alt 16.12.2002, 02:36   #1
Slowhand
Jr. Member
 
Registriert seit: 15.11.2002
Beiträge: 45


Standard Hilfe bei Graphen in Java Swing(mit code)

Habe hier einen Swing Graphen den ich verändern möchte undzwar dass ich ihm Parameter für x1, y1, x2, y2, etc übergeben kann und er zeichnet es dann. So wie der Code ist zeichnet er eine x² Funktion Aber dass ich ihm nur ein paar gewisse Punkte geben kann klappt nicht sorecht.
Der code wäre folgender:

// GraphDemo.java

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

public class GraphDemo extends JFrame {

GraphDemo()
{
// super("Resize me!");
this.setTitle("Frame Title");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(new GraphPane());
setSize(400,400);
setVisible(true);
}

public static void main(String[] args) {
new GraphDemo();
}
}

class GraphPane extends JPanel
{
final int margin = 8;
public void paintComponent(Graphics g)
{
Dimension d = getSize();
Rectangle r = new
Rectangle(margin,margin,d.width-margin*2,d.height-margin*2);
// background color
g.setColor(new Color(255,255,255));
g.fillRect(0,0,d.width,d.height);
// grid color
g.setColor(Color.lightGray);
drawGrid(g,r,16,16);
// line color
g.setColor(Color.blue);
drawFunction(g,r);
}

private void drawGrid(Graphics g,Rectangle r,int w, int h)
{
for(int y = 0;y <= h;y++) {
int gy = ntrp(0,h,r.y,r.height,y);
g.drawLine(r.x,gy,r.width,gy);
}
for(int x = 0;x <= w;x++) {
int gx = ntrp(0,w,r.x,r.width,x);
g.drawLine(gx,r.y,gx,r.height);
}

}

private void drawFunction(Graphics g,Rectangle r)
{
boolean start = true;
double epsilon = .01;
double xa = -3.0;
double xb = 3.0;
int oldx = 0, oldy = 0;

for(double x = xa;x <= xb; x += epsilon)
{
// just a picturesque
// sample function
double y = Math.exp(-(x*x));
int gx = ntrp(xa,xb,r.x,r.width,x);
int gy = ntrp(0,1,r.height,r.y,y);
if(!start) {
g.drawLine(oldx,oldy,gx,gy);
}
oldx = gx;
oldy = gy;
start = false;
}

}

// interpolation routine
private int ntrp(double xa,double xb,int ya,int yb, double x)
{
return (int)((x-xa)/(xb-xa) * (yb-ya)) + ya;
}
}
Slowhand ist offline   Mit Zitat antworten
Antwort


Aktive Benutzer in diesem Thema: 1 (Registrierte Benutzer: 0, Gäste: 1)
 

Forumregeln
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.

Gehe zu


Alle Zeitangaben in WEZ +2. Es ist jetzt 08:44 Uhr.


Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Forum SEO by Zoints
© 2009 FSL Verlag