Einfache Grafiken

 

Zunächst werden Grundbegriffe wie Datentypen (int, double, String, char boolean),einfache Anweisungen, Fallunterscheidungen (if ... else, switch ... case), Schleifen (for, while) behandelt, die Programmierung einfacher Grafiken (drawline(int,int,int,int); drawRect(int, int, int, int); drawPolygon(int[],int[],int); drawOval(int,int,int,int);) angegangen unter Ausnutzung der Komponenten, die Java für die Appletprogrammierung zur Verfügung stellt (Button, Slider, ComboBox, Textfeld). Beim Befehl drawPolygon(int[],int[],int); muss zum ersten Mal auf Felder eingegangen werden.

Auszüge aus dem Quelltext:

private void drawTriangle(int pX,int pY,int pLength,int pAlpha){
        int lXb=pX+pLength;
        int lYb=pY;
        int lXc=pX+(int)Math.round(pLength/2);
        int lYc=pY- (int)Math.round(pLength/2*Math.tan(pAlpha*Math.PI/180));
        int[]lX={pX,lXb,lXc};
        int[]lY={pY,lYb,lYc};
        knowsGraphics.drawPolygon(lX, lY, 3);
    
    }
    
    public void start(){
        knowsGraphics=this.getGraphics();
        zLength=0;
        sldrWidth.setValue(0); //der Slider verändert den Wert der Variable zLength
    }
    
    
    private void drawStar(int pX, int pY, int pLength){
        int d=(int)Math.round(pLength/Math.sqrt(2));
        knowsGraphics.drawLine(pX, pY, pX+pLength, pY);
        knowsGraphics.drawLine(pX, pY, pX+d, pY-d);
        knowsGraphics.drawLine(pX, pY, pX, pY-pLength);
        knowsGraphics.drawLine(pX, pY, pX-d, pY-d);
        knowsGraphics.drawLine(pX, pY, pX-pLength, pY);
        knowsGraphics.drawLine(pX, pY, pX-d, pY+d);
        knowsGraphics.drawLine(pX, pY, pX, pY+pLength);
        knowsGraphics.drawLine(pX, pY, pX+d, pY+d);
    }
    
    private void drawCircle(int pX, int pY, int pRadius){
        knowsGraphics.drawOval(pX-pRadius, pY-pRadius,2*pRadius,2*pRadius);
    }
    
    private void drawSquare(int pX, int pY, int pRadius){
        knowsGraphics.drawRect(pX-pRadius, pY-pRadius,2*pRadius,2*pRadius);
    }
    
    private void clear(){
        knowsGraphics.setColor(Color.white);
        knowsGraphics.fillRect(0, 0, 800, 500);
        knowsGraphics.setColor(Color.blue);
    }
    
    private void zeichne(){         
        int lAuswahl=cmbBxSelection.getSelectedIndex();
        switch (lAuswahl){
            case 0:clear();break;
            case 1:clear();drawStar(400,250,150+zLength);break;
            case 2:clear();drawTriangle(300,450,300,5+(int)Math.round(zLength*0.6));break;
            case 3:clear();drawCircle(400,250,50+zLength);break;
            case 4:clear();drawSquare(400,250,50+zLength);break;
        }
        
        
    }