The tiling and stretching of SWT background graphs

Source: Internet
Author: User
Tags set background
Import Java.io.InputStream;


Import Org.eclipse.swt.SWT;
Import org.eclipse.swt.events.PaintEvent;
Import Org.eclipse.swt.events.PaintListener;
Import Org.eclipse.swt.graphics.Image;
Import Org.eclipse.swt.graphics.ImageData;
Import Org.eclipse.swt.graphics.Rectangle;
Import Org.eclipse.swt.layout.FillLayout;
Import Org.eclipse.swt.widgets.Composite;
Import Org.eclipse.swt.widgets.Display;
Import Org.eclipse.swt.widgets.Shell;


/**
* can set the background picture of the composite
* Composite itself can set the background picture, but only provides a tile display, the class to achieve the center, tile and stretch three display methods.
*
* @author 003
*/
public class Imagecomposite extends composite
{
/**
* Center
*/
public static final String CENTRE = "CENTRE";

/**
* Tile
*/
public static final String tiled = "tiled";


/**
* Stretch
*/
public static final String SCALED = "SCALED";


/**
* Background picture
*/
Private Image backgroundimage;

/**
* Background picture display mode
*/
Private String Imagedisplaymode;


/**
* Background picture display mode index (this attribute is introduced to help expand when necessary)
*/
private int modeindex;


/**
* Construct a imagecomposite with no background picture
* @param parent Component
* @param style
*/
Public Imagecomposite (composite parent, int style)
{
This (parent, style, NULL, CENTRE);
}

/**
* Constructs a imagecomposite with the specified background picture and the specified display mode
* @param parent Component
* @param style
* @param image Background image
* @param modename background picture display mode
*/
Public Imagecomposite (composite parent, int style, image image, String modename)
{
Super (parent, style);
Addpaintlistener (New Paintlistener ()
{
@Override
public void Paintcontrol (PaintEvent e)
{
DrawImage (e);
}
});
SetBackgroundImage (image);
Setimagedisplaymode (modename);
}


/**
* Get background picture
* @return Background picture
* @see Org.eclipse.swt.widgets.control#getbackgroundimage (Image)
*/
@Override
Public Image Getbackgroundimage ()
{
return backgroundimage;
}


/**
* Set Background picture
* @param background picture
* @see Org.eclipse.swt.widgets.control#setbackgroundimage (Image)
*/
@Override
public void SetBackgroundImage (Image backgroundimage)
{
This.backgroundimage = backgroundimage;
This.redraw ();
}


/**
* Get background picture display mode
* @return Display Mode
*/
Public String Getimagedisplaymode ()
{
return imagedisplaymode;
}


/**
* Set background picture display mode
* @param modename Mode name, value is limited to imagepane.tiled imagepane.scaled Imagepane.centre
*/
public void Setimagedisplaymode (String modename)
{
if (modename!= null)
{
Modename = Modename.trim ();

Center
if (Modename.equalsignorecase (CENTRE))
{
This.imagedisplaymode = CENTRE;
Modeindex = 0;
}
Tile
else if (modename.equalsignorecase (tiled))
{
This.imagedisplaymode = tiled;
Modeindex = 1;
}
Stretch
else if (Modename.equalsignorecase (SCALED))
{
This.imagedisplaymode = SCALED;
Modeindex = 2;
}

This.redraw ();
}
}

/**
* Draw Background
* @param e PaintEvent
*/
private void DrawImage (PaintEvent e)
{
If a background picture is set, the display
if (backgroundimage!= null)
{
int width = this.getsize (). x;
int height = this.getsize (). Y;
int imagewidth = Backgroundimage.getimagedata (). width;
int imageheight = Backgroundimage.getimagedata (). Height;

Switch (MODEINDEX)
{
Center
Case 0:
{
int x = (width-imagewidth)/2;
int y = (height-imageheight)/2;
E.gc.drawimage (backgroundimage, x, y);
Break
}
Tile
Case 1:
{
for (int ix = 0; ix < width; IX = imagewidth)
{
for (int iy = 0; iy < height; iy + = ImageHeight)
{
E.gc.drawimage (BackgroundImage, IX, iy);
}
}

Break
}
Stretch
Case 2:
{
ImageData data = Backgroundimage.getimagedata (). Scaledto (width, height);
E.gc.drawimage (New Image (E.display, data), 0, 0);
Break
}
}
}
}

/**
* The Settings window is in the middle of the screen
* @param display Equipment
* @param the Shell to adjust the position of the Window object
*/
public static void Center (display display, shell shell)
{
Rectangle bounds = Display.getprimarymonitor (). getbounds ();
Rectangle rect = Shell.getbounds ();
int x = bounds.x + (bounds.width-rect.width)/2;
int y = bounds.y + (bounds.height-rect.height)/2;
Shell.setlocation (x, y);
}

public static void Main (string[] args)
{
Display display = Display.getdefault ();
InputStream is = ImageComposite.class.getResourceAsStream ("test.jpg");
Image image = New Image (Display, new ImageData (IS));
Shell shell = new Shell ();
New Imagecomposite (Shell, SWT. NONE, image, imagecomposite.scaled);
Shell.settext ("Imagecomposite Test");
Shell.setlayout (New Filllayout ());
Shell.setsize (800, 600);
Center (display, Shell);
Shell.open ();
Shell.layout ();

while (!shell.isdisposed ())
{
if (!display.readanddispatch ())
{
Display.sleep ();
}
}


Display.dispose ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.