החומרים של היום–18.10.2020

תלמידים יקרים

ההקלטה של היום עולה מיד ל-drive של google classroom שלנו

 

קישור לסרטון

תוכן עניינים

MainActivity. 1

XML. 1

JAVA. 6

MyPaint 7

XML. 7

JAVA. 11

MyShape. 13

MyRect 15

MyCircle. 16

PaintView.. 17

 

 

 

 

 

MainActivity

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android
="http://schemas.android.com/apk/res/android"
   
xmlns:app
="http://schemas.android.com/apk/res-auto"
   
xmlns:tools
="http://schemas.android.com/tools"
   
android:layout_width
="match_parent"
   
android:layout_height
="match_parent"
   
android:background
="#567DA64D"
   
tools:context=".MainActivity"
>

    <TextView
       
android:id
="@+id/mainTitle"
       
android:layout_width
="match_parent"
       
android:layout_height
="wrap_content"
       
android:text="
אפליקצית המשחקים שלי"
       
android:layout_centerHorizontal="true"
       
android:gravity
="center"
       
android:textSize
="24sp"
       
android:layout_margin
="10dp"
   
>

    </TextView>

    <LinearLayout
       
android:id
="@+id/box2"

        android:layout_below="@+id/mainTitle"
       
android:layout_width
="match_parent"
       
android:layout_height
="wrap_content"
       
android:weightSum
="3"
       
android:orientation
="horizontal"
       
android:layout_margin
="5dp"
       
>
        <
Button
           
android:id
="@+id/ticBTN"
           
android:layout_weight
="1"
           
android:text="
איקס עיגול"
           
android:textColor="#FFFFFF"
           
android:textSize
="20sp"

            android:layout_width="wrap_content"
           
android:layout_height
="wrap_content"
           
android:background
="@drawable/buttonshape"
           
android:shadowColor
="#A8A8A8"
           
android:shadowDx
="0"
           
android:shadowDy
="0"
           
android:shadowRadius
="0"
           
android:onClick
="TicTacToe"
           
>
        </
Button
>
        <
Button
           
android:layout_weight
="1"
           
android:layout_width
="wrap_content"
           
android:layout_height
="wrap_content"
           
android:text="
ארבע בשורה"
           
android:textColor="#FFFFFF"
           
android:textSize
="20sp"

            android:background="@drawable/buttonshape"
           
android:shadowColor
="#A8A8A8"
           
android:shadowDx
="0"
           
android:shadowDy
="0"
           
android:shadowRadius
="0"
           
android:onClick
="fourInARow"
           
>

        </Button>
        <
Button
           
android:layout_weight
="1"
           
android:layout_width
="wrap_content"
           
android:layout_height
="wrap_content"
           
android:text="
ציור"
           
android:onClick="goToPaint"
           
android:textColor
="#FFFFFF"
           
android:textSize
="20sp"

            android:background="@drawable/buttonshape"
           
android:shadowColor
="#A8A8A8"
           
android:shadowDx
="0"
           
android:shadowDy
="0"
           
android:shadowRadius
="0"
           
>

        </Button>
    </
LinearLayout
>
    <
LinearLayout
       
android:id
="@+id/layou2"
       
android:orientation
="horizontal"
       
android:layout_width
="match_parent"
       
android:layout_height
="wrap_content"
       
android:layout_below="@+id/box2"
>
        <
Button
           
android:id
="@+id/anim1BTN"
           
android:layout_weight
="1"
           
android:text="
אנימציה ראשונה"
           
android:textColor="#FFFFFF"
           
android:textSize
="20sp"

            android:layout_width="wrap_content"
           
android:layout_height
="wrap_content"
           
android:background
="@drawable/buttonshape"
           
android:shadowColor
="#A8A8A8"
           
android:shadowDx
="0"
           
android:shadowDy
="0"
           
android:shadowRadius
="0"
           
android:onClick
="TicTacToe"
           
>
        </
Button
>
        <
Button
           
android:layout_weight
="1"
           
android:layout_width
="wrap_content"
           
