help me about the image redraw! [message #453278] |
Mon, 04 April 2005 03:32 |
Eclipse User |
|
|
|
Originally posted by: wuhui.eastcompeace.com
Hi, anybody:
i have a question about swt image redraw. any anybody help me ?
There is the program i wrote
------------------------
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.SWT;
public class MyPanel {
private Button button = null;
private Composite composite = null;
private Composite parent = null;
private boolean ready = false;
private boolean run = false;
private Card[] currentPokers = null;
public static int startX = 20;
public static int startY = 30;
public static int spaceX = 20;
public static int spaceY = 20;
public MyPanel(Composite parent) {
composite = new Composite(parent, SWT.NULL);
this.parent = parent;
initialize();
}
private void initialize() {
//layout(false);
button = new Button(composite, SWT.BORDER);
button
.setBounds(new org.eclipse.swt.graphics.Rectangle(99, 61, 86,
25));
button.setText("start");
composite.setSize(new org.eclipse.swt.graphics.Point(300, 150));
composite.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
public void mouseDown(org.eclipse.swt.events.MouseEvent e) {
calcCardClicked(e.x, e.y);
drawPoker();
}
});
button
.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(
org.eclipse.swt.events.SelectionEvent e) {
ready = true;
showReady();
}
});
}
public void calcCardClicked(int x, int y) {
if (currentPokers != null) {
int cardsNum = currentPokers.length;
int rightMargin = spaceX * (cardsNum - 1) + Card.cardWidth;
if ((x > startX) && (x < rightMargin)) {
if ((x - startX) < spaceX * (cardsNum - 1)) {
currentPokers[(x - startX) / spaceX].changeCardStatus();
} else {
currentPokers[cardsNum - 1].changeCardStatus();
}
}
}
}
public void showReady() {
//button.setVisible(false);
button.dispose();
button = null;
GC gc = new GC(composite);
//gc.setForeground(Display.getDefault().getSystemColor(SWT.C OLOR_RED));
//gc.setBackground(Display.getDefault().getSystemColor(SWT.C OLOR_BLUE));
gc.drawRectangle(composite.getClientArea());
gc.drawString("you are ready to start...", 40, 70);
gc.dispose();
// emulate start status here.
run = true;
addCards(new int[] { 1, 2, 3, 4, 5, 6, 7 });
}
public void addCards(int[] cards) {
if (cards != null) {
if (currentPokers == null) {
currentPokers = new Card[0];
}
Card[] newCards = new Card[currentPokers.length + cards.length];
System.arraycopy(currentPokers, 0, newCards, 0,
currentPokers.length);
for (int i = 0; i < cards.length; i++) {
newCards[currentPokers.length + i] = new Card(cards[i]);
}
currentPokers = newCards;
}
drawPoker();
}
public void drawPoker() {
if ((composite != null) && !composite.isDisposed()) {
composite.dispose();
}
composite = new Composite(parent, SWT.NULL);
GC gc = new GC(composite);
gc.fillRectangle(composite.getClientArea());
if (currentPokers != null) {
for (int i = 0; i < currentPokers.length; i++) {
Card card = currentPokers[i];
Image img = card.getCardImage();
if (card.isSelected()) {
gc.drawImage(img, startX + i * spaceX, startY - spaceY);
}
gc.drawImage(img, startX + i * spaceX, startY);
//card.getCardImage().dispose();
//img.dispose();
}
}
gc.dispose();
composite.getShell().layout(true);
}
public static void main(String args[]) {
Display display = Display.getDefault();
Shell shell = new Shell(display, SWT.TITLE | SWT.CLOSE | SWT.RESIZE);
MyPanel myPanel = new MyPanel(shell);
shell.layout(true);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
---------------------------------------------------------
The question is that, when i draw a new image, i can not erase the old
image, even the previous button. it's so bothering.... can any one help me ?
plz..
thanks a lot.
|
|
|
Powered by
FUDForum. Page generated in 0.02208 seconds