2011-08-23 2 views
0

어떻게하면 페인트에서 여러 번 호출 할 수 있습니까?하지만 무효화를 시도했지만 아무도 페인트를 여러 번 호출하는 샘플 코드를 제공 할 수 있다고 생각하지 않습니다.블랙 베리에서 페인트 방법을 호출하는 방법

package mypackage; 

import com.rss.logger.Log; 

import net.rim.device.api.system.Bitmap; 
import net.rim.device.api.ui.Graphics; 
import net.rim.device.api.ui.UiApplication; 
import net.rim.device.api.ui.component.BitmapField; 
import net.rim.device.api.ui.component.LabelField; 
import net.rim.device.api.ui.container.MainScreen; 

/** 
* A class extending the MainScreen class, which provides default standard 
* behavior for BlackBerry GUI applications. 
*/ 
public final class MyScreen extends MainScreen 
{ 
    BitmapField objBitmapField; 
    boolean objBoolean; 
    int postion=0; 
    public MyScreen() 
    {   
     objBitmapField=new BitmapField(Bitmap.getBitmapResource("bb.png")); 
     // Set the displayed title of the screen  
     setTitle("MyTitle"); 
     objBoolean=false; 
     new AnimationThread().start(); 

    } 
    private class AnimationThread extends Thread{ 

     public void run() { 
      super.run(); 
      UiApplication.getUiApplication().invokeLater(new Runnable() { 
        public void run() 
        { 
         objBoolean=true; 
        //Add a new LabelField to the screen. 
        //theScreen.add(new LabelField("Hello there."); 

        //Call the screen’s invalidate method to 
        //force the screen to redraw itself. 
        //Note that invalidate can be called 
        //at the screen, manager or field level, 
        //which means you can inform the 
        //BlackBerry to only redraw the area that 
        //has changed. 

         for (int i = 0; i < 20; i++) { 
          try { 
          sleep(200); 
         } catch (InterruptedException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
         Log.info("in run"); 
          postion=postion+5; 
          MyScreen.this.invalidate(); 
        } 


        } 
       }); 
     } 

    } 

    protected void paint(Graphics graphics) { 

     super.paint(graphics); 
     Log.info("in paint"); 
     if(objBoolean)   
     graphics.drawBitmap(20, postion, 50, 50, Bitmap.getBitmapResource("bb.png"), 30, 40); 

    } 

} 
+0

당신이 무효로 전화하는거야 방법에 대한 코드 샘플을 제공하십시오() –

답변

0

안녕하세요 piyus는 작동 코드를 찾습니다. invalidate()는 paint() 메소드를 호출 할 필요가있다. 그리고 페인트 방법에서 객체를 만들어서는 안되는 것도 중요합니다.

import net.rim.device.api.system.Bitmap; 
import net.rim.device.api.ui.Graphics; 
import net.rim.device.api.ui.container.MainScreen; 

public final class MyScreen extends MainScreen 
{ 
    private boolean objBoolean=false; 
    private int postion=0; 
    private Bitmap bb=Bitmap.getBitmapResource("bb.png"); 

    public MyScreen() 
    {   
     setTitle("MyTitle"); 
     new AnimationThread().start(); 
    } 

    private class AnimationThread extends Thread 
    { 
     public void run() 
     { 
      objBoolean=true; 

      for (int i = 0; i < 20; i++) 
      { 
       try 
       { 
        sleep(200); 
       } 
       catch (InterruptedException e) 
       { 
       } 
       postion=postion+5; 
      } 
     } 
    } 

    protected void paint(Graphics graphics) 
    { 
     super.paint(graphics); 
     if(objBoolean)   
      graphics.drawBitmap(20, postion, 50, 50, bb, 30, 40); 
     invalidate(); 
    } 
} 
+0

우리는 graphics.drawBitmap (20, postion, 50, 50, bb, 30, 40)과 같은 drawBitmap 메서드를 사용하기 때문에 비트 맵의 ​​크기는 30x40보다 커야합니다. . –

+0

코드를 사용해 주셔서 감사합니다. 어떻게 작동하는지 알 수 있습니다. – Piyush

+0

이미지를 한 위치에서 다른 위치로 옮기고 싶습니다. –

0

페인트() 메소드는 무효화 방법을 사용하거나 당신이 지금 사용하고있는 클래스의 객체를 생성하여 중 하나를 호출 할 수 있습니다.

여기서 언급 한 샘플 코드에는 Typo 오류가 있다고 생각합니다. 클래스를 정의하는 것만으로 클래스 안에 클래스를 가질 수는 없습니다. 당신이 직면 한 문제로 간다

무효화 메서드를 호출하는 것은 그것을 사용하는 올바른 방법이 아닌 MyScreen.this.invalidate으로 사용하고 있지만, 다른 클래스의 페인트 메서드를 호출 할 수는 없다. AnimationThread은 불가능합니다. Screen 클래스의 propreitary 기능과 페인트 메소드는 스크린의 Object가 생성되면 호출됩니다. 그리고 내가 알고있는 것처럼 동일한 클래스 내에서 관리자 또는 스크린에 대해 무효화 메소드를 호출 할 수는 있지만 다른 클래스에있는 스크린의 객체를 생성하고 호출하여 호출 할 수는 없습니다.

단순히 객체를 사용하지 않고 단순히 호출해야하며 invalidate();은 화면을 다시 그리거나 무효화합니다.

동일한 화면의 개체를 만들어서도 다른 방법으로 화면을 표시하고 다른 인수를 사용하여 개체를 만들 수 있으며 paint 메서드에서 적절하게 처리 할 수 ​​있습니다. . 여기서 스레드가 실행될 때마다 개체를 만들어 원하는대로 처리 할 수 ​​있습니다. 샘플로

:이 문제 해피 코딩 해결

public class theScreen extends MainScreen{ 
int dummy; 
public theScreen(int firstArg){ 
    // Handle the firstArgument here 
    dummy=0; 
} 
public theScreen(int secondArg,int dummy){ 
    // Handle the second Argument here 
    this.dummy = dummy; 
} 
public void paint(Graphics graphcis) 
{ 
    if(dummy == 0) 
    { 
     //Handle what you would like to paint here 
    } 
    else{ 
     //Handle with a different paint here 
    } 
}} 

희망. 건배

+0

난 당신이 BB dev에, 자바에 대한 몇 가지 오해가있을 수 있습니다 생각합니다. 다른 클래스의 클래스를 선언 할 수 있습니다. 내부 클래스라고합니다. 또한,'MyScreen.this.invalidate()'는'invalidate()'와 같은 호출입니다. 왜냐하면'Thread'는 자체 invalidate() 메소드를 가지고 있지 않기 때문입니다. – jprofitt

0

Screen.doPaint()을 사용해 볼 수 있습니다.

위쪽/왼쪽 (30, 40)부터 완전히 그릴 수 있도록 비트 맵이 충분히 큽니까? 그림이 그려지는 것처럼 보이지 않을 수도 있습니다. 왜냐하면 그 그림이 그려지는 경계선 밖에 있기 때문입니다.

관련 문제