android:layout_height
="wrap_content"
           
android:text="
ארבע בשורה"
           
android:textColor="#FFFFFF"
           
android:textSize
="20sp"

            android:background="@drawable/buttonshape"
           
android:shadowColor
="#A8A8A8"
           
android:shadowDx
="0"
           
android:shadowDy
="0"
           
android:shadowRadius
="0"
           
android:onClick
="fourInARow"
           
>

        </Button>
        <
Button
           
android:layout_weight
="1"
           
android:layout_width
="wrap_content"
           
android:layout_height
="wrap_content"
           
android:text="
ציור"
           
android:onClick="goToPaint"
           
android:textColor
="#FFFFFF"
           
android:textSize
="20sp"

            android:background="@drawable/buttonshape"
           
android:shadowColor
="#A8A8A8"
           
android:shadowDx
="0"
           
android:shadowDy
="0"
           
android:shadowRadius
="0"
           
>

        </Button>
    </
LinearLayout
>

    <RelativeLayout
       
android:layout_width
="match_parent"
       
android:layout_height
="match_parent"
       
android:layout_below
="@+id/box2"
       
android:layout_above
="@+id/footer"
       
android:layout_centerInParent
="true"
       
android:layout_margin
="20dp"
       
android:gravity="center"
>

        <ImageView
           
android:id
="@+id/pic1"
           
android:layout_width
="150dp"
           
android:layout_height
="150dp"
           
android:src
="@drawable/pic4"

            >

        </ImageView>

        <ImageView
           
android:id
="@+id/pic2"
           
android:layout_width
="150dp"
           
android:layout_height
="150dp"
           
android:layout_toRightOf
="@+id/pic1"
           
android:src
="@drawable/pic3"

            >

        </ImageView>

        <ImageView
           
android:id
="@+id/pic3"
           
android:layout_width
="150dp"
           
android:layout_height
="150dp"
           
android:layout_below
="@+id/pic1"
           
android:src="@drawable/pic1"
>

        </ImageView>

        <ImageView
           
android:id
="@+id/pic4"
           
android:layout_width
="150dp"
           
android:layout_height
="150dp"
           
android:layout_below
="@+id/pic2"
           
android:layout_toRightOf
="@+id/pic3"
           
android:src="@drawable/pic2"
>

        </ImageView>

    </RelativeLayout>

    <TextView
       
android:id
="@+id/footer"
       
android:layout_alignParentBottom
="true"
       
android:layout_width
="match_parent"
       
android:layout_height
="wrap_content"
       
android:text="
כל הזכויות שמורות (c)"
       
android:textSize="18dp"
       
android:gravity
="center"
       
>

    </TextView>

</RelativeLayout>

 

JAVA

package com.contineo.mygames;

import
androidx.appcompat.app.AppCompatActivity;

import
android.content.Intent;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.Button;

public class MainActivity extends
AppCompatActivity {

    Button
b1,b2,b3
;
   
@Override
   
protected void
onCreate(Bundle savedInstanceState) {
       
super
.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main
);
       
b1 = findViewById(R.id.ticBTN
);
    }



   
public void
fourInARow(View view) {
        Intent intent =
new Intent(this,fourInARow.class
);
        startActivity(intent);
    }

   
public void
TicTacToe(View view) {
            Intent intent =
new Intent(this,tictactoe.class
);
            startActivity(intent);

    }

   
public void
goToPaint(View view) {
        Intent intent =
new Intent(this,MyPaint.class
);
        startActivity(intent);
    }

}

 

MyPaint

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android
="http://schemas.android.com/apk/res/android"
   
xmlns:app
="http://schemas.android.com/apk/res-auto"
   
xmlns:tools
="http://schemas.android.com/tools"
   
android:layout_width
="match_parent"
   
android:layout_height
="match_parent"
   
tools:context=".MyPaint"
>

    <
LinearLayout
       
android:id
="@+id/mainFrame"
       
android:layout_width
="match_parent"
       
android:layout_height
="match_parent"
       
android:weightSum
="100"
       
