Java复合backgroundimage与相对路径

时间:2012-11-28 20:19:05

标签: java image background swt relative

我是Java的新手,我想在Composite中添加背景图像。我只能使用SWT,没有JFace。我正在使用eclipse indigo IDE(3.8),当我想设置背景图像时,首先我将Image类初始化为图像对象,但是当我按CTRL + SPACE来帮助选择构造函数时,我有5个不同的构造函数。我不知道该选择什么。

我必须使用相对路径。该包具有以下结构:

org.mypackage.program //the package name
org.mypackage.program/src/org.mypackage.program //the plugin-project automated created classes
org.mypackage.program/src/views // all views

org.mypackage.program/car_image.jpg // the image what I would set in background
org.mypackage.program/views/View.java // the class where I want to set the background

这就是我所做的,但它不起作用:

Image image = new Image(Display.getCurrent(),  this.getClass().getClassLoader().getResource("car_image.jpg"));
compImage.setBackgroundImage(image);

我也是OOP的新手,我只编写了结构化/模块化程序。

3 个答案:

答案 0 :(得分:2)

请尝试以下代码: -

请注意,如何提及相对路径

.\\src\\org\\mypackage\\program\\car_image.gif

这里src是应用程序的根文件夹。


package org.mypackage.program;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Demo {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
            Display display = new Display();
            Shell shell = new Shell(display);
            Image img;
            shell.setMaximized(true);
            img= new Image(display,".\\src\\org\\mypackage\\program\\car_image.gif");
            Composite comp= new Composite(shell, SWT.NONE);
            comp.setBackgroundImage(img);
            shell.setLayout(new FillLayout());
            shell.open();
            while (!shell.isDisposed())
            {       
                if (!display.readAndDispatch())
                {         
                    display.sleep();       
                }     
            }     
            display.dispose();
    }
}

答案 1 :(得分:2)

或者您可以尝试使用Graphics方式从组件的起点(x,y)绘制图像。您可以对图像进行缩放,使其适合您想要覆盖图像的空间使用Image对象的特定缩放方法。例如:

package org.mypackage.program;

//your imports here

public class MyClassName extends JFrame{

public Image myImg;
public int startingX = 0,startingY = 0,newWidth = 300,newHeight = 300;

public static void main(String[] args){
    MyClassName frame = new MyClassName();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400,400);
    frame.setVisible(true);
}

public MyClassName(){
    super("JFrame title")

    myImg = Toolkit().getDefaultToolkit().getImage(MyClassName.class.getResource("org/mypackage/program/car_image.jpg"));
    Display display = new Display();
    Shell shell = new Shell(display);

    shell.setMaximized(true);

    myImg = myImg.getScaledInstance(newWidth,newHeight,0);

    Composite comp= new Composite(shell, SWT.NONE){

        protected void paintComponent(Graphics g){
            g.drawImage(myImg, startingX, startingY, this);
        }

    };


}
}

答案 2 :(得分:0)

  

使用swt / jface应用程序设置动态或相对的图像路径

Button btnNewButton = new Button(parent, SWT.NONE);
//Absolute path never use
//btnNewButton.setImage(SWTResourceManager.getImage("D:\\Bharat\\Icon\\uncheck.png"));
btnNewButton.setImage(ResourceManager.getPluginImage("RCP_Demo", "icons/delete.png"));
btnNewButton.setBounds(18, 370, 68, 23);
/////Launching time your application can not generate some image related problem 

1)Control: Drag and drop Button control
2)Add image to Button control 
3)select image chooser property and click plugin resource radiobutton
4)select your project,select your image folder after set image to button

以编程方式有用这个答案....祝你有个美好的一天....谢谢