使用相对路径合成Java组合背景图像

3

我刚接触Java,想在一个组合框中添加背景图片。我只能使用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个回答

2

或者您可以尝试使用图形方式,它使您能够从组件的起始点(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

请尝试以下代码:

请注意,相对路径的写法:

.\\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();
    }
}

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

这个答案在编程上非常有用...祝你有美好的一天...谢谢


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接