android:orientation="vertical"
>

        <
LinearLayout
           
android:id
="@+id/upperFrame"
           
android:layout_weight
="10"
           
android:layout_width
="match_parent"
           
android:layout_height
="wrap_content"
           
android:orientation
="horizontal"
           
android:weightSum
="4"
           
android:layout_gravity="top"
>

            <
Button
               
android:id
="@+id/btnColor1"
               
android:layout_width
="wrap_content"
               
android:layout_height
="wrap_content"
               
android:text
=""
               
android:background
="#D31B9E"
               
android:tag
="#D31B9E"
               
android:layout_weight
="1"
               
android:onClick
="changeColor"
               
android:layout_margin="5dp"
>

            </
Button
>
            <
Button
               
android:id
="@+id/btnColor2"
               
android:layout_width
="wrap_content"
               
android:layout_height
="wrap_content"
               
android:text
=""
               
android:background
="#364FDA"
               
android:tag
="#364FDA"
               
android:layout_weight
="1"
               
android:onClick
="changeColor"
               
android:layout_margin="5dp"
>

            </
Button
>
            <
Button
               
android:id
="@+id/btnColor3"
               
android:layout_width
="wrap_content"
               
android:layout_height
="wrap_content"
               
android:text
=""
               
android:background
="#149E91"
               
android:layout_weight
="1"
               
android:tag
="#149E91"
               
android:onClick
="changeColor"
               
android:layout_margin="5dp"
>

            </
Button
>
            <
Button
               
android:id
="@+id/btnColor4"
               
android:layout_width
="wrap_content"
               
android:layout_height
="wrap_content"
               
android:text
=""
               
android:background
="#FFC107"
               
android:layout_weight
="1"
               
android:tag
="#FFC107"
               
android:onClick
="changeColor"
               
android:layout_margin="5dp"
>

            </
Button
>

        </
LinearLayout
>

        <
FrameLayout
           
android:id
="@+id/frmPaint"
           
android:layout_width
="match_parent"
           
android:layout_height
="match_parent"
           
android:layout_weight="80"
>

        </
FrameLayout
>

        <
LinearLayout
           
android:id
="@+id/lowerButtons"
           
android:layout_weight
="10"
           
android:layout_width
="match_parent"
           
android:layout_height
="wrap_content"
           
android:orientation
="horizontal"
           
>
            <
Button
               
android:id
="@+id/addLine"
               
android:layout_width
="wrap_content"
               
android:layout_height
="wrap_content"
               
android:textSize
="20sp"
               
android:textColor
="#ffffff"
               
android:text="
הוסף קו"
               
android:background="@drawable/buttonshape"
               
android:shadowColor
="#A8A8A8"
               
android:shadowDx
="0"
               
android:shadowDy
="0"
               
android:onClick
="addLine"
               
android:shadowRadius
="0"
               
>

            </
Button
>

            <
Button
               
android:id
="@+id/addCircle"
               
android:layout_width
="wrap_content"
               
android:layout_height
="wrap_content"
               
android:textSize
="20sp"
               
android:textColor
="#ffffff"
               
android:text="
הוסף עיגול"
               
android:background="@drawable/buttonshape"
               
android:shadowColor
="#A8A8A8"
               
android:shadowDx
="0"
               
android:shadowDy
="0"
               
android:onClick
="addCircle"
               
android:shadowRadius
="0"
               
>

            </
Button
>
            <
Button
               
android:id
="@+id/addRect"
               
android:layout_width
="wrap_content"
               
android:layout_height
="wrap_content"
               
android:textSize
="20sp"
               
android:textColor
="#ffffff"
               
android:text="
הוסף מלבן"
               
android:background="@drawable/buttonshape"
               
android:shadowColor
="#A8A8A8"
               
android:shadowDx
="0"
               
android:shadowDy
="0"
               
android:onClick
="addRect"
               
android:shadowRadius
="0"
               
>

            </
Button
>

            <
Button
               
android:id
="@+id/undo"
               
android:layout_width
="wrap_content"
               
