0

In my university I have to do a project that includes a part where I have to right my own coordinate system with Java and it has to be dynamic, and an applet.

It is mostly already done but I don't know how I can put external data into my arrays. I don't know how many parameters I get (it could be 10 but it also could be 10000).

So how do I tell the array how much space it needs and how do I put the parameters into it?

It will be always x and y coordinates, so I have 2 arrays.

This is the code I already tried. I have hardcoded parameters for the array just to see if it works.

underneath the java code i post te html code.In that are the parameter which I want add to my arrays.

import java.awt.*;
import java.applet.Applet;
import java.lang.Math;

public class graph extends java.applet.Applet {
    //Array x-Werte
    private double[] WerteX= {0.0, 5.5, 6.5, 7.0, 16.6, 50.6};

    //Array y-Werte
    private double[] WerteY= {0.0 , 100.0, 320.0, 550.0, 700.0, 0.3};

    public void init() {
        // Hintergrundfarbe schwarz
        setBackground (Color.black);
    }

    public void paint (Graphics g) {
        float x, y;
        int xnorm, ynorm, i;

        // initialisierung der min und max X/Y werte damit sich das System anpasst
        double minX= WerteX [0];
        double maxX= WerteX [0];
        double minY= WerteY [0];
        double maxY= WerteY [0];

        // zum Kleinsten bzw. größten Wert im Array raussuchen (Prinzip Bubbelsotrt (ungefähr))
        for ( i=1; i<WerteX.length; i++) {

            if (WerteX[i]<minX){
                minX=WerteX[i];
            }
            if (WerteX[i]>maxX){
                maxX=WerteX[i];
            }
            if (WerteY[i]<minY){
                minY=WerteY[i];
            }
            if (WerteY[i]>maxY){
                maxY=WerteY[i];
            }
        }

        // Vordergrundfarbe Rot fuer die Achsen
        g.setColor (Color.red);

        // X-Achse zeichnen (schon skaliert auf 500%)
        g.drawLine (0, 650, 650, 650);

        // Y-Achse zeichnen (schon skaliert auf 500%)
        g.drawLine (100, 0, 100, 650);

        // Skaleneinteilung fuer die X-Achse zeichnen
        for (i = 100; i <= 750; i += 150){
            g.drawLine (i, 0, i, 650);
        }

        // Skaleneinteilung fuer die Y-Achse zeichnen
        for (i = 0; i <= 650; i += 50){
            g.drawLine (100, i, 750, i);
        }

        double Irgendwasdx=maxX-minX;               // achsen bezeichnung 
        double Irgendwasdy=maxY-minY;

        maxX+=Irgendwasdx/12;

        maxY+=Irgendwasdy/12;

        g.setColor(Color.red);              // farbe des Systems und schrift
        g.setFont(new Font("Arial", Font.BOLD, 20));

        // Koordinatenbezeichnung x-Achse       

        i=0;

        for ( double plotty= minX; plotty<=maxX; plotty+=(maxX-minX)/(13.0/3.0)){

            g.drawString(String.valueOf(Math.round(plotty)), 100+i*150, 670);       //runden
            i++;
        }

        //Koordinatenbezeichnung y-Achse
        i=0;

        for ( double plotty= minY; plotty<=maxY; plotty+=(maxY-minY)/13){

            g.drawString(String.valueOf(Math.round(plotty)), 80 ,650-50*i); //runden
            i++;
        }

        int size = WerteX.length; 

        g.setColor (Color.white);

        for (i=0; i<size; i++)  {               //Punkte
            g.fillOval ((int)(WerteX[i]*650/(maxX-minX)-650*minX/(maxX-minX))+100-3, 650-(int)(WerteY[i]*650/(maxY-minY)-650*minY/(maxX-minY))-3, 6, 6);
        }                       
        //Punkte verbinden
        for (i=0; i<size-1; i++) {
            g.drawLine ((int)(WerteX[i]*650/(maxX-minX)-650*minX/(maxX-minX))+100, 650-(int)(WerteY[i]*650/(maxY-minY)-650*minY/(maxX-minY)),(int)(WerteX[i+1]*650/(maxX-minX)-650*minX/(maxX-minX))+100, 650-(int)(WerteY[i+1]*650/(maxY-minY)-650*minY/(maxX-minY)));
        }

    }

}

<html>
<head>
</head>
<body>

<Applet code="graph.class" width="700" height="700">

<param name= "P" value=5>

<param name="x0" value="0.60">
<param name="x1" value="1.20">
<param name="x2" value="2.50">
<param name="x3" value="3.40">
<param name="x4" value="3.80">

<param name = "N" value =5>

<param name="y0" value="0.00">
<param name="y1" value="0.12">
<param name="y2" value="0.25">
<param name="y3" value="0.37">
<param name="y4" value="0.50">


</Applet>

</body>
</html>
JustMe
  • 1
  • 1
  • Eg to construct a new array that can contain seven doubles ==> `double[] = new double[7]`. If you want it to have 'unlimited' space, try using ArrayList's. Also, since you're using an Object-Oriented-Programming language, you could go and make your own Coordinate class, that can spawn many coordinate pairs. – MrKickkiller May 31 '16 at 22:12
  • I'm sorry i forget to sey that I am an absulut beginner so please be a littel bit more detailed.... :) – JustMe May 31 '16 at 22:14
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free). .. – Andrew Thompson Jun 01 '16 at 04:53
  • .. 3) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Jun 01 '16 at 04:53
  • Why an applet? Because our teacher says ist has to be..... o maybe i says wrong the data are in an external file (html) here is an example: – JustMe Jun 01 '16 at 08:39

1 Answers1

2

Constructing an array for doubles of size X

double[] NameOfYourArray = new double[x];

Constructing an array / list - type that can take infinite objects / doubles (in this case)

ArrayList<Double> nameOfYourArray = new ArrayList<Double>();
nameOfYourArray.add(NEW_ELEMENT); // to add an element of type Double.
nameOfYourArray.get(INDEX); // to get the element at position INDEX

Coordinates

Java is an object oriented language, meaning that you can represent anything with an object. Objects are made by defining classes. Just like Double, ArrayList, String, Integer and such are classes. We can make our own class Coordinate that holds an x and y.

public class Coordinate {
    private double x; private double y;

    public Coordinate(double x, double y){
        this.x = x; this.y = y;
    }

    public double getX(){ return x; }
    public double getY(){ return y; }
}

To make the Coordinate system work, you'll need to change the type of your ArrayList to Coordinate though, instead of Double.

Also, define Coordinate in a new file with name Coordinate.java . It should live in the same directory as your current file.

Community
  • 1
  • 1
MrKickkiller
  • 501
  • 10
  • 22