Simple Graphics

 

First, basic concepts such as data types (int, double, String, char, boolean), simple instructions, conditional statements (if ... else, switch ... case), loops (for, while) are used, the programming of simple graphics (DrawLine (int , int, int, int); drawRect (int, int, int, int); drawPolygon (int [], int [], int); drawOval (int, int, int, int);) will be shown by taking advantage of the components, Java supports (button, slider, ComboBox text box). The command drawPolygon (int [], int [], int); uses arrays for the first time.

Excerpts from the source code:

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); //the Slider changes the value of the 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 drawFigur(){         
        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;
        }
        
        
    }