android:layout_height
="wrap_content"
               
android:textSize
="20sp"

               
android:textColor
="#ffffff"
               
android:text="
בטל"
               
android:background="@drawable/buttonshape"
               
android:shadowColor
="#A8A8A8"
               
android:shadowDx
="0"
               
android:shadowDy
="0"
               
android:onClick
="undo"
               
android:shadowRadius
="0"
               
>

            </
Button
>
        </
LinearLayout
>



    </
LinearLayout
>

</
RelativeLayout
>

 

JAVA

package com.contineo.mygames;

import
androidx.appcompat.app.AppCompatActivity;

import
android.os.Bundle;
import
android.util.Log;
import
android.view.View;
import
android.widget.Button;
import
android.widget.FrameLayout;

public class MyPaint extends
AppCompatActivity {

   
public final int TYPE_LINE = 1
;
   
public final int TYPE_RECT = 2
;
   
public final int TYPE_CIRCLE = 3
;

   
private FrameLayout frmpaint
;
   
private PaintView paintView
;
    String
colorStr
;
    Button
btnBlue,btnGreen,btnPink,btnYellow,btnLine,btnCircle,btnRect,btnUndo
;
   
@Override
   
protected void
onCreate(Bundle savedInstanceState) {
       
super
.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_my_paint
);
       
frmpaint = findViewById(R.id.frmPaint
);
       
paintView = new PaintView(this,"#D31B9E"
);
       
frmpaint.addView(paintView
);
       
colorStr = ""
;
       
btnBlue = findViewById(R.id.btnColor2
);
       
btnGreen = findViewById(R.id.btnColor3
);
       
btnPink = findViewById(R.id.btnColor1
);
       
btnYellow = findViewById(R.id.btnColor4
);

       
btnLine = findViewById(R.id.addLine
);
       
btnCircle = findViewById(R.id.addCircle
);
       
btnRect = findViewById(R.id.addRect
);
       
btnUndo = findViewById(R.id.undo
);

       
btnPink.setAlpha(1
);
       
btnYellow.setAlpha(0.5f
);
       
btnBlue.setAlpha(0.5f
);
       
btnGreen.setAlpha(0.5f
);
       
btnLine.setAlpha(1
);
       
btnRect.setAlpha(0.5f
);
       
btnCircle.setAlpha(0.5f
);
       
btnUndo.setAlpha(0.5f
);
    }


   
public void
addLine(View view) {
       
paintView
.addLine();
       
btnLine.setAlpha(1
);
       
btnRect.setAlpha(0.5f
);
       
btnCircle.setAlpha(0.5f
);
       
btnUndo.setAlpha(0.5f
);

    }

   
public void
addCircle(View view) {
       
paintView
.addCircle();
       
btnLine.setAlpha(0.5f
);
       
btnRect.setAlpha(0.5f
);
       
btnCircle.setAlpha(1
);
       
btnUndo.setAlpha(0.5f
);
    }


   
public void
addRect(View view) {
       
paintView
.addRect();
       
btnLine.setAlpha(0.5f
);
       
btnRect.setAlpha(1
);
       
btnCircle.setAlpha(0.5f
);
       
btnUndo.setAlpha(0.5f
);
    }

   
public void
undo(View view) {
       
paintView
.undo();
    }

   
public void
changeColor(View view) {
       
colorStr
= view.getTag().toString();
       
paintView.setCurrColor(colorStr
);
       
if (view.getId() == R.id.btnColor1
){
           
btnPink.setAlpha(1
);
           
btnYellow.setAlpha(0.5f
);
           
btnBlue.setAlpha(0.5f
);
           
btnGreen.setAlpha(0.5f
);
        }
       
if (view.getId() == R.id.btnColor2
){
           
btnPink.setAlpha(0.5f
);
           
btnYellow.setAlpha(0.5f
);
           
btnBlue.setAlpha(1
);
           
btnGreen.setAlpha(0.5f
);
        }
       
if (view.getId() == R.id.btnColor3
){
           
btnPink.setAlpha(0.5f
);
           
btnYellow.setAlpha(0.5f
);
           
btnBlue.setAlpha(0.5f
);
           
btnGreen.setAlpha(1
);
        }
       
if (view.getId() == R.id.btnColor4
){
           
btnPink.setAlpha(0.5f
);
           
btnYellow.setAlpha(1
);
           
btnBlue.setAlpha(0.5f
);
           
btnGreen.setAlpha(0.5f
);
        }

    }
}

 

