Java/SWT JFace Eclipse/Canvas

Материал из Java эксперт
Перейти к: навигация, поиск

A paint canvas.

 
/* 
 * JFreeChart : a free chart library for the Java(tm) platform
 * 
 *
 * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
 *
 * Project Info:  http://www.jfree.org/jfreechart/index.html
 *
 * This library is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License as published by 
 * the Free Software Foundation; either version 2.1 of the License, or 
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
 * USA.  
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 * in the United States and other countries.]
 *
 * -------------------
 * SwtPaintCanvas.java
 * -------------------
 * (C) Copyright 2000-2007, by Object Refinery Limited.
 *
 * Original Author:  Henry Proudhon (henry.proudhon AT ensmp.fr);
 * Contributor(s):   David Gilbert (for Object Refinery Limited);
 *
 * Changes
 * -------
 * 04-Aug-2006 : New class (HP);
 * 25-Oct-2007 : Fixed Eclipse warnings (DG);
 * 
 */

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.ruposite;
/**
 * A paint canvas.
 */
public class SWTPaintCanvas extends Canvas {
    private Color myColor;
    
    /**
     * Creates a new instance.
     * 
     * @param parent  the parent.
     * @param style  the style.
     * @param color  the color.
     */
    public SWTPaintCanvas(Composite parent, int style, Color color) {
        this(parent, style);
        setColor(color);
    }
    
    /**
     * Creates a new instance.
     * 
     * @param parent  the parent.
     * @param style  the style.
     */
    public SWTPaintCanvas(Composite parent, int style) {
        super(parent, style);
        addPaintListener(new PaintListener() {
            public void paintControl(PaintEvent e) {
                e.gc.setForeground(e.gc.getDevice().getSystemColor(
                        SWT.COLOR_BLACK));
                e.gc.setBackground(SWTPaintCanvas.this.myColor);
                e.gc.fillRectangle(getClientArea());
                e.gc.drawRectangle(getClientArea().x, getClientArea().y, 
                        getClientArea().width - 1, getClientArea().height - 1);
            }
        });
    }
    
    /**
     * Sets the color.
     * 
     * @param color  the color.
     */
    public void setColor(Color color) {
        if (this.myColor != null) {
            this.myColor.dispose();
        }
        //this.myColor = new Color(getDisplay(), color.getRGB());
        this.myColor = color;
    }
    /**
     * Returns the color.
     * 
     * @return The color.
     */
    public Color getColor() {
        return this.myColor;
    }
    
    /**
     * Overridden to do nothing.
     * 
     * @param c  the color.
     */
    public void setBackground(Color c) {
        return;
    }
    /**
     * Overridden to do nothing.
     * 
     * @param c  the color.
     */
    public void setForeground(Color c) {
        return;
    }
    
    /**
     * Frees resources.
     */
    public void dispose() {
        this.myColor.dispose();
    }
}





Canvas example snippet: implement tab traversal

 
/******************************************************************************
 * Copyright (c) 1998, 2004 Jackwind Li Guojie
 * All right reserved. 
 * 
 * Created on Jan 24, 2004 3:03:14 PM by JACK
 * $Id$
 * 
 * visit: http://www.asprise.ru/swt
 *****************************************************************************/
/*
 * Canvas example snippet: implement tab traversal (behave like a tab group)
 *
 * For a list of all SWT example snippets see
 * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class CanvasExample {
  public static void main(String[] args) {
    Display display = new Display();
    
    Image small = new Image(display, 16, 16);
    GC gc = new GC(small);
    gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
    gc.fillArc(0, 0, 16, 16, 45, 270);
    gc.dispose();
    
    Image large = new Image(display, 32, 32);
    gc = new GC(large);
    gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
    gc.fillArc(0, 0, 32, 32, 45, 270);
    gc.dispose();
    
    /* Provide different resolutions for icons to get
     * high quality rendering wherever the OS needs 
     * large icons. For example, the ALT+TAB window 
     * on certain systems uses a larger icon.
     */
    Shell shell = new Shell(display);
    shell.setText("Small and Large icons");
    shell.setImages(new Image[] {small, large});
    /* No large icon: the OS will scale up the
     * small icon when it needs a large one.
     */
    Shell shell2 = new Shell(display);
    shell2.setText("Small icon");
    shell2.setImage(small);
    
    shell.open();
    shell2.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Demonstrates a Canvas

 
//Send questions, comments, bug reports, etc. to the authors:
//Rob Warner (rwarner@interspatial.ru)
//Robert Harris (rbrt_harris@yahoo.ru)
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
/**
 * This class demonstrates a Canvas
 */
public class SimpleCanvas {
  /**
   * Runs the application
   */
  public void run() {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Canvas Example");
    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());
    // Create a canvas
    Canvas canvas = new Canvas(shell, SWT.NONE);
    // Create a button on the canvas
    Button button = new Button(canvas, SWT.PUSH);
    button.setBounds(10, 10, 300, 40);
    button.setText("You can place widgets on a canvas");
    // Create a paint handler for the canvas
    canvas.addPaintListener(new PaintListener() {
      public void paintControl(PaintEvent e) {
        // Do some drawing
        Rectangle rect = ((Canvas) e.widget).getBounds();
        e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_RED));
        e.gc.drawFocus(5, 5, rect.width - 10, rect.height - 10);
        e.gc.drawText("You can draw text directly on a canvas", 60, 60);
      }
    });
  }
  /**
   * The application entry point
   * 
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    new SimpleCanvas().run();
  }
}