Java Tutorial/SWT 2D Graphics/SWT Paint — различия между версиями

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

Текущая версия на 18:18, 31 мая 2010

A dynamic gradient

   <source lang="java">

/*******************************************************************************

* Copyright (c) 2000, 2005 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*     IBM Corporation - initial API and implementation
*******************************************************************************/

/*

* Control example snippet: a dynamic gradient
* 
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
* 
* @since 3.2
*/

import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; public class GradientDynamic {

 static Image oldImage;
 public static void main(String[] args) {
   final Display display = new Display();
   final Shell shell = new Shell(display);
   shell.setBackgroundMode(SWT.INHERIT_DEFAULT);
   shell.addListener(SWT.Resize, new Listener() {
     public void handleEvent(Event event) {
       Rectangle rect = shell.getClientArea();
       Image newImage = new Image(display, Math.max(1, rect.width), 1);
       GC gc = new GC(newImage);
       gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
       gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
       gc.fillGradientRectangle(rect.x, rect.y, rect.width, 1, false);
       gc.dispose();
       shell.setBackgroundImage(newImage);
       if (oldImage != null){
         oldImage.dispose();
       }
       oldImage = newImage;
     }
   });
   shell.pack();
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch())
       display.sleep();
   }
   if (oldImage != null)
     oldImage.dispose();
   display.dispose();
 }

}</source>





Alpha blending

   <source lang="java">

/*******************************************************************************

* Copyright (c) 2000, 2005 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*     IBM Corporation - initial API and implementation
*******************************************************************************/

/*

* Drawing with transformations, paths and alpha blending
*
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
* 
* @since 3.1
*/

import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphics.FontData; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.Path; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; public class FillDrawPath {

 public static void main(String[] args) {
   final Display display = new Display();
   final Shell shell = new Shell(display);
   FontData fd = shell.getFont().getFontData()[0];
   final Font font = new Font(display, fd.getName(), 60, SWT.BOLD | SWT.ITALIC);
   final Image image = new Image(display, 640, 480);
   final Rectangle rect = image.getBounds();
   GC gc = new GC(image);
   gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
   gc.fillOval(rect.x, rect.y, rect.width, rect.height);
   gc.dispose();
   shell.addListener(SWT.Paint, new Listener() {
     public void handleEvent(Event event) {
       GC gc = event.gc;
       gc.drawImage(image, 0, 0, rect.width, rect.height, 0, 0, rect.width / 2, rect.height / 2);
       gc.setAlpha(100);
       Path path = new Path(display);
       path.addString("SWT", 0, 0, font);
       gc.setBackground(display.getSystemColor(SWT.COLOR_GREEN));
       gc.setForeground(display.getSystemColor(SWT.COLOR_BLUE));
       gc.fillPath(path);
       gc.drawPath(path);
       path.dispose();
     }
   });
   shell.setSize(shell.ruputeSize(rect.width / 2, rect.height / 2));
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch())
       display.sleep();
   }
   image.dispose();
   font.dispose();
   display.dispose();
 }

}</source>





A paint canvas.

   <source lang="java">

/*

* 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();
   }

}</source>





Implement a simple scribble program

   <source lang="java">

/*******************************************************************************

* Copyright (c) 2000, 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*     IBM Corporation - initial API and implementation
*******************************************************************************/

import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; public class ScribbleProgram {

 public static void main(String[] args) {
   Display display = new Display();
   final Shell shell = new Shell(display);
   Listener listener = new Listener() {
     int lastX = 0, lastY = 0;
     public void handleEvent(Event event) {
       switch (event.type) {
       case SWT.MouseMove:
         if ((event.stateMask & SWT.BUTTON1) == 0)
           break;
         GC gc = new GC(shell);
         gc.drawLine(lastX, lastY, event.x, event.y);
         gc.dispose();
       case SWT.MouseDown:
         lastX = event.x;
         lastY = event.y;
         break;
       }
     }
   };
   shell.addListener(SWT.MouseDown, listener);
   shell.addListener(SWT.MouseMove, listener);
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch())
       display.sleep();
   }
   display.dispose();
 }

}</source>