MyShape

package com.contineo.mygames;
import
android.graphics.Canvas;
import
android.graphics.Color;
import
android.graphics.Paint;

public class
MyShape {

   
public final int TYPE_LINE = 1
;
   
public final int TYPE_RECT = 2
;
   
public final int TYPE_CIRCLE = 3
;

   
public int type
;
   
public int startX,startY
;
   
public String color
;

   
public MyShape(int startX, int
startY, String color) {
       
this.startX
= startX;
       
this.startY
= startY;
       
this.color
= color;
    }

   
public void
draw(Canvas canvas,Paint paint){
        paint.setColor(Color.parseColor(
color
));
    }
   
public void updatePoint(int xe,int
ye){

    }

   
public int
getType() {
       
return type
;
    }

   
public void setType(int
type) {
       
this.type
= type;
    }

   
public int
getStartX() {
       
return startX
;
    }

   
public void setStartX(int
startX) {
       
this.startX
= startX;
    }

   
public int
getStartY() {
       
return startY
;
    }

   
public void setStartY(int
startY) {
       
this.startY
= startY;
    }

   
public
String getColor() {
       
return color
;
    }

   
public void
setColor(String color) {
       
this.color
= color;
    }
}

 

MyRect

package com.contineo.mygames;

import
android.graphics.Canvas;
import
android.graphics.Paint;

public class MyRect extends
MyShape {
   
public int xStart
;
   
public int yStart
;
   
private int xEnd
;
   
private int yEnd
;

   
public MyRect (int xStart,int yStart,int xEnd, int
yEnd, String color){
       
super
(xStart,yStart,color);
       
this.xStart
= xStart;
       
this.yStart
= yStart;
       
super.type = TYPE_RECT
;
       
this.xEnd
= xEnd;
       
this.yEnd
= yEnd;

    }

   
public void updatePoint(int xe, int
ye) {
       
xEnd
= xe;
       
yEnd
= ye;
    }

   
public void
draw(Canvas canvas, Paint paint) {
       
super.draw(canvas,paint);
// change color
       
canvas.drawRect(xStart,yStart,xEnd,yEnd
,paint);
    }


}

 

MyCircle

package com.contineo.mygames;

import
android.graphics.Canvas;
import
android.graphics.Paint;

public class MyCircle extends
MyShape{
   
public int xCenter
;
   
public int yCenter
;
   
public float radius
;


   
public MyCircle(int xCenter, int yCenter, float
radius,String color) {
       
super
(xCenter,yCenter,color);
       
this.xCenter
= xCenter;
       
this.yCenter
= yCenter;
       
this.radius
= radius;
       
super.type = TYPE_CIRCLE
;
    }




   
public int
getxCenter() {
       
return xCenter
;
    }

   
public void setxCenter(int
xCenter) {
       
this.xCenter
= xCenter;
    }

   
public int
getyCenter() {
       
return yCenter
;
    }

   
public void setyCenter(int
yCenter) {
       
this.yCenter
= yCenter;
    }

    
public float
getRadius() {
       
return radius
;
    }

   
public void setRadius(float
radius) {
       
this.radius
= radius;
    }

   
public void
draw(Canvas canvas, Paint paint) {
       
super.draw(canvas,paint);
// change color
       
canvas.drawCircle(xCenter,yCenter, radius
,paint);
    }
   
public void updatePoint(int xe, int
ye) {
       
int dx = xCenter
-xe;
       
int dy = yCenter
-ye;
       
radius = (float
)Math.sqrt(dx*dx+dy*dy);
    }

}

PaintView

 

package com.contineo.mygames;

