×

Discussion Board

Results 1 to 13 of 13
  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    15

    Unhappy problem in repaint a part of canvas!!

    i am making a GUI canvas like forum with back ground image and sub-images
    ( to act as buttons) and i don't know when i add action to its sub-images
    How to make the action is to repaint the canvas and change part of the screen only not all the screen like what i am facing here ( i just call repaint)
    i.e it draws all the screen again and make repeat some sub-image in different location which i don't want to show when i want to show it in the same area but with another color or border style .

    here my code:


    public class DrawImage extends Canvas implements Runnable{
    // Define your variables
    Image source;
    Font f;
    int fontheight;
    int mWidth= getWidth();
    int mHeight= getHeight();
    private static int i=0;
    private String mMessage=" ";
    public DrawImage(){
    try{
    source=getImage("images/ma3ak.PNG") ;
    }catch(IOException e){
    System.out.print("Couldnot Load the image");
    e.printStackTrace();
    }
    f= Font.getFont(Font.FACE_MONOSPACE,i, Font.SIZE_MEDIUM);//Font.getDefaultFont();
    fontheight=f.getHeight();
    }
    public Image getImage(String path) throws IOException {
    return Image.createImage(path);
    }

    public void run() {

    }
    public void paint (Graphics g){
    try {

    g.drawImage(source, 0, 0, Graphics.TOP | Graphics.LEFT);
    g.setColor(255,10, 10);
    g.setFont(f);
    g.drawString("Welcome To Ma3ak Mobile ", mWidth / 2, 0, Graphics.TOP | Graphics.HCENTER);
    drawSubscriptions(g);
    DrawGameItem(g);
    drawTarfeeh(g);
    //repaint();
    } catch (IOException ex) {
    ex.printStackTrace();
    }
    }
    private void drawTarfeeh(Graphics g) throws IOException {
    tarfeeh = resizeImage(Image.createImage("images/tarfeeh.PNG"), mWidth , mHeight/8) ;

    g.drawImage(subscriptions, 0, mHeight / 2, Graphics.TOP | Graphics.LEFT);
    }
    private void drawSubscriptions(Graphics g) throws IOException {
    subscriptions = resizeImage(Image.createImage("images/subscriptions.png"), mWidth, mHeight / Cool;

    g.drawImage(subscriptions, 0, mHeight / 6, Graphics.TOP | Graphics.LEFT);
    }
    public Image getImage(String path) throws IOException {
    return Image.createImage(path);
    }

    private Image resizeImage(Image src,int screenWidth,int screenHeight) {
    int srcWidth = src.getWidth();
    int srcHeight = src.getHeight();
    Image tmp = Image.createImage(screenWidth, srcHeight);
    Graphics g = tmp.getGraphics();
    int ratio = (srcWidth << 16) / screenWidth;
    int pos = ratio/2;

    //Horizontal Resize

    for (int x = 0; x < screenWidth; x++) {
    g.setClip(x, 0, 1, srcHeight);
    g.drawImage(src, x - (pos >> 16), 0, Graphics.LEFT | Graphics.TOP);
    pos += ratio;
    }

    Image resizedImage = Image.createImage(screenWidth, screenHeight);
    g = resizedImage.getGraphics();
    ratio = (srcHeight << 16) / screenHeight;
    pos = ratio/2;

    //Vertical resize

    for (int y = 0; y < screenHeight; y++) {
    g.setClip(0, y, screenWidth, 1);
    g.drawImage(tmp, 0, y - (pos >> 16), Graphics.LEFT | Graphics.TOP);
    pos += ratio;
    }
    return resizedImage;

    }//resize image

    //then i repaint it with to user actions selecting a sub_image (as Button here)
    Last edited by ahmedcse; 2009-12-31 at 16:40.

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: problem in repaint a part of canvas!!

    You should repaint the whole screen every time.

    Graham.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    15

    Re: problem in repaint a part of canvas!!

    Quote Originally Posted by grahamhughes View Post
    You should repaint the whole screen every time.

    Graham.
    i do already but it(repaint) draws canvas wrongly
    (it adds sub-image in front of canvas).

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: problem in repaint a part of canvas!!

    It just paints what your paint() code tells it to paint.

    I recommend you don't resize the image every time you paint. Keep the paint quick, resize the images in advance.

    You've wrapped all the code in try..catches, which might make you miss any exceptions that are being thrown at run time. You might have problems you're not aware of. I suggest you catch exceptions only if you're going to do something useful with them.

    Graham.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    15

    Smile Re: problem in repaint a part of canvas!!

    Quote Originally Posted by grahamhughes View Post
    It just paints what your paint() code tells it to paint.

    I recommend you don't resize the image every time you paint. Keep the paint quick, resize the images in advance.

    You've wrapped all the code in try..catches, which might make you miss any exceptions that are being thrown at run time. You might have problems you're not aware of. I suggest you catch exceptions only if you're going to do something useful with them.

    Graham.
    Thanks graham
    for your patient and advices
    but what do u mean by resizing the images in advance ??
    and how to repaint only a part of the screen ?

  6. #6
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: problem in repaint a part of canvas!!

    Every time you paint(), you're loading the same two images, and resize them to the same sizes. You would be better to load the images and resize them somewhere else, and keep references to the resized images in non-local variables.

    You don't repaint only part of the screen. You should always repaint everything.

    Graham.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    15

    Red face Re: problem in repaint a part of canvas!!

    Quote Originally Posted by grahamhughes View Post
    You don't repaint only part of the screen.
    You should always repaint everything.

    Graham.
    THEN IF I Repaint every thing i cannot change part of the screen
    so how can i change part of the screen or repaint sub-Area of the screen!!

  8. #8
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: problem in repaint a part of canvas!!

    Of course you can change part of the screen. You repaint some parts of the screen the same as the previous paint(), and some parts differently.

    Code:
    public class MyCanvas extends Canvas {
        // the object has "state"
        private boolean buttonPressed;
    
        // events change the state
    
        protected pointerPressed(int x, int y) {
            if (isInButtonArea(x, y)) {
                buttonPressed = true;
                repaint();
            }
        }
    
        protected pointerReleased(int x, int y) {
            buttonPressed = false;
            repaint();
        }
    
        // paint() draws a representation of the state
    
        protected void paint(Graphics g) {
            drawBackground(g);
            drawStaticElements(g);
            if (buttonPressed) {
                drawPressedButton(g);
            } else {
                drawReleasedButton(g);
            }
        }
    }
    When an event happens (in this case, the screen being touched or released), the state of the object changes, and a repaint() is issued.

    The paint() method redraws the entire screen. Some parts are always drawn (those parts then never change), and other parts are drawn according to the state of the object.

    I hope that helps.

    Graham.

  9. #9
    Registered User
    Join Date
    Oct 2009
    Posts
    15

    Re: problem in repaint a part of canvas!!

    thanks Graham
    but i dont use a touch screen mobile i use normal canvas and default net-beans emulator
    so i use the method below to make actions if u can help me to redraw every sub-image with user down or up buttons plz do thanks
    this the method to specify user action:

    protected void keyPressed(int keyCode) {
    int gameAction = getGameAction(keyCode);
    if (i < 0 || i > 2) {
    i = 0;
    }

    switch (gameAction) {

    case UP: {

    i--;
    mMessage = Integer.toString(i);
    // repaint(mWidth/6, mHeight / 6, mWidth, mHeight ); is this right??
    repaint();
    }
    break; // decrease i mMessage="UP";
    case DOWN: {
    i++; // increase i
    mMessage = Integer.toString(i);
    //repaint(mWidth/6, mHeight / 6, mWidth, mHeight );
    repaint();
    }
    break;

    case FIRE: {

    if (i == 0) {
    getDisplay().setCurrent(getSubscriptionForm());
    repaint();
    }
    if (i == 1) {
    getDisplay().setCurrent(getGamesForm());
    repaint();
    }
    if (i == 2) {
    getDisplay().setCurrent(getSuccessAlert("its counting " + mMessage));
    repaint();
    }
    }
    break; //to add action

    default:
    mMessage = "";
    break;
    }

    }

    then i want according to that to redraw sub-Image to show user that it is selected
    Last edited by ahmedcse; 2009-12-31 at 17:15.

  10. #10
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: problem in repaint a part of canvas!!

    Quote Originally Posted by ahmedcse View Post
    but i dont use a touch screen mobile i use normal canvas and default net-beans emulator
    It doesn't matter, the principle is identical. keyPressed() is your event, and i (or mMessage?) is your state. The code in the event-handler changes the state, and the paint() method uses the state to decide what to draw on the Canvas.

    So, as the value of "i" changes, you re-draw the entire screen, reading the value of "i" and drawing the screen accordingly.

    Remember that the paint() method is called at many different times, not just when you call repaint(). It's called when the Canvas is setCurrent(). It is called when the Canvas is revealed after being completely or partially obscured by something else (such as a system alert, the volume control display, an SMS notification, an incoming call, etc.). For this reason, the paint() method needs to be able to paint the entire display at any time.

    Quote Originally Posted by ahmedcse View Post
    getDisplay().setCurrent(getSubscriptionForm());
    repaint();
    No need to repaint() when you're changing to a different Displayable. The Canvas isn't current anymore, so it can't repaint.

    Graham.

  11. #11
    Registered User
    Join Date
    Oct 2009
    Posts
    15

    Re: problem in repaint a part of canvas!!

    Quote Originally Posted by grahamhughes View Post


    No need to repaint() when you're changing to a different Displayable. The Canvas isn't current anymore, so it can't repaint.

    Graham.
    hi Graham here i clear it
    keyPressed() is my event and (i) is my state
    how the paint() method uses the state to decide what to draw on the Canvas.
    the value of "i" changes, i want to re-draw the entire screen, reading the value of "i" and drawing the screen accordingly.!!
    how to do this?? is it by reversing the order of code
    repaint() ;
    then setCurrent(next Displayable) reversing does not work !!
    or i repaint every (i) changes up or down what is the required parameters for repaint part of screen only while i have no Graphics(g) object in my keyPressed() method to use in editing my sub-images! how to override it ??
    Last edited by ahmedcse; 2010-01-02 at 12:35.

  12. #12
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: problem in repaint a part of canvas!!

    First: forget the repaint() when you setCurrent(). repaint() tell the Canvas to repaint, but the Canvas won't be displayed anymore, because setCurrent() is replacing the Canvas with a different object (another Canvas, a Form, an Alert, or whatever).

    Second: forget the whole idea of repainting part of the screen. paint() must be written to paint the entire screen every time. When "i" changes, you repaint the entire screen.

    How do you want the screen to look for each value of "i"?

    Code:
    // state
    private int i;
    
    // handle event, update state, trigger repaint
    protected void keyPressed(int keyCode) {
        switch (getGameAction(keyCode)) {
        case UP:
            i--;
            if (i < 0) {
                i = 2;
            }
            repaint();
            break;
        case DOWN:
            i++;
            if (i > 2) {
                i = 0;
            }
            repaint();
            break;
         case FIRE:
            // some code here...
            break;
        }
    }
    
    // paint the screen according to the state
    protected void paint(Graphics g) {
        switch (i) {
        case 0:
            // paint screen for i = 0
            break;
        case 1:
            // paint screen for i = 1
            break;
        case 2:
            // paint screen for i = 2
            break;
        default:
            throw new IllegalStateException("paint: i = " + i);
        }
    }
    Graham.

  13. #13
    Registered User
    Join Date
    Oct 2009
    Posts
    15

    Smile Re: problem in repaint a part of canvas!!

    IT is working !!!!
    Thanks graham
    Thank you very Much

Similar Threads

  1. Problem developing canvas based application for N95
    By shallu_taneja in forum Mobile Java Tools & SDKs
    Replies: 2
    Last Post: 2007-09-07, 06:20
  2. problem on drawing a part from a bitmap to the screen
    By ceehira in forum Symbian Media (Closed)
    Replies: 0
    Last Post: 2005-08-26, 07:35
  3. 6600 Canvas CommandListener problem
    By ssandor in forum Mobile Java General
    Replies: 3
    Last Post: 2005-08-09, 12:15
  4. problem with a speed of repaint image
    By marcart in forum Mobile Java Media (Graphics & Sounds)
    Replies: 2
    Last Post: 2003-08-28, 12:10
  5. Read Chinese Text file and Display Chinese on Canvas Problem?
    By goeast in forum Mobile Java General
    Replies: 4
    Last Post: 2002-09-18, 03:28

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •