SWT 2D Simple Demo : 2D « SWT JFace Eclipse « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » SWT JFace Eclipse » 2DScreenshots 
SWT 2D Simple Demo
SWT 2D Simple Demo

/*******************************************************************************
 * Copyright (c) 2004 Berthold Daum. All rights reserved. This program and the
 * accompanying materials are made available under the terms of the Common
 * Public License v1.0 which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 
 * Contributors: Berthold Daum
 ******************************************************************************/

import java.awt.RenderingHints;
import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
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.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class SWT2D {
  // Shell fur Popup-Editor
  Shell eShell = null;

  // Text-Widget fur Editor
  Text eText = null;

  // Liste eingegebener Zeichenketten
  ArrayList wordList = new ArrayList(12);

  public static void main(String[] args) {
    SWT2D swtawt = new SWT2D();
    swtawt.run();
  }

  private void run() {
    // Create top level shell
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Java 2D Example");
    // GridLayout for canvas and button
    shell.setLayout(new GridLayout());
    // Create container for AWT canvas
    final Composite canvasComp = new Composite(shell, SWT.EMBEDDED);
    // Set preferred size
    GridData data = new GridData();
    data.widthHint = 600;
    data.heightHint = 500;
    canvasComp.setLayoutData(data);
    // Create AWT Frame for Canvas
    java.awt.Frame canvasFrame = SWT_AWT.new_Frame(canvasComp);
    // Create Canvas and add it to the Frame
    final java.awt.Canvas canvas = new java.awt.Canvas();
    canvasFrame.add(canvas);
    // Get graphical context and cast to Java2D
    final java.awt.Graphics2D g2d = (java.awt.Graphics2Dcanvas
        .getGraphics();
    // Enable antialiasing
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);
    // Remember initial transform
    final java.awt.geom.AffineTransform origTransform = g2d.getTransform();
    // Create Clear button and position it
    Button clearButton = new Button(shell, SWT.PUSH);
    clearButton.setText("Clear");
    data = new GridData();
    data.horizontalAlignment = GridData.CENTER;
    clearButton.setLayoutData(data);
    // Event processing for Clear button
    clearButton.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        // Delete word list and redraw canvas
        wordList.clear();
        canvasComp.redraw();
      }
    });
    // Process canvas mouse clicks
    canvas.addMouseListener(new java.awt.event.MouseListener() {
      public void mouseClicked(java.awt.event.MouseEvent e) {
      }

      public void mouseEntered(java.awt.event.MouseEvent e) {
      }

      public void mouseExited(java.awt.event.MouseEvent e) {
      }

      public void mousePressed(java.awt.event.MouseEvent e) {
        // Manage pop-up editor
        display.syncExec(new Runnable() {
          public void run() {
            if (eShell == null) {
              // Create new Shell: non-modal!
              eShell = new Shell(shell, SWT.NO_TRIM
                  | SWT.MODELESS);
              eShell.setLayout(new FillLayout());
              // Text input field
              eText = new Text(eShell, SWT.BORDER);
              eText.setText("Text rotation in the SWT?");
              eShell.pack();
              // Set position (Display coordinates)
              java.awt.Rectangle bounds = canvas.getBounds();
              org.eclipse.swt.graphics.Point pos = canvasComp
                  .toDisplay(bounds.width / 2,
                      bounds.height / 2);
              Point size = eShell.getSize();
              eShell.setBounds(pos.x, pos.y, size.x, size.y);
              // Open Shell
              eShell.open();
            else if (!eShell.isVisible()) {
              // Editor versteckt, sichtbar machen
              eShell.setVisible(true);
            else {
              // Editor is visible - get text
              String t = eText.getText();
              // set editor invisible
              eShell.setVisible(false);
              // Add text to list and redraw canvas
              wordList.add(t);
              canvasComp.redraw();
            }
          }
        });
      }

      public void mouseReleased(java.awt.event.MouseEvent e) {
      }
    });
    // Redraw the canvas
    canvasComp.addPaintListener(new PaintListener() {
      public void paintControl(PaintEvent e) {
        // Pass the redraw task to AWT event queue
        java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
            // Compute canvas center
            java.awt.Rectangle bounds = canvas.getBounds();
            int originX = bounds.width / 2;
            int originY = bounds.height / 2;
            // Reset canvas
            g2d.setTransform(origTransform);
            g2d.setColor(java.awt.Color.WHITE);
            g2d.fillRect(00, bounds.width, bounds.height);
            // Set font
            g2d.setFont(new java.awt.Font("Myriad",
                java.awt.Font.PLAIN, 32));
            double angle = 0d;
            // Prepare star shape
            double increment = Math.toRadians(30);
            Iterator iter = wordList.iterator();
            while (iter.hasNext()) {
              // Determine text colors in RGB color cycle
              float red = (float) (0.5 0.5 * Math.sin(angle));
              float green = (float) (0.5 0.5 * Math.sin(angle
                  + Math.toRadians(120)));
              float blue = (float) (0.5 0.5 * Math.sin(angle
                  + Math.toRadians(240)));
              g2d.setColor(new java.awt.Color(red, green, blue));
              // Redraw text
              String text = (Stringiter.next();
              g2d.drawString(text, originX + 50, originY);
              // Rotate for next text output
              g2d.rotate(increment, originX, originY);
              angle += increment;
            }
          }
        });
      }
    });
    // Finish shell and open it
    shell.pack();
    shell.open();
    // SWT event processing
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }

}



           
       
