-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#22 Echange des Information entre Joueur et Serveur
- Loading branch information
1 parent
dd577dc
commit 83d33bd
Showing
12 changed files
with
355 additions
and
313 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
199 changes: 92 additions & 107 deletions
199
android/app/src/main/java/projet/license/PaintView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,161 +1,146 @@ | ||
package projet.license; | ||
|
||
import android.content.Context; | ||
import android.graphics.Bitmap; | ||
import android.graphics.Canvas; | ||
import android.graphics.Color; | ||
import android.graphics.Paint; | ||
import android.graphics.Path; | ||
import android.util.AttributeSet; | ||
import android.util.Log; | ||
import android.util.Base64; | ||
import android.view.MotionEvent; | ||
import android.view.View; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.io.ByteArrayOutputStream; | ||
|
||
|
||
public class PaintView extends View implements View.OnTouchListener { | ||
private final int defaultDot = 10; | ||
private final int defaultColor = Color.GRAY; | ||
private int mPenColor; | ||
private int mDotSize; | ||
private float mX,mY,mOldX,mOldY; | ||
public int countTicks = 0; | ||
private ArrayList<Path> mPaths; | ||
private ArrayList<Paint> mPaints; | ||
public class PaintView extends View { | ||
public Bitmap mBitmap; | ||
public Canvas mCanvas; | ||
private Path mPath; | ||
private Paint mPaint; | ||
private float[] coord; | ||
private Canvas canvas; | ||
|
||
private Paint mBitmapPaint; | ||
public Paint mPaint; | ||
public int countTicks = 0; | ||
|
||
|
||
public PaintView(Context c, AttributeSet attrs) { | ||
super(c, attrs); | ||
|
||
public PaintView(Context context) { | ||
super(context); | ||
this.init(); | ||
} | ||
mPath = new Path(); | ||
mBitmapPaint = new Paint(Paint.DITHER_FLAG); | ||
|
||
private void init() { | ||
this.mDotSize = defaultDot; | ||
mPaint = new Paint(); | ||
mPaint.setAntiAlias(true); | ||
mPaint.setDither(true); | ||
mPaint.setColor(0xFF000000); | ||
mPaint.setStyle(Paint.Style.STROKE); | ||
mPaint.setStrokeJoin(Paint.Join.ROUND); | ||
mPaint.setStrokeCap(Paint.Cap.ROUND); | ||
mPaint.setStrokeWidth(9); | ||
mPaint.setColor(Color.WHITE); | ||
this.setBackgroundColor(Color.BLACK); | ||
this.mPenColor = defaultColor; | ||
this.mPaths = new ArrayList<Path>(); | ||
this.mPaints = new ArrayList<Paint>(); | ||
this.mPath = new Path(); | ||
this.addPath(false); | ||
this.mX = this.mY = this.mOldY = this.mOldX = (float) 0.0; | ||
this.countTicks = 0; | ||
this.setOnTouchListener(this); | ||
this.coord = new float[]{}; | ||
} | ||
|
||
|
||
} | ||
public int getTicks(){ | ||
return this.countTicks; | ||
@Override | ||
protected void onSizeChanged(int w, int h, int oldw, int oldh) { | ||
super.onSizeChanged(w, h, oldw, oldh); | ||
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); | ||
mCanvas = new Canvas(mBitmap); | ||
} | ||
|
||
public void addPath(boolean fill){ | ||
mPath = new Path(); | ||
mPaths.add(mPath); | ||
mPaint = new Paint(); | ||
mPaints.add(mPaint); | ||
mPaint.setColor(mPenColor); | ||
if (!fill) | ||
mPaint.setStyle(Paint.Style.STROKE); | ||
mPaint.setStrokeWidth(mDotSize); | ||
} | ||
protected void onDraw(Canvas canvas){ | ||
super.onDraw(canvas); | ||
for (int i = 0; i<mPaths.size();i++) | ||
canvas.drawPath(mPaths.get(i),mPaints.get(i)); | ||
canvas.drawPath(mPath,mPaint); | ||
} | ||
@Override | ||
protected void onDraw(Canvas canvas) { | ||
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); | ||
|
||
canvas.drawPath(mPath, mPaint); | ||
|
||
|
||
public void setPenColor(int penColor){ | ||
this.mPenColor = penColor; | ||
this.mPaint.setColor(mPenColor); | ||
} | ||
|
||
private float mX, mY; | ||
private static final float TOUCH_TOLERANCE = 4; | ||
|
||
public PaintView(Context context, AttributeSet attrs){ | ||
super(context,attrs); | ||
this.init(); | ||
private void touch_start(float x, float y) { | ||
mPath.reset(); | ||
mPath.moveTo(x, y); | ||
mX = x; | ||
mY = y; | ||
} | ||
|
||
public PaintView(Context context,AttributeSet attrs,int defSystelAttr){ | ||
super(context,attrs,defSystelAttr); | ||
this.init(); | ||
} | ||
|
||
public void reset(int PenColor) { | ||
this.init(); | ||
this.invalidate(); | ||
setPenColor(PenColor); | ||
this.countTicks = 0; | ||
private void touch_move(float x, float y) { | ||
float dx = Math.abs(x - mX); | ||
float dy = Math.abs(y - mY); | ||
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { | ||
mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2); | ||
mX = x; | ||
mY = y; | ||
} | ||
} | ||
public void addTick(){ | ||
this.countTicks = this.countTicks + 1; | ||
private void touch_up() { | ||
mPath.lineTo(mX, mY); | ||
// commit the path to our offscreen | ||
mCanvas.drawPath(mPath, mPaint); | ||
// kill this so we don't double draw | ||
mPath.reset(); | ||
} | ||
|
||
@Override | ||
public boolean onTouch(View v, MotionEvent event) { | ||
mX = event.getX(); | ||
mY = event.getY(); | ||
Log.d("Touched : "," ("+mX + "," + mY + ")"); | ||
switch (event.getAction()){ | ||
public boolean onTouchEvent(MotionEvent event) { | ||
float x = event.getX(); | ||
float y = event.getY(); | ||
|
||
switch (event.getAction()) { | ||
case MotionEvent.ACTION_DOWN: | ||
this.addPath(true); | ||
this.mPath.addCircle(mX,mY,mDotSize/2,Path.Direction.CW); | ||
this.addPath(false); | ||
this.mPath.moveTo(mX,mY); | ||
this.addTick(); | ||
touch_start(x, y); | ||
invalidate(); | ||
break; | ||
case MotionEvent.ACTION_MOVE: | ||
this.mPath.lineTo(mX,mY); | ||
|
||
touch_move(x, y); | ||
invalidate(); | ||
break; | ||
case MotionEvent.ACTION_UP: | ||
this.addPath(true); | ||
if (mOldX == mX && mOldY == mY) | ||
this.mPath.addCircle(mX,mY,mDotSize/2,Path.Direction.CW); | ||
|
||
touch_up(); | ||
invalidate(); | ||
break; | ||
} | ||
this.addElement(coord, mX); | ||
this.addElement(coord, mY); | ||
this.invalidate(); | ||
mOldX = mX; | ||
mOldY = mY; | ||
return true; | ||
|
||
} | ||
|
||
public float[] getCoord(){ | ||
return this.coord; | ||
public Bitmap getBitmap() | ||
{ | ||
//this.measure(100, 100); | ||
//this.layout(0, 0, 100, 100); | ||
this.setDrawingCacheEnabled(true); | ||
this.buildDrawingCache(); | ||
Bitmap bmp = Bitmap.createBitmap(this.getDrawingCache()); | ||
this.setDrawingCacheEnabled(false); | ||
|
||
|
||
return bmp; | ||
} | ||
|
||
private float[] addElement(float[] a, float e) { | ||
a = Arrays.copyOf(a, a.length + 1); | ||
a[a.length - 1] = e; | ||
return a; | ||
public void addTick(){ | ||
this.countTicks = this.countTicks + 1; | ||
} | ||
|
||
public void drawline(Canvas canvas){ | ||
for(int i = 0; i < coord.length - 4; i ++){ | ||
|
||
canvas.drawLine(coord[i],coord[i+1],coord[i+2],coord[i+3],this.mPaint); | ||
} | ||
public void clear(){ | ||
mBitmap.eraseColor(Color.BLACK); | ||
invalidate(); | ||
System.gc(); | ||
|
||
} | ||
public Canvas getCanvas(){ | ||
return this.canvas; | ||
public String encodeBitMap(){ | ||
Bitmap bmp = this.mBitmap; | ||
ByteArrayOutputStream stream = new ByteArrayOutputStream(); | ||
bmp.compress(Bitmap.CompressFormat.PNG,100,stream); | ||
byte[] byteArray = stream.toByteArray(); | ||
String encoded = Base64.encodeToString(byteArray,Base64.DEFAULT); | ||
return encoded; | ||
} | ||
|
||
public Paint getmPaint(){ | ||
return this.mPaint; | ||
public Integer getTicks(){ | ||
return this.countTicks; | ||
} | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.