# Background Image auf SWT Oberfläche legen



## Art1977 (11. Mai 2005)

Ich habe folgende GUI erzeugt:


```
final Display display = new Display(); 
           //Shell must be created with style SWT.NO_TRIM 
           final Shell shell = new Shell(display, SWT.NO_TRIM | SWT.ON_TOP); 
           shell.setBackground(display.getSystemColor(SWT.COLOR_BLACK)); 
           //define a region 
           Region region = new Region(); 
           /* TestGUI Hintergrund
           region.add(circle(80, 0, 80));
           region.add(circle(80, 100, 80));
           region.add(new Rectangle(20, 20, 250, 200));
           region.subtract(circle(20, 67, 50)); 
           region.subtract(new int[]{67, 50, 55, 105, 79, 105}); 
           region.subtract(circle(60, 67, 150));
           */
           
           /*
           region.add(circle(150, 170, 150));
           region.add(new Rectangle(170, 0, 600, 450));
           region.add(circle(100, 170, 380));
           region.add(circle(120, 120, 430));
           */
           
           //Anfang ist oben links die Ecke
           region.add(new int[]{238, 0, 785, 0, 800, 15, 800, 455, 785 ,470
                   				, 785, 526, 765, 545, 290, 545, 270, 525
                   				, 270, 510, 185, 495, 146, 513,  90, 416
                   				, 118, 389, 241, 6});
           region.add(circle(222, 233, 225));
           region.add(circle(75, 77, 485));
           region.add(circle(75, 236, 460));
           region.add(circle(15, 785, 15));//rechts Oben, runde Ecke
           region.add(circle(15, 785, 455));
           region.add(circle(15, 770, 530));
           
           
          
           /*
           region.subtract(new int[]{80, 50, 55, 105, 79, 105}); 
           region.subtract(circle(60, 100, 150));
           */
           
           //define the shape of the shell using setRegion 
           shell.setRegion(region); 
           Rectangle size = region.getBounds(); 
           shell.setSize(size.width, size.height); 
           //shell.setSize(800,600);
           

/***************************************************************
                Hier ist der versuch, das bild reinzusetzen!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

           	Label label = new Label(shell, SWT.IMAGE_JPEG);	
   	      	ImageData data = new ImageData("title.jpg"); 
   	      	Image i = new Image(display, data);
   	      	label.setImage(i);

***************************************************************/

           //add ability to move shell around 
           Listener l = new Listener() { 
              Point origin; 
              public void handleEvent(Event e) { 
                 switch (e.type) { 
                    case SWT.MouseDown: 
                       origin = new Point(e.x, e.y); 
                       break; 
                    case SWT.MouseUp: 
                       origin = null; 
                       break; 
                    case SWT.MouseMove: 
                       if (origin != null) { 
                          Point p = display.map(shell, null, e.x, e.y); 
                          shell.setLocation(p.x - origin.x, p.y - origin.y); 
                       } 
                       break; 
                 } 
              } 
           }; 
           shell.addListener(SWT.MouseDown, l); 
           shell.addListener(SWT.MouseUp, l); 
           shell.addListener(SWT.MouseMove, l); 
           
           //add ability to close shell 
           Button b = new Button(shell, SWT.PUSH); 
           b.setBackground(shell.getBackground()); 
           b.setText("X"); 
           b.pack(); 
           b.setLocation(150, 250); 
           b.addListener(SWT.Selection, new Listener() 
           { 
              public void handleEvent(Event e) 
              { 
                 shell.close(); 
              } 
           }
           ); 
           
           shell.open();
           
  
       	while (!shell.isDisposed()) {
       		if (!display.readAndDispatch())
       			display.sleep();
       	}
       	region.dispose();
       	display.dispose();
    }
```

das ganze sieht so aus






nun hab ich in photoshop nen testbild als jpg gemacht, das ich auf diese schwarze fläche legen will gemacht.
im code hab ich die stelle markiert. Aber das funktioniert irgendwie nicht.[/img]


----------



## stev.glasow (11. Mai 2005)

Was hälst davon:


