cyclic drawing in KeyboardView - java

I have class
public class OwnKeyboardView extends KeyboardView {
...
}
Its have override method OnDraw:
#Override
public void onDraw(final Canvas canvas) {
super.onDraw(canvas);
mCanvas = draw_bitmap(canvas);
Draw_Bitmap(mCanvas);
}
Class KeyboardView Inherited Methods
From class android.view.View
From class java.lang.Object
From interface android.graphics.drawable.Drawable.Callback
Method OnDraw working at press any key.
When i do next:
public Canvas draw_bitmap(Canvas canvas) {
if (mCanvas != null) {
Log.i(TAG, "Copy Canvas");
}
return canvas;
}
Why in method OnDraw i can draw with mCanvas, but other don't can? I want cycle drawing on Keyboard, but OnDraw work only when you press on keyboard

Yoy may create Thread and in the Thread manually call postInvalidate, like this.
Note: It will drain the battery much
boolean flag=true;
private void Thread_Draw(){
new Thread(new Runnable(){
public void run(){
while(flag)
{
postInvalidate();
try{
Thread.sleep(45);
} catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
}
Stop thread you can from method
public void stopThread(boolean bool)
{
flag=bool;
}
read about PostInvalidate yom can this: http://developer.android.com/reference/android/view/View.html#postInvalidate()

Related

Java - Different paint with multiple threads that have thread.sleep

I'm newbie and I'm trying to make a game but than I don't understand how to use repaint() from different paint with different threads too. One thread with thread.sleep and the other one doesn't have.
Here's my piece of code :
GamePanel :
public class GamePanel extends JPanel implements MouseListener, MouseMotionListener{
EnemyEngine enemyE = new EnemyEngine();
public GamePanel() {
new Thread(new Runnable() {
#Override
public void run() {
while(true){
repaint();
}
}
}).start();
new Thread(new Runnable() {
#Override
public void run() {
while(true){
enemyE.update();
enemyE.repaint();
try {
Thread.sleep(1000/10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
public void paintComponent(Graphics g) {
// board painting
}
}
EnemyEngine:
public class EnemyEngine extends JPanel{
Vector<Enemy> enemyVect = new Vector<>();
Random rand = new Random();
public void paintComponent(Graphics g){
for (Enemy enemy : enemyVect) {
enemy.render(g);
}
}
public void update() {
for (Enemy enemy : enemyVect) {
enemy.move();
}
}
}
I have already search on the internet but it still didn't work...or maybe I'm the stupid one :/
Please help me senpai
I don't understand clearly your issue about repaint?
If you want to refresh ui immediately, why don't you use paintImmediately(0, 0, getWidth(), getHeight());

Android: Override onDraw() or run() in order to draw on Canvas

I want to do a simple animation by drawing on canvas in Android Studio. I followed this tutorial series to set up the basic java code (see my own code below):
https://www.youtube.com/watch?v=UH2OWnq7NQ4
He is overwriting the run() method to do all the drawing, however everybody else seems to do it in the onDraw() method. Which method is generally better? Will I run into problems when doing it this way?
public class AnimationActivity extends SurfaceView implements Runnable {
Thread thread = null;
boolean canDraw = false;
Bitmap backgroundBlack;
Canvas canvas;
SurfaceHolder surfaceHolder;
public AnimationActivity(Context context) {
super(context);
surfaceHolder = getHolder();
backgroundBlack = BitmapFactory.decodeResource(getResources(), R.drawable.background_black);
}
#Override
public void run() {
prepPaintBrushes();
while (canDraw) {
if (!surfaceHolder.getSurface().isValid()) {
continue;
}
canvas = surfaceHolder.lockCanvas();
canvas.drawBitmap(backgroundBlack,0,0, null);
//drawing is done here
surfaceHolder.unlockCanvasAndPost(canvas);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void pause() {
canDraw = false;
while (true) {
try {
thread.join();
break;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
thread = null;
}
public void resume() {
canDraw = true;
thread = new Thread(this);
thread.start();
}
}
I am not familiar with this particular API, but from my experience with graphics programming, writing your own rendering loop seems very suspicious to me. Calling sleep() is also a big no-no. Both of these will lead to an unresponsive UI.
I suggest that you read the documentation and tutorials for SurfaceView on the official Android site.

Applet code for moving banner

This is my applet code for a moving banner .It is working properly but there is one doubt .
import java.applet.*;
import java.awt.*;
/*<html>
<applet code ="SimpleBanner" width="2000" height="2000"></applet></html>*/
public class SimpleBanner extends Applet implements Runnable{
String msg=" A Simple Banner Is Moving";
boolean flag=false;
Font f=new Font("TimesRoman",Font.BOLD,50);
int i=10;
public void init()
{
setBackground(Color.gray);
setFont(f);
setForeground(Color.green);
}
public void start()
{
Thread t=new Thread(this);
t.start();
}
public void run()
{
for(;;)
{
try{
repaint();
if(flag)
break;
Thread.sleep(250);
}catch(InterruptedException e){}
}
}
public void paint(Graphics g)
{
char ch=msg.charAt(0);
msg=msg.substring(1,msg.length());
msg+=ch;
drawString(msg,300,100);
}
public void stop()
{
flag=true;
}
}
As you can see I am not extending Graphics class and I am using setFont() method defined in Graphics class without its object how this is possible? And if I try to call drawString() method in paint() method without g it is not working.

Error while creating a class from SurfaceView, which is to be run as a thread

I know the basic concepts of OOP, I am trying my hands on Android programming.(Games in particular). I am trying to implement a game project from this book Android Game programming by example. I am able to understand theoretically what is being said. But when I put together the pieces of code, into a single java class TDView.java file, I get an error in Android studio. Maybe I misunderstood some phrase and Put a piece where I wasn't suppose to put it.
The errors are labeled error1 and error2 in the code below:
package com.gamecodeschool.c1tappydefender;
import android.content.Context;
import android.view.SurfaceView;
public class TDView extends SurfaceView implements Runnable {
volatile boolean playing;
Thread gameThread = null;
#Override
public void run() {
while (playing) {
update();
draw();
control();
} //error2: it says a semicolon is needed here.
private void update(){
}
private void draw(){
}
private void control(){
}
}// error1: it says class or interface missing
public TDView(Context context) {
super(context);
}
// Clean up our thread if the game is interrupted or the player quits
public void pause() {
playing = false;
try {
gameThread.join();
} catch (InterruptedException e) {
}
}
// Make a new thread and start it
// Execution moves to our R
public void resume() {
playing = true;
gameThread = new Thread(this);
gameThread.start();
}
}
Move your method declaration outside of the declaration of your run method. this should do the trick
public void run() {
while (playing) {
update();
draw();
control();
}
}
private void update(){
}
private void draw(){
}
private void control(){
}

How to pass a variable to a inner class

I want to use canvas inside mousePressed. How can I do that?
public DragManager(Canvas canvas)
{
canvas.addMouseListener(new MouseAdapter() {
#Override public void mousePressed(MouseEvent e)
{
canvas.something(); // does not work.
}
});
}
As many of the guys over here already said you have to make function parameter final.
public DragManager(final Canvas canvas)
{
canvas.addMouseListener(new MouseAdapter() {
#Override public void mousePressed(MouseEvent e)
{
canvas.something();
}
});
}
That means that this variable cannot point to any other object. E.g. you cannot do this inside function:
canvas = SomeOtherCanvas
If you create an object using a local class definition, that object can keep "living" after local variables have been discarded from the stack (after DragManager constructor completion). It has to have a copy of the local values. If you make this parameter final (so it's guaranteed that reference inside constructor wouldn't point to some other place) it's really easy to have a copy: just copy a reference. If there was no such rule you (well, not you personally, but Java language) would need to constantly sync those values and that would be much more complex and slow solution.
Make the parameter final:
public DragManager(final Canvas canvas)
you Cannot refer to non final variable inside an inner class defined. mark your canvas as final.
public void DragManager(final Canvas canvas)
{
canvas.addMouseListener(new MouseAdapter() {
#Override public void mousePressed(MouseEvent e)
{
System.out.println(canvas);;// does not work.
}
});
public DragManager(final Canvas canvas)
{
canvas.addMouseListener(new MouseAdapter() {
#Override public void mousePressed(MouseEvent e)
{
canvas.something(); // does work.
}
});
}
since you can modify canvas variable, you should define it as final(constant reference).
without using final keyword. you can add Init method that return this and add private variable.
pass canvas by Call Init method.
public DragManager(Canvas canvas)
{
canvas.addMouseListener(new MouseAdapter() {
Canvas _canvas;
#Override public void mousePressed(MouseEvent e)
{
_canvas.something(); // does not work.
}
public MouseAdapter Init(Canvas canvas){
_canvas = canvas;
return this;
}
}.Init(canvas));
}

Categories

Resources