Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

deutsche Version

Classes are one of the most important programming constructs of Java and many other languages. They are the abstract model or blueprint for an object. One could We can also see understand classes as extended data types. Unlike the primitive data types we have already looked at, classes define both variables and functions. Programmers call class variables attributes, and class functions are as methods, but otherwise they work much the same to the variables and functions we already know. But in order to get access to these attributes and methods we need to user dot syntax: a "." must be placed after the object name, followed by the name of the attribute or the method.

...

Code Block
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)
  { // draw the zeichnedirection dieof abschussrichtungshoot
    p1.set(ball1._pos.x,ball1._pos.y,0);
    line(p1.x,p1.y,p2.x,p2.y);
  }
 
  // zeichnedraw denthe ballballs
  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;
 
  // shooting abschussdirection staerkecalculation. berechnen
  PVector dir = PVector.sub(p2,p1);
  // shorten laengethe length verkuerzen
  dir.mult(.09);
 
  //the ball is dergiven balla wirdnew neudirection ausgerichtet
  ball1.set(p1,dir,.993);
  ball2.set(p1,dir,.95);
}

...

Code Block
class BouncingBall
{
  PVector _pos;
  PVector _dir;
  float   _dampV;
  PShape  _shape;
  int     _w;
  int     _h;
   
  // constructor 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");
  }
   
  // setztset diethe neuenew posposition + richtungdirection + daempfungdampening
  void set(PVector pos,PVector dir,float dampV)
  {
    _pos = pos.get();
    _dir.add(dir);
    _dampV = dampV;
  }
   
  // erneuertupdate diethe aktuellecurrent position
  void calcPos()
  {
    // aktuellecurent position verschiebenshifted
    _pos.add(_dir);
     
    // bewegungsmovement vektorvector veraendertmodified
    _dir.mult(_dampV);
     
    // testetest horizontalhorisontal 
    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;
    }
     
    // testetest vertikalvertical 
    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;
    }
     
  }
   
  // zeichnetdraw denthe ball
  void draw()
  {
    calcPos();
     shape(_shape,
          _pos.x,_pos.y,
          _w,_h);
  }
}
 


Download Example

Image Added

Exercise 12 A

  • Modify the program, so that several balls can be placed independently of each other 

Exercise 12 B

  • Extend the class again to make a third kind of ball. Modify its attributes and methods to create new kinds of behavior 

Example BallKlasse Extended 

Code Block
class BallEx extends BouncingBall
{
  BallEx(int size)
  {
     super(size,size);
  }
 
  void draw()
  {
    super.draw();
    line(0,0,_pos.x,_pos.y);
  }
 
}

Download Example

Image Removed

Exercise:

...