```
package test;

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.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.Region;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class Test {
	 static int[] circle(int r, int offsetX, int offsetY) 
     { 
        int[] polygon = new int[8 * r + 4]; 
        //x^2 + y^2 = r^2 
        for (int i = 0; i < 2 * r + 1; i++) { 
           int x = i - r; 
           int y = (int)Math.sqrt(r*r - x*x); 
           polygon[2*i] = offsetX + x; 
           polygon[2*i+1] = offsetY + y; 
           polygon[8*r - 2*i - 2] = offsetX + x; 
           polygon[8*r - 2*i - 1] = offsetY - y; 
        } 
        return polygon; 
     } 
     
	public static void main(String[] args) throws Exception {
		final Display display = new Display();
        //Shell must be created with style SWT.NO_TRIM
        final Shell shell = new Shell(display, SWT.NO_TRIM | SWT.ON_TOP);
        shell.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
        //define a region
        Region region = new Region();
        /* TestGUI Hintergrund
        region.add(circle(80, 0, 80));
        region.add(circle(80, 100, 80));
        region.add(new Rectangle(20, 20, 250, 200));
        region.subtract(circle(20, 67, 50));
        region.subtract(new int[]{67, 50, 55, 105, 79, 105});
        region.subtract(circle(60, 67, 150));
       
        
        /*
        region.add(circle(150, 170, 150));
        region.add(new Rectangle(170, 0, 600, 450));
        region.add(circle(100, 170, 380));
        region.add(circle(120, 120, 430));
        */
        
        //Anfang ist oben links die Ecke
        region.add(new int[]{238, 0, 785, 0, 800, 15, 800, 455, 785 ,470
                            , 785, 526, 765, 545, 290, 545, 270, 525
                            , 270, 510, 185, 495, 146, 513,  90, 416
                            , 118, 389, 241, 6});
        region.add(circle(222, 233, 225));
        region.add(circle(75, 77, 485));
        region.add(circle(75, 236, 460));
        region.add(circle(15, 785, 15));//rechts Oben, runde Ecke
        region.add(circle(15, 785, 455));
        region.add(circle(15, 770, 530));
        
        
      
        /*
        region.subtract(new int[]{80, 50, 55, 105, 79, 105});
        region.subtract(circle(60, 100, 150));
        */
        
        //define the shape of the shell using setRegion
        shell.setRegion(region);
        Rectangle size = region.getBounds();
        shell.setSize(size.width, size.height);
        //shell.setSize(800,600);
//        
//		 Label label = new Label(shell, SWT.IMAGE_JPEG);   
         ImageData data = new ImageData("title.jpg");
         final Image i = new Image(display, data);
//         label.setImage(i);
//		 label.pack();

		
		shell.addPaintListener(new PaintListener(){


			public void paintControl(PaintEvent e) {
				e.gc.drawImage(i, 0, 0);
				
			}
			
		});
		
        //add ability to move shell around
        Listener l = new Listener() {
           Point origin;
           public void handleEvent(Event e) {
              switch (e.type) {
                 case SWT.MouseDown:
                    origin = new Point(e.x, e.y);
                    break;
                 case SWT.MouseUp:
                    origin = null;
                    break;
                 case SWT.MouseMove:
                    if (origin != null) {
                       Point p = display.map(shell, null, e.x, e.y);
                       shell.setLocation(p.x - origin.x, p.y - origin.y);
                    }
                    break;
              }
           }
        };
        shell.addListener(SWT.MouseDown, l);
        shell.addListener(SWT.MouseUp, l);
        shell.addListener(SWT.MouseMove, l);
        
       
        Button b = new Button(shell, SWT.PUSH);
        b.setBackground(shell.getBackground());
        b.setText("X");
        b.pack();
        b.setLocation(150, 250);
        b.addListener(SWT.Selection, new Listener()
        {
           public void handleEvent(Event e)
           {
              shell.close();
           }
        }
        );
        
        shell.open();
        

       while (!shell.isDisposed()) {
          if (!display.readAndDispatch())
             display.sleep();
       }
       region.dispose();
       display.dispose();
 }
	

	

}
```
Hab das Bild nicht als Label eingefügt sondern in den Hintergrund gezeichnet.

[edit]


			
				Art|work im Chat hat gesagt.:
			
		

> Art|work	ok ich versuche das mal selbst hinzuhauen
> <--|	Art|work has left #java-forum.net


Du Schnödel  :bae: Ich hab 3 mal geschrieben das ich einen Lösungsansatz habe und den auch gepostet habe


----------



## Guest (12. Mai 2005)

Hey danke, ich probier das heute mal aus..... 

Hab das neben dem ganzen Firewall gelaber nicht gesehen ......

Danke nochmal...............


----------



## Art1977 (13. Mai 2005)

Hey vielen Dank, 

das funktioniert einwandfrei..... und ist ja total simpel


Aber noch ne Frage, gibts ne möglichkeit Buttons Individuell zu gestalten ? Oder muss ich da quasi tricksen mit nem Mouseoverlistener und nen Bereich definieren ? 

Gruß Art


----------

