Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6643159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:01:02+00:00 2026-05-26T00:01:02+00:00

I have two images that i want to draw into a canvas object. I

  • 0

I have two images that i want to draw into a canvas object. I got those images from a server and when the loadHandler invokes, i get the dimensions of the image (they have the same width and height) and calculate the dimensions of the canvas. Then i draw each image, at the calculated x,y position in canvas. The problem is that only one image appears in canvas. Why?

Here is a part of the code:

final Image siImg = new Image();
            siImg.setVisible(false);
            siImg.setUrl(Constants.URL_PREFIX + siPath);
            siImg.addLoadHandler(new LoadHandler() {

                @Override
                public void onLoad(LoadEvent event) {

                    int siWidth = siImg.getWidth();
                    int siHeight = siImg.getHeight();

                    siImg.removeFromParent();

                    if (!CategoryTableView.this.dimFromBg) {
                        CategoryTableView.this.width = siWidth;
                        CategoryTableView.this.height = siHeight * sSize;
                        //CategoryTableView.this.setPixelSize(CategoryTableView.this.width, CategoryTableView.this.height);
                        CategoryTableView.this.canvas.setPixelSize(CategoryTableView.this.width, CategoryTableView.this.height);
                        CategoryTableView.this.canvas.setCoordinateSpaceHeight(CategoryTableView.this.height);
                        CategoryTableView.this.canvas.setCoordinateSpaceWidth(CategoryTableView.this.width);
                        CategoryTableView.this.dimFromBg = true;
                    }

                    ImageElement imageElement = (ImageElement) siImg.getElement().cast();

                    int left = xOff;
                    int top = yOff + (siHeight * fi);


                    CategoryTableView.this.context.drawImage(imageElement, left, top);

                }

            });
            RootPanel.get().add(siImg);

Ok i think i find it…i have to save the context’s state each time. Is that right? (Because it works now!)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:01:03+00:00Added an answer on May 26, 2026 at 12:01 am

    You add the image to your root panel in the last line

    final Image siImg = new Image();
    ...
    RootPanel.get().add(siImg);
    

    instead of adding your canvas. So you will only see the image instead of the canvas. You have to add the Canvas to your root panel and draw both images to your canvas. For performance-reasons it is better to draw to a backbuffer instead of drawing directly to the canvas. Here is a little example:

    Canvas canvas = Canvas.createIfSupported();
    Canvas backBuffer = Canvas.createIfSupported();
    
    Context2d context = canvas.getContext2d();
    Context2d backBufferContext = backBuffer.getContext2d();
    
    Image image1 = new Image("http://your.url.to/image.jpg");
    image1.addLoadHandler(new LoadHandler() {       
        public void onLoad(LoadEvent event) {
            // do anything you want here
            doDraw();
        }
    });
    
    Image image2 = new Image("http://your.url.to/image2.jpg");
    image2.addLoadHandler(new LoadHandler() {       
        public void onLoad(LoadEvent event) {
            // do anything you want here
            doDraw();
        }
    });
    
    RootPanel.get().add(canvas);
    

    And the draw-method would look like this:

    public void doDraw() {
        backBufferContext.setFillStyle(redrawColor);
        backBufferContext.fillRect(0, 0, width, height);
        ImageElement imageElement = ImageElement.as(image1.getElement());
        backBufferContext.drawImage(imageElement, 0, 0, 1024, 768, 0, 0, 102, 76);
    
        ImageElement imageElement = ImageElement.as(image2.getElement());
        backBufferContext.drawImage(imageElement, 0, 0, 1024, 768, 102, 76, 102, 76);
    
        context.drawImage(backBufferContext.getCanvas(), 0, 0);
    }
    

    Please note: You have to use global variables in this example. Change this to your needs by either passing the arguments/classes or defining the variables class-wide. Also the drawing-areas are hardcoded in this example: this you have also to change to your needs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got two images one that i want to display on the top
I have two images that I want to merge into one. (Eg House.png on
I have two <canvas> elements in a document. I want to draw a shape
I have two overlay images that I want show in camera view depending on
I have two images among many that I want to swap places between. If
Sheet1 Sheet2 Above i have two images link that i have captured from my
I have two images that I want adjacent but they appear one above the
Simple idea: I have two images that I want to merge, one is 500x500
I have two images that I want to be positioned directly on top of
Here's the situation: I have a two images that are over 1024 in width

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.