Java SWT - Canvas:为什么删除图像? [英] Java SWT - Canvas: why delete images ?

查看:82
本文介绍了Java SWT - Canvas:为什么删除图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我有问题。

我使用SWT制作GUI。

在主窗口中我绘制了一些图像(画布,画布上的gc),然后按一个按钮就会出现一个新窗口。当我在主窗口上移动新窗口时,图像将消失。为什么?


对不起我的英文:)

我希望你能理解。


谢谢

Carmine


Ps:如果你想我可以发送图片



附加图像
(17.9 KB,493 views)


Hello,
I have a problem.
I use SWT to make GUI.
In the main window I have drawed some images (canvas, gc on canvas) and pressing a button will appear a new window. When I I move the new window over the main window the images will disappear. Why ?

Sorry for my english :)
I hope that you will understand.

Thank you
Carmine

Ps: if you want i can send an image

Attached Images
problema.jpg (18.8 KB, 587 views)

解决方案

Hello,
I have a problem.
I use SWT to make GUI.
In the main window I have drawed some images (canvas, gc on canvas) and pressing a button will appear a new window. When I I move the new window over the main window the images will disappear. Why ?

Sorry for my english :)
I hope that you will understand.

Thank you
Carmine

Ps: if you want i can send an image

Let''s see the code.


public class grafica extends javax.swing.JFrame {
Shell shell;
Text text;
//GC gc;
public grafica() throws IOException, Exception {
Menu menuBar, fileMenu, helpMenu;
MenuItem fileMenuHeader, aboutMenuHeader;
MenuItem fileExitItem, aboutAboutItem;

display = new Display();
shell = new Shell(display);
shell.setText("Composer");

menuBar = new Menu(shell, SWT.BAR);
fileMenuHeader = new MenuItem(menuBar, SWT.CASCADE);
fileMenuHeader.setText("&File");
fileMenu = new Menu(shell, SWT.DROP_DOWN);
fileMenuHeader.setMenu(fileMenu);

MenuItem expertItem = new MenuItem(fileMenu, SWT.CASCADE);
expertItem.setText("&Expert");
Menu submenu = new Menu(shell, SWT.DROP_DOWN);
expertItem.setMenu(submenu);
MenuItem expertParseInItem = new MenuItem(submenu, SWT.PUSH);
expertParseInItem.setText("Parse Input\tCTRL+P");
expertParseInItem.setAccelerator(SWT.CTRL + ''P'');
MenuItem dialogItem = new MenuItem(submenu, SWT.PUSH);
dialogItem.setText("&Parse Output\tCTRL+O");
dialogItem.setAccelerator(SWT.CTRL + ''O'');
MenuItem separator = new MenuItem(fileMenu, SWT.SEPARATOR);

fileExitItem = new MenuItem(fileMenu, SWT.PUSH);
fileExitItem.setText("E&xit\tCTRL+C");
fileExitItem.setAccelerator(SWT.CTRL + ''C'');

aboutMenuHeader = new MenuItem(menuBar, SWT.CASCADE);
aboutMenuHeader.setText("Info");
helpMenu = new Menu(shell,SWT.DROP_DOWN);
aboutMenuHeader.setMenu(helpMenu);
aboutAboutItem = new MenuItem(helpMenu, SWT.PUSH);
aboutAboutItem.setText("Info");

fileExitItem.addSelectionListener(new MenuItemListener());
aboutAboutItem.addSelectionListener(new MenuItemListener());
expertParseInItem.addSelectionListener(new MenuItemListener());

Rectangle screen = display.getMonitors()[0].getBounds();

shell.setMenuBar(menuBar);
// Creazione ToolBar
Image image = new Image(display,"img/Parse.jpg");
Image image2 = new Image(display,"img/Help.jpg");
//GC gc = new GC(image);
//GC gc2 = new GC(image2);
//gc.dispose();
//gc2.dispose();
ToolBar toolBar = new ToolBar(shell, SWT.NO);

ToolItem itemParse = new ToolItem(toolBar, SWT.PUSH);
itemParse.setImage(image);
itemParse.setText("Parse In");
ToolItem itemHelp = new ToolItem(toolBar, SWT.PUSH);
itemHelp.setImage(image2);
itemHelp.setText("Help");
//itemHelp.addSelectionListener(new ToolBarButtonListener());


itemParse.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e2) {
System.out.println("E'' stato premuto il tasto ParseIn");
RWmidi midi = null;
try {
midi = new RWmidi("midi.mid");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Parser parser = new Parser(midi);
String eventi = parser.getParsingTraccia(1);
Shell dialog = new Shell(shell);
dialog.setText("Dialog");
Text text4 = new Text(dialog, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
text4.setBounds(0,0,800,500);
text4.setEditable(false);
text4.setText(eventi);
dialog.setSize(800, 500);
dialog.open();
}
});



itemHelp.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
System.out.println("E'' stato premuto il tasto Help");
MessageBox aboutBox = new MessageBox(shell,SWT.ICON_INFORMATION);
aboutBox.setText("Informazioni");
StringBuffer messaggio = new StringBuffer();
messaggio.append("Composer Midi\n");
messaggio.append("Programma sviluppato da Fedullo Carmine\n");
messaggio.append("\n");
messaggio.append("Per ulteriori informazioni\n");
messaggio.append("carmine82@gmail.com\n");
aboutBox.setMessage(messaggio.toString());
int buttonOK = aboutBox.open();
//shell.pack();
shell.open();
}
});
toolBar.pack();

