Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e5c3f6d886a3a5a1bdd57be3c9cb51aec89835d6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*******************************************************************************
 * Copyright (c) 2006, 2016 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
 *******************************************************************************/

package org.eclipse.swt.examples.graphics;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Path;
import org.eclipse.swt.graphics.Point;

/**
 * This tab is an animated graphic. It presents the word SWT in different fonts
 * as it bounces around the screen.
 */
public class FontBounceTab extends AnimatedGraphicsTab {
	
	float x, y;
	float incX = 10.0f;				// units by which to move the word along X axis
	float incY = 5.0f;				// units by which to move the word along Y axis 
	int textWidth, textHeight;		// width and height of the word SWT
	String text = GraphicsExample.getResourceString("SWT");
	int fontSize = 100;
	int fontFace = 0; 
	int foreGrdColor, fillColor;	// font colors
	int fontStyle;				// represents various style attributes applicable to a Font
	
public FontBounceTab(GraphicsExample example) {
	super(example);
}

@Override
public String getCategory() {
	return GraphicsExample.getResourceString("Font"); //$NON-NLS-1$
}

@Override
public String getText() {
	return GraphicsExample.getResourceString("Bounce"); //$NON-NLS-1$
}

@Override
public String getDescription() {
	return GraphicsExample.getResourceString("FontBounceDescription"); //$NON-NLS-1$
}

@Override
public void next(int width, int height) {
	x += incX;
    y += incY;
	float random = (float)Math.random();
	
	// collision with right side of screen 
    if (x + textWidth > width) {
        x = width - textWidth;
        incX = random * -width / 16 - 1;
        fontFace = 0;
        fontSize = 125;
        fillColor = SWT.COLOR_DARK_BLUE;  
        foreGrdColor = SWT.COLOR_YELLOW;
        fontStyle = SWT.ITALIC;
    }
	// collision with left side of screen
    if (x < 0) {
        x = 0;
        incX = random * width / 16 + 1;
        fontFace = 1;
        fontSize = 80;
        fillColor = SWT.COLOR_DARK_MAGENTA;
        foreGrdColor = SWT.COLOR_CYAN;
        fontStyle = SWT.NONE;
    }
	// collision with bottom side of screen
    if (y + textHeight > height) {
        y = (height - textHeight)- 2;
        incY = random * -height / 16 - 1;
        fontFace = 2;
        fontSize = 100;
        fillColor = SWT.COLOR_YELLOW;
        foreGrdColor = SWT.COLOR_BLACK;
        fontStyle = SWT.BOLD;
    }
	// collision with top side of screen
    if (y < 0) {
        y = 0;
        incY = random * height / 16 + 1;
        fontFace = 3;
        fontSize = 120;
        fillColor = SWT.COLOR_GREEN;
        foreGrdColor = SWT.COLOR_GRAY;
        fontStyle = SWT.NONE;
    }
}


@Override
public void paint(GC gc, int width, int height) {
	if (!example.checkAdvancedGraphics()) return;
	Device device = gc.getDevice();
	
	Font font = new Font(device, getPlatformFontFace(fontFace), fontSize, fontStyle);
	gc.setFont(font);
	
	Point size = gc.stringExtent(text);
	textWidth = size.x;
	textHeight = size.y;
	
	Path path = new Path(device);
	path.addString(text, x, y, font);

	gc.setForeground(device.getSystemColor(foreGrdColor));
	gc.setBackground(device.getSystemColor(fillColor));

	gc.fillPath(path);
	gc.drawPath(path);
	font.dispose();
	path.dispose();
}

/**
 * Returns the name of the font using the specified index.
 * This method takes into account the resident platform.
 * 
 * @param index
 * 			The index of the font to be used
 */
static String getPlatformFontFace(int index) {	
	if(SWT.getPlatform() == "win32") {
		return new String [] {"Arial", "Impact", "Times", "Verdana"} [index];	
	} else if (SWT.getPlatform() == "gtk") {
		return new String [] {"URW Chancery L", "Baekmuk Batang", "Baekmuk Headline", "KacsTitleL"} [index];
	} else { 
		return new String [] {"Arial", "Impact", "Times", "Verdana"} [index];
	}
}
}

Back to the top