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 6201747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:36:38+00:00 2026-05-24T04:36:38+00:00

I am trying to make an Image Viewer using SWT where I want to

  • 0

I am trying to make an Image Viewer using SWT where I want to display images on a canvas.On the click of “next” button I want the image on the canvas to change to the next image in the selected folder.Further, I want to draw rectangles on this image by using the rectangle bounds thats present in an xml file.How can I achieve this?Please help

  • 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-24T04:36:39+00:00Added an answer on May 24, 2026 at 4:36 am

    Here is a simple code for you (should be enhanced for image shrinking with correct aspect ratio, etc.. ;])

    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.Random;
    
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.PaintEvent;
    import org.eclipse.swt.events.PaintListener;
    import org.eclipse.swt.events.SelectionAdapter;
    import org.eclipse.swt.events.SelectionEvent;
    import org.eclipse.swt.graphics.Color;
    import org.eclipse.swt.graphics.Image;
    import org.eclipse.swt.graphics.RGB;
    import org.eclipse.swt.graphics.Rectangle;
    import org.eclipse.swt.layout.FillLayout;
    import org.eclipse.swt.widgets.Button;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Shell;
    
    public class ImageLoader {
    
        private Display display = null;
        private Shell shell = null;
        private Composite imageCanvas = null;
        private Image img = null;
        private Random random = new Random();
        private Color rectColor = null;
    
        private String[] imageURLs = new String[] {
                "http://upload.wikimedia.org/wikipedia/en/7/70/Example.png",
                "http://upload.wikimedia.org/wikipedia/commons/thumb/1/19/APEC_Police_Helicopter%2C_Opera_House%2C_2_Sept_2007.JPG/220px-APEC_Police_Helicopter%2C_Opera_House%2C_2_Sept_2007.JPG",
                "http://upload.wikimedia.org/wikipedia/commons/thumb/a/a2/Abroad_-_1882.djvu/page13-474px-Abroad_-_1882.djvu.jpg"
            };
        private int imgNum = 0;
        private Rectangle[] rectangles = null;
    
        public ImageLoader() {
            display = new Display();
            shell = new Shell(display);
            shell.setLayout(new FillLayout());
            shell.setSize(700, 500);
    
            rectColor = new Color(display, new RGB(255, 0, 0));
    
            // load first image which should be displayed
            loadImage();
    
            imageCanvas = new Composite(shell, SWT.BORDER);
            // on each paint event (first view, window resize, redraw method, ...) make my code
            imageCanvas.addPaintListener(new PaintListener() {
    
                @Override
                public void paintControl(PaintEvent e) {
                    // draw the image from (0,0) in it's own size
                    e.gc.drawImage(img, 0, 0);
    
                    // set foreground color (paint color)
                    e.gc.setForeground(rectColor);
                    // draw all loaded rectangles
                    for(int i = 0; i < rectangles.length; i++) e.gc.drawRectangle(rectangles[i]);
                }
            });
    
            Button btnNext = new Button(shell, SWT.PUSH);
            btnNext.setText("Next image");
            btnNext.addSelectionListener(new SelectionAdapter() {
    
                @Override
                public void widgetSelected(SelectionEvent e) {
                    // load next image
                    loadImage();
                    // repaint image canvas
                    imageCanvas.redraw();
                }
    
            });
    
    //      shell.pack();
            shell.open();
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch())
                    display.sleep();
            }
    
            rectColor.dispose();
        }
    
        /**
         * Loads next image from folder
         */
        private void loadImage() {
            try {
                img = new Image(display, getImageURL().openStream());
            } catch (IOException ex) {
                ex.printStackTrace();
            }
    
            // read rectangles which should be shown
            rectangles = new Rectangle[random.nextInt(5) + 3];
            for(int i = 0; i < rectangles.length; i++) rectangles[i] = getRectangleFromFile();
        }
    
        /**
         * Fakes loading next image from folder by cycling on image array
         * @return
         */
        private URL getImageURL() {
            URL tmpURL;
            try {
                tmpURL = new URL(imageURLs[imgNum]);
            } catch (MalformedURLException e) {
                e.printStackTrace();
                return null;
            }
            // in the end? let's turn back on start
            if(++imgNum >= imageURLs.length) imgNum = 0;
            return tmpURL;
        }
    
        /**
         * Fakes reading rectangles from file by generating random size and position rectangle
         * @return
         */
        private Rectangle getRectangleFromFile() {
            return new Rectangle(random.nextInt(img.getBounds().width), random.nextInt(img.getBounds().height), random.nextInt(50), random.nextInt(50));
        }
    
        public static void main(String[] args) {
            new ImageLoader();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make this button change background when you click it and this
I am trying to make a Xml Image Menu that on button click goes
i am trying to make image and text display at same line by using
I am trying to make the image title display at the same time the
I am trying to make a combined image of all images added to a
I'm trying to make a random image appear on the press of a button.
I'm trying to make an image of a graph using ruby-graphviz by @graph.output(:output =>
I'm trying to build an image viewer for 16-bit PNG images with WPF. My
I'm trying to make an image editor with canvas, but I ran into some
I'm trying to make an image mosaic that consists mostly of images of the

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.