// Creazione Separatore
Label separatore = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
separatore.setBounds(0,83,1000,20);

// Creazione Cavas
Canvas canvas = new Canvas(shell, SWT.BORDER);
canvas.setSize(950, 500);
canvas.setLocation(20, 120);

Point size = shell.computeSize(-1,-1);
shell.setBounds(0, 0, 0, 0);
//shell.setSize(screen.width,screen.height-30);
shell.setSize(1000,700);
shell.open();
Color black = display.getSystemColor(SWT.COLOR_BLACK);
Color white = display.getSystemColor(SWT.COLOR_WHITE);
//canvas.setBackground(white);
//canvas.setBackgroundMode(2);
gc = new GC(canvas);


//canvas.setBackground(white);

gc.setBackground(white);
gc.setForeground(black);
gc.fillRectangle(0, 0, 1000, 900);

//gc.setLineWidth(2);
// Costruzione Pentagramma
gc.drawLine(30, 50, 930, 50);
//gc.drawLine(30, 60, 930, 60);
//gc.drawLine(30, 70, 930, 70);
//gc.drawLine(30, 80, 930, 80);
//gc.drawLine(30, 90, 930, 90);
costruisciPentagramma(50,0);
costruisciPentagramma(120,0);
costruisciPentagramma(190,0);
costruisciPentagramma(260,0);
costruisciPentagramma(330,0);
costruisciPentagramma(400,0);
gc.dispose();
//canvas.redraw();
// shell.pack();


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

}
/*
class ToolBarButtonListener extends SelectionAdapter {
public void widgetSelected(SelectionEvent event) {
if(event.widget.getData().)
shell.close();
System.exit(0);
}
}
*/

public void costruisciPentagramma(int altezza, int chiave) {
// Chiave di Basso
if(chiave==0) {
Image img = new Image(display, "img/chiavebasso80.jpg");
gc.drawImage(img, 25, altezza-5);
}
gc.drawLine(30, altezza, 30, altezza+40);
gc.drawLine(30, altezza, 930, altezza);
gc.drawLine(30, altezza+10, 930, altezza+10);
gc.drawLine(30, altezza+20, 930, altezza+20);
gc.drawLine(30, altezza+30, 930, altezza+30);
gc.drawLine(30, altezza+40, 930, altezza+40);
gc.drawLine(300, altezza,300 ,altezza+40);
gc.drawLine(510, altezza,510 ,altezza+40);
gc.drawLine(720, altezza,720 ,altezza+40);
gc.drawLine(930, altezza,930 ,altezza+40);

}
public void newWindow(String titolo, String messaggio) {
Shell dialog = new Shell(shell);
dialog.setText("Dialog");
Text text4 = new Text(dialog, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
text4.setBounds(0,0,800,500);
text4.setEditable(false);
text4.setText("ciao provola affumicata");
dialog.setSize(800, 500);
dialog.open();
}
class MenuItemListener extends SelectionAdapter {
public void widgetSelected(SelectionEvent event) {
if (((MenuItem) event.widget).getText().equals("E&xit\tCTRL+C")) {
shell.close();
System.exit(0);
}
if (((MenuItem) event.widget).getText().equals("Info")) {
MessageBox aboutBox = new MessageBox(shell,SWT.ICON_INFORMATION);
aboutBox.setText("Informazioni");
StringBuffer messaggio = new StringBuffer();
messaggio.append("Composer Midi\n");
messaggio.append("Programma sviluppato da Fedullo Carmine\n");
messaggio.append("\n");
messaggio.append("Per ulteriori informazioni\n");
messaggio.append("carmine82@gmail.com\n");
aboutBox.setMessage(messaggio.toString());
int buttonOK = aboutBox.open();
//shell.pack();
shell.open();
System.out.println("premuto help");
}
if (((MenuItem) event.widget).getText().equals("Parse Input\tCTRL+P")) {
RWmidi midi = null;
try {
midi = new RWmidi("midi.mid");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Parser parser = new Parser(midi);
String eventi = parser.getParsingTraccia(1);
Shell dialog = new Shell(shell);
dialog.setText("Dialog");
Text text4 = new Text(dialog, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
text4.setBounds(0,0,800,500);
text4.setEditable(false);
text4.setText(eventi);
dialog.setSize(800, 500);
dialog.open();


}
}
}
private GC gc;
private Display display;
}


This occur even when I move another application window over images as you can see by attached picture.

Attached Images
problema2.jpg (17.9 KB, 493 views)


这篇关于Java SWT - Canvas:为什么删除图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