3

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に不慣れで、構造化/モジュールプログラムのみをプログラムしました。

4

3 に答える 3

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();
    }
}
于 2012-11-29T05:58:49.177 に答える
2

または、コンポーネントの開始点(x、y)から画像を描画できるグラフィックスの方法を試すことができます。もちろん、特定の画像でカバーしたいスペースに合うように画像を拡大縮小することもできます。 Imageオブジェクトのscaleメソッド。例:

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);
        }

    };


}
}
于 2012-12-01T17:46:57.530 に答える
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

プログラム的に役立つこの答え....良い一日を....ありがとう

于 2017-01-19T11:04:35.887 に答える