Atlassian uses cookies to improve your browsing experience, perform analytics and research, and conduct advertising. Accept all cookies to indicate that you agree to our use of cookies on your device. Atlassian cookies and tracking notice, (opens new window)
Confluence
For you

Programming
Results will update as you type.
  • Tiling and Repetition
  • Reactors
  • Programming Basics: Parametric and Generative Graphic Design 2016
  • Archive
  • High Scores
  • Artificial Neural Network
  • Alternatives to the Processing IDE
  • p5.js Programming
    • p5.js Introduction
    • p5.js Variables
    • p5.js Random Numbers
    • p5.js SVG + Images
    • p5.js WebGL
    • p5.js Classes and Objects
      • P5js Classes and Objects (de)
    • p5.js Events and Functions
    • p5.js Loops
    • p5.js Coordinates
    • P5js Nested Loops
    • p5.js Animation Exercise 1
    • p5.js Animation Exercise 2
    • p5.js Conditionals
    • p5.js Arrays and Lists
    • p5.js Simple Collision Detection
    • p5.js Reactors
    • p5.js Tiling and Repetition
    • p5.js Vectors
    • p5.js Animation Solution with Objects
    • p5.js Easing
    • p5.js Perlin Noise
    • p5.js Particle System
    • p5.js Sound
    • p5j.s Typography
    • P5js Archive
  • Programming in Processing (java)

    Two hearts overlapped with each other
    Welcome back
    Catch up on the top discussions and highlights from your team.
    /
    P5js Classes and Objects (de)
    Published Jul 30, 2021

    P5js Classes and Objects (de)

    Jul 30, 2021

    Klassen sind eine der wichtigsten Programmierkonstrukte von Java. Sie sind das abstrakte Modell eines Objektes. Sozusagen der Bauplan für ein Objekt. Vereinfacht gesagt könnte man Klassen auch als erweiterte Datentypen sehen. Als selbst definierte Datentypen die Variablen und Funktionen beinhalten. Um Zugriff auf Variablen=Attribute und Funktionen=Methoden zu haben muss nach dem Objekt ein Punkt “.” gesetzt werden, gefolgt von dem Namen des Attributs oder der Methode.

    Aufbau einer Klasse:

    class KlassenName extends BasisKlasse
    {
    [Konstruktor – Initialisierungs Funktion]

    [Attribute - Variablen]

    [Methoden - Funkionen]

    }

    Konstruktor

    Der Konstruktor ist die Initialisierungsfunktion einer Klasse. Sie wird aufgerufen wenn eine Klasse erzeugt wird. Eine Klasse wird mit dem Befehl ‘new’ und dem anschliessenden Aufruf des Konstruktors erzeugt. Um den Konstruktor einer Basisklasse aufzurufen(im Falle einer abgeleiteten Klasse) muss das Schlüsselwort ‘super’ benutzt werden.

    Attribute

    Attribute sind die Variablen einer Klasse.

    Methoden

    Methoden sind die Funktionen einer Klasse.

    Abgeleitete Klasse – Keyword ‘extends’

    Eine Klasse kann von einer anderen Klasse abgeleitet werden. Damit erbt diese Klasse die Eigenschaften der Basisklasse. Die Klasse erbt die Methoden und Attribute der Basisklasse. Sollte sie aber Methoden oder Attribute setzen, welche gleich heissen wie solche in der Basisklasse, dann werden die Basisklassen Eigenschaften überschrieben.

    Beispiel BallKlasse

    BouncingBall ball1; BouncingBall ball2; PVector p1 = new PVector(); PVector p2 = new PVector(); boolean drag = false; void setup() { size(800,600); ball1 = new BouncingBall(100,100); ball2 = new BouncingBall(50,50); // svg bild soll zentriert sein shapeMode(CENTER); smooth(); } void draw() { // hintergrund loeschen //background(255); // ghosting fill(255,255,255,60); rect(0,0,width,height); if(drag) { // zeichne die abschussrichtung p1.set(ball1._pos.x,ball1._pos.y,0); line(p1.x,p1.y,p2.x,p2.y); } // zeichne den ball ball1.draw(); ball2.draw(); } void mousePressed() { drag = true; p1.set(ball1._pos.x,ball1._pos.y,0); p2.set(mouseX,mouseY,0); } void mouseDragged() { p2.set(mouseX,mouseY,0); } void mouseReleased() { drag = false; // abschuss staerke berechnen PVector dir = PVector.sub(p2,p1); // laenge verkuerzen dir.mult(.09); // der ball wird neu ausgerichtet ball1.set(p1,dir,.993); ball2.set(p1,dir,.95); }

    BouncingBall

    class BouncingBall { PVector _pos; PVector _dir; float _dampV; PShape _shape; int _w; int _h; // konstruktor BouncingBall(int shapeWidth,int shapeHeight) { _pos = new PVector(width/2, height/2); _dir = new PVector(0,0); _dampV = 1; _w = shapeWidth; _h = shapeHeight; _shape = loadShape("ball1.svg"); } // setzt die neue pos + richtung + daempfung void set(PVector pos,PVector dir,float dampV) { _pos = pos.get(); _dir.add(dir); _dampV = dampV; } // erneuert die aktuelle position void calcPos() { // aktuelle position verschieben _pos.add(_dir); // bewegungs vektor veraendert _dir.mult(_dampV); // teste horizontal if(_pos.x + _w/2 > width) { _dir.x *= -1; _pos.x = width - _w/2; } else if(_pos.x - _w/2 < 0) { _dir.x *= -1; _pos.x = _w/2; } // teste vertikal if(_pos.y + _h/2 > height) { _dir.y *= -1; _pos.y = height - _w/2; } else if(_pos.y - _h/2 < 0) { _dir.y *= -1; _pos.y = _h/2; } } // zeichnet den ball void draw() { calcPos(); shape(_shape, _pos.x,_pos.y, _w,_h); } } class BallEx extends BouncingBall { BallEx(int size) { super(size,size); } void draw() { super.draw(); line(0,0,_pos.x,_pos.y); } }

    Download Beispiel

    Aufgabe:

    • Erweitere das Programm, dass mehrere Bälle unabhängig voneinander platziert werden können

    • Erweitere die Klasse um eine neue Art von Bällen







    {"serverDuration": 17, "requestCorrelationId": "c2bdc28aa12a464185228f66d7ee0f33"}