Related examples in the same category
1. SWT 2D Chart: FlowchartSWT 2D Chart: Flowchart
2. Use of Java2D on SWT or Draw2D graphical contextUse of Java2D on SWT or Draw2D graphical context
3. SWT 2D UnicodeSWT 2D Unicode
4. SWT Draw 2DSWT Draw 2D
5. SWT Draw2D Example
6. XORXOR
7. Animations ExampleAnimations Example
8. Alpha Fade InAlpha Fade In
9. DrawingsDrawings
10. Draw Text DemoDraw Text Demo
11. GC Creation
12. PalettesPalettes
13. TransparencyTransparency
14. Draw2D SampleDraw2D Sample
15. Class AnalyzerClass Analyzer
16. Demonstrates animation. It uses double buffering.Demonstrates animation. It uses double buffering.
17. Demonstrates drawing an ArcDemonstrates drawing an Arc
18. Demonstrates drawing polygonsDemonstrates drawing polygons
19. Demonstrates drawing points. It draws a sine waveDemonstrates drawing points. It draws a sine wave
20. Draw Round RectangleDraw Round Rectangle
21. Displays information about the display deviceDisplays information about the display device
22. Demonstrates the effects of the flags on the constructorDemonstrates the effects of the flags on the constructor
23. Demonstrates how to draw vertical textDemonstrates how to draw vertical text
24. Utility methods for drawing graphics
25. Demonstrates drawing linesDemonstrates drawing lines
26. Demonstrates animationDemonstrates animation
27. Demonstrates how to draw textDemonstrates how to draw text
28. Demonstrates drawing ovalsDemonstrates drawing ovals
29. Demonstrates how to draw text in colorsDemonstrates how to draw text in colors
30. SWT Paint Example
31. SWT Graphics ExampleSWT Graphics Example
32. SWT OpenGL snippet: draw a square
33. Drawing with transformations, paths and alpha blendingDrawing with transformations, paths and alpha blending
34. Draw lines and polygons with different cap and join stylesDraw lines and polygons with different cap and join styles
35. GC: implement a simple scribble programGC: implement a simple scribble program
36. GC: measure a stringGC: measure a string
37. GC example: draw a thick lineGC example: draw a thick line
38. How to draw directly on an SWT ControlHow to draw directly on an SWT Control
w_ww___.j___a_v_a2s.com | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.