import
android.content.Context;
import
android.graphics.Canvas;
import
android.graphics.Color;
import
android.graphics.Paint;
import
android.graphics.Picture;
import
android.util.Log;
import
android.view.MotionEvent;
import
android.view.View;
import
android.widget.Switch;

import
java.util.ArrayList;
import
java.util.List;

public class PaintView extends
View {

   
public final int TYPE_LINE = 1
;
   
public final int TYPE_RECT = 2
;
   
public final int TYPE_CIRCLE = 3
;

   
private Paint paint
;
   
private Paint bgPaint
;
   
private String currColor
;
   
int draw_type
;

    ArrayList<MyShape>
shapeList
;


   
private MyShape shape
;
   
public
PaintView(Context context,String currColor) {
       
super
(context);
       
shapeList = new
ArrayList<MyShape>();

       
paint = new
Paint();
       
bgPaint = new
Paint();
       
this.currColor
= currColor;
       
paint.setStyle(Paint.Style.STROKE
);
       
paint.setStrokeWidth(15
);
       
bgPaint.setColor(Color.WHITE
);
       
draw_type = TYPE_LINE
;

       
paint.setColor(Color.parseColor("#364FDA"
));
       
shape = null
;
        Log.d(
"PaintView", "Constructor: "
);
    }

   
@Override
   
protected void
onDraw(Canvas canvas) {
        Log.d(
"PaintView", "onDraw: "
);
        canvas.drawPaint(
bgPaint
);
        drawAllShapes(canvas);

    }

   
private void
drawAllShapes(Canvas canvas) {
       
int len = shapeList
.size();
       
for (int i=0
;i<len;i++){
           
shapeList.get(i).draw(canvas,paint
);
        }
    }

   
@Override
   
public boolean
onTouchEvent(MotionEvent event) {

       
int
x1,y1,x2,y2;
       
if(event.getAction() == MotionEvent.ACTION_DOWN
){
            x1 = (
int
)event.getX();
            y1 = (
int
)event.getY();
            x2 = (
int
)event.getX();
            y2 = (
int
)event.getY();
           
if (draw_type == TYPE_LINE
){
               
shape = new MyLine(x1,y1,x2,y2,10,currColor
);
                Log.d(
"PaintView ", "onTouchEvent *** ACTION_DOWN *** : x1 =  "+x1+" y1 = "+y1+" x2 = "+x2+" y2 = "
+y2);
            }
           
if (draw_type == TYPE_CIRCLE
){
               
float
r = diff(x1,y1,x2,y2);
               
shape = new MyCircle(x1,y1,r,currColor
);
            }
           
if (draw_type == TYPE_RECT
){
               
shape = new MyRect(x1,y1,x2,y2,currColor
);
            }

           
if (draw_type != 0
){
               
shapeList.add(shape
);
                invalidate();
            }
        }
       
if (event.getAction() == MotionEvent.ACTION_MOVE
){
           
if (draw_type != 0
){
               
int xEnd = (int
)event.getX();
               
int yEnd = (int
) event.getY();
               
shape
.updatePoint(xEnd,yEnd);
                Log.d(
"PaintView ", "onTouchEvent *** ACTION_MOVE *** : xEnd =  "+xEnd+" yEnd = "
+yEnd);

            }
            invalidate();
        }

       
return true
;
    }

   
private float diff(int x1,int y1,int x2,int
y2){
       
int
dx = x1-x2;
       
int
dy = y1-y2;
       
return (float
) Math.sqrt(dx*dx + dy*dy);
    }
   
public void
addLine() {
      
draw_type = TYPE_LINE
;
    }
   
public void
addCircle(){
       
draw_type = TYPE_CIRCLE
;

    }
   
public void
addRect(){
       
draw_type = TYPE_RECT
;
    }

   
public void
setCurrColor(String color){
       
this.currColor
= color;
    }
   
public void
undo() {
       
int len = shapeList
.size();
       
if (len > 0
){
           
shapeList.remove(len-1
);
            invalidate();
        }

    }

}