如何在swt canvas中控制图片的大小 50
如何在canvas中控制图片的大小,我的图片比我的canvas大好多啊,差不多满屏这么大,我想控制在canvas中显示的时候控制他的大小,跟我canvas那么大就行,不是...
如何在canvas中控制图片的大小,我的图片比我的canvas大好多啊,差不多满屏这么大,我想控制在canvas中显示的时候控制他的大小,跟我canvas那么大就行,不是用滚动条滚
也就是让图片按比例显示在Canvas控制的那么大小中,请教高手如何实现呢
public class CanvasExample {
private Image image;
public CanvasExample() {
final Display display = Display.getDefault();
final Shell shell=new Shell(SWT.DIALOG_TRIM|SWT.ON_TOP);
shell.setText("我的窗口");
shell.setSize(300,360);
shell.setLayout(new GridLayout(4,false));
final Canvas cans=new Canvas(shell,SWT.BORDER);
GridData gridA= new GridData();
gridA.horizontalIndent=15;
gridA.horizontalSpan=4;
gridA.widthHint=250;
gridA.heightHint=120;
gridA.verticalIndent=15;
cans.setLayoutData(gridA);
//监听canvas的重绘事件
cans.addPaintListener(new PaintListener(){
public void paintControl(PaintEvent e) {
if(image!=null)
e.gc.drawImage(image,0,0);
image=null;
}
});
final List list=new List(shell,SWT.BORDER|SWT.V_SCROLL);
list.setItems(new String [] {
"001","002","003","sence"
});
GridData gridB=new GridData();
gridB.horizontalSpan=4;
gridB.widthHint=100;
gridB.heightHint=100;
gridB.horizontalIndent=15;
gridB.verticalIndent=10;
list.setLayoutData(gridB);
Button button1=new Button(shell,SWT.NONE);
button1.setText("浏览");
GridData gridC=new GridData();
gridC.horizontalSpan=3;
gridC.horizontalIndent=90;
gridC.verticalIndent=10;
gridC.widthHint=80;
button1.setLayoutData(gridC);
button1.addSelectionListener(new SelectionAdapter(){
@Override
public void widgetSelected(SelectionEvent e) {
if(list.getSelectionIndex()==0){
image=new Image(display,"icons/003.jpg");
cans.redraw();
}else if(list.getSelectionIndex()==1){
image=new Image(display,"icons/004.jpg");
cans.redraw();
}else if(list.getSelectionIndex()==2){
image=new Image(display,"icons/005.jpg");
cans.redraw();
}else if(list.getSelectionIndex()==3){
image=new Image(display,"icons/scene.jpg");
cans.redraw();
}
}
});
Button b2=new Button(shell,SWT.NONE);
b2.setText("退出");
GridData gridD=new GridData();
gridD.widthHint=80;
gridD.verticalIndent=10;
gridD.horizontalIndent=15;
b2.setLayoutData(gridD);
b2.addSelectionListener(new SelectionAdapter(){
@Override
public void widgetSelected(SelectionEvent e) {
shell.dispose();
}
});
shell.open();
shell.layout();
while(!display.isDisposed()){
if(!display.readAndDispatch())
display.sleep();
}
}
public static void main(String [] args){
CanvasExample cas=new CanvasExample();
}
}
上边那4个图片都没能完整显示在canvas中,图片是320*240的,如何让图片按比例完整显示在Canvas
那为什么同样的大小的两张图片,显示结果却不一样,一张可以全显,一张只能看到一部分
图片格式为jpg 展开
也就是让图片按比例显示在Canvas控制的那么大小中,请教高手如何实现呢
public class CanvasExample {
private Image image;
public CanvasExample() {
final Display display = Display.getDefault();
final Shell shell=new Shell(SWT.DIALOG_TRIM|SWT.ON_TOP);
shell.setText("我的窗口");
shell.setSize(300,360);
shell.setLayout(new GridLayout(4,false));
final Canvas cans=new Canvas(shell,SWT.BORDER);
GridData gridA= new GridData();
gridA.horizontalIndent=15;
gridA.horizontalSpan=4;
gridA.widthHint=250;
gridA.heightHint=120;
gridA.verticalIndent=15;
cans.setLayoutData(gridA);
//监听canvas的重绘事件
cans.addPaintListener(new PaintListener(){
public void paintControl(PaintEvent e) {
if(image!=null)
e.gc.drawImage(image,0,0);
image=null;
}
});
final List list=new List(shell,SWT.BORDER|SWT.V_SCROLL);
list.setItems(new String [] {
"001","002","003","sence"
});
GridData gridB=new GridData();
gridB.horizontalSpan=4;
gridB.widthHint=100;
gridB.heightHint=100;
gridB.horizontalIndent=15;
gridB.verticalIndent=10;
list.setLayoutData(gridB);
Button button1=new Button(shell,SWT.NONE);
button1.setText("浏览");
GridData gridC=new GridData();
gridC.horizontalSpan=3;
gridC.horizontalIndent=90;
gridC.verticalIndent=10;
gridC.widthHint=80;
button1.setLayoutData(gridC);
button1.addSelectionListener(new SelectionAdapter(){
@Override
public void widgetSelected(SelectionEvent e) {
if(list.getSelectionIndex()==0){
image=new Image(display,"icons/003.jpg");
cans.redraw();
}else if(list.getSelectionIndex()==1){
image=new Image(display,"icons/004.jpg");
cans.redraw();
}else if(list.getSelectionIndex()==2){
image=new Image(display,"icons/005.jpg");
cans.redraw();
}else if(list.getSelectionIndex()==3){
image=new Image(display,"icons/scene.jpg");
cans.redraw();
}
}
});
Button b2=new Button(shell,SWT.NONE);
b2.setText("退出");
GridData gridD=new GridData();
gridD.widthHint=80;
gridD.verticalIndent=10;
gridD.horizontalIndent=15;
b2.setLayoutData(gridD);
b2.addSelectionListener(new SelectionAdapter(){
@Override
public void widgetSelected(SelectionEvent e) {
shell.dispose();
}
});
shell.open();
shell.layout();
while(!display.isDisposed()){
if(!display.readAndDispatch())
display.sleep();
}
}
public static void main(String [] args){
CanvasExample cas=new CanvasExample();
}
}
上边那4个图片都没能完整显示在canvas中,图片是320*240的,如何让图片按比例完整显示在Canvas
那为什么同样的大小的两张图片,显示结果却不一样,一张可以全显,一张只能看到一部分
图片格式为jpg 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询