有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在单击按钮时重新绘制SWT画布

我可以使用PaintListener事件成功地在画布上绘制。一旦我画了矩阵。我需要创建一个按钮,当点击它时,将删除矩阵的随机行和列。问题是,当打开按钮选择侦听器时,我无法获得图形GC的句柄。那么,我该如何处理这个问题呢。请检查以下代码:

package com.matrix.example;

import java.util.Random;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * This class demonstrates drawing lines
 */
public class MatrixVisualizer {
    private Matrix denseMatrix;
    private Matrix sparseMatrix;
    Display display;
    static double[][] myMatrix = new double[][] { { 1, 9, 8, 3, 7, 0 },
            { 0, 2, 0, 1, 2, 4 }, { 2, 0, 1, 3, 8, 0 }, { 1, 9, 8, 2, 1, 0 },
            { 2, 0, 1, 4, 5, 6 } };

    public void run() {
        display = new Display();
        Shell shell = new Shell(display);
        shell.setText("Matrix Visualizor");
        shell.setSize(800, 600);

        createContents(shell);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }

    /**
     * Creates the main window's contents
     * 
     * @param shell
     *            the main window
     */
    private void createContents(Shell shell) {
        shell.setLayout(new FillLayout());
        final Canvas denseCanvas = new Canvas(shell, SWT.NONE);
        denseCanvas.setLocation(400, 400);

        Button hideButton = new Button(denseCanvas, SWT.PUSH);
        hideButton.setBounds(250, 10, 100, 40);
        hideButton.setText("Hide");

        denseCanvas.addPaintListener(new PaintListener() {
            public void paintControl(PaintEvent e) {
              renderMatrix(denseMatrix, e.gc);

            }
        });
        hideButton.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                Random rand = new Random();
                int randCol = rand.nextInt(5) + 1;
                int randRow = rand.nextInt(5) + 1;
                denseMatrix = new DecoratorMatrix(myMatrix,true);
                denseMatrix.setColumnNumber(randCol);
                denseMatrix.setRowNumber(randRow);
                denseCanvas.redraw();

            }
        });
    }

    private void renderMatrix(Matrix activeMatrix, GC c)
    {
        activeMatrix = new DenseMatrix(myMatrix);
        Painter painter = new Painter();
        painter.setGraphics(c);
        activeMatrix.paintMatrix(painter);
    }

    /**
     * The application entry point
     * 
     * @param args
     *            the command line arguments
     */
    public static void main(String[] args) {
        new MatrixVisualizer().run();
    }
}

共 (0) 个答案