1 /*******************************************************************************
2  * Copyright (c) 2000, 2013 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.examples.graphics;
15 
16 import java.util.*;
17 
18 import org.eclipse.swt.*;
19 import org.eclipse.swt.graphics.*;
20 
21 public class IntroTab extends AnimatedGraphicsTab {
22 
23 	Font font;
24 	Image image;
25 	Random random = new Random();
26 	float x, y;
27 	float incX = 10.0f;
28 	float incY = 5.0f;
29 	int textWidth, textHeight;
30 	String text = GraphicsExample.getResourceString("SWT");
31 
IntroTab(GraphicsExample example)32 public IntroTab(GraphicsExample example) {
33 	super(example);
34 }
35 
36 @Override
dispose()37 public void dispose() {
38 	if (image != null) image.dispose();
39 	image = null;
40 	if (font != null) font.dispose();
41 	font = null;
42 }
43 
44 @Override
getCategory()45 public String getCategory() {
46 	return GraphicsExample.getResourceString("Introduction"); //$NON-NLS-1$
47 }
48 
49 @Override
getText()50 public String getText() {
51 	return GraphicsExample.getResourceString("SWT"); //$NON-NLS-1$
52 }
53 
54 @Override
getDescription()55 public String getDescription() {
56 	return GraphicsExample.getResourceString("IntroductionDescription"); //$NON-NLS-1$
57 }
58 
59 @Override
next(int width, int height)60 public void next(int width, int height) {
61 	x += incX;
62 	y += incY;
63 	float random = (float)Math.random();
64 	if (x + textWidth > width) {
65 		x = width - textWidth;
66 		incX = random * -width / 16 - 1;
67 	}
68 	if (x < 0) {
69 		x = 0;
70 		incX = random * width / 16 + 1;
71 	}
72 	if (y + textHeight > height) {
73 		y = (height - textHeight)- 2;
74 		incY = random * -height / 16 - 1;
75 	}
76 	if (y < 0) {
77 		y = 0;
78 		incY = random * height / 16 + 1;
79 	}
80 }
81 
82 @Override
paint(GC gc, int width, int height)83 public void paint(GC gc, int width, int height) {
84 	if (!example.checkAdvancedGraphics()) return;
85 	Device device = gc.getDevice();
86 	if (image == null) {
87 		image = example.loadImage(device, "irmaos.jpg");
88 		Rectangle rect = image.getBounds();
89 		FontData fd = device.getSystemFont().getFontData()[0];
90 		font = new Font(device, fd.getName(), rect.height / 4, SWT.BOLD);
91 		gc.setFont(font);
92 		Point size = gc.stringExtent(text);
93 		textWidth = size.x;
94 		textHeight = size.y;
95 	}
96 	Path path = new Path(device);
97 	path.addString(text, x, y, font);
98 	gc.setClipping(path);
99 	Rectangle rect = image.getBounds();
100 	gc.drawImage(image, 0, 0, rect.width, rect.height, 0, 0, width, height);
101 	gc.setClipping((Rectangle)null);
102 	gc.setForeground(device.getSystemColor(SWT.COLOR_BLUE));
103 	gc.drawPath(path);
104 	path.dispose();
105 }
106 }
107