1 /*******************************************************************************
2  * Copyright (c) 2006, 2016 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 
15 package org.eclipse.swt.examples.graphics;
16 
17 import org.eclipse.swt.graphics.Device;
18 import org.eclipse.swt.graphics.GC;
19 import org.eclipse.swt.graphics.Image;
20 import org.eclipse.swt.graphics.Rectangle;
21 import org.eclipse.swt.graphics.Transform;
22 
23 /**
24  * This tab demonstrates various transformations, such as scaling, rotation, and
25  * translation.
26  */
27 public class CardsTab extends AnimatedGraphicsTab {
28 
29 	float movClubX, movClubY, movDiamondX, movDiamondY, movHeart, movSpade;
30 	float inc_club = 5.0f;
31 	float inc_diamond = 5.0f;
32 	float inc_hearts = 5.0f;
33 	float inc_spade = 5.0f;
34 	float scale, scaleWidth;
35 	int rotationAngle = 0;
36 	float scaleArg = 0;
37 	float heartScale = 0.5f;
38 	float spadeScale = 0.333f;
39 	int clubWidth, diamondWidth, heartWidth, spadeHeight;
40 
41 	Image ace_club, ace_spade, ace_diamond, ace_hearts;
42 
43 /**
44  * Constructor
45  * @param example A GraphicsExample
46  */
CardsTab(GraphicsExample example)47 public CardsTab(GraphicsExample example) {
48 	super(example);
49 }
50 
51 @Override
getCategory()52 public String getCategory() {
53 	return GraphicsExample.getResourceString("Transform"); //$NON-NLS-1$
54 }
55 
56 @Override
getText()57 public String getText() {
58 	return GraphicsExample.getResourceString("Cards"); //$NON-NLS-1$
59 }
60 
61 @Override
getDescription()62 public String getDescription() {
63 	return GraphicsExample.getResourceString("CardsDescription"); //$NON-NLS-1$
64 }
65 
66 @Override
dispose()67 public void dispose() {
68 	if (ace_club != null) {
69 		ace_club.dispose();
70 		ace_club = null;
71 		ace_spade.dispose();
72 		ace_spade = null;
73 		ace_diamond.dispose();
74 		ace_diamond = null;
75 		ace_hearts.dispose();
76 		ace_hearts = null;
77 	}
78 }
79 @Override
next(int width, int height)80 public void next(int width, int height) {
81 	rotationAngle = (rotationAngle+10)%360;
82 
83 	// scaleVal goes from 0 to 1, then 1 to 0, then starts over
84 	scaleArg = (float)((scaleArg == 1) ? scaleArg - 0.1 : scaleArg + 0.1);
85 	scale = (float)Math.cos(scaleArg);
86 
87 	movClubX += inc_club;
88 	movDiamondX += inc_diamond;
89 	movHeart += inc_hearts;
90 	movSpade += inc_spade;
91 
92 	scaleWidth = (float) ((movClubY/height)*0.35 + 0.15);
93 	movClubY = 2*height/5 * (float)Math.sin(0.01*movClubX - 90) + 2*height/5;
94 	movDiamondY = 2*height/5 * (float)Math.cos(0.01*movDiamondX) + 2*height/5;
95 
96 	if (movClubX + clubWidth*scaleWidth > width) {
97 		movClubX = width - clubWidth*scaleWidth;
98 		inc_club = -inc_club;
99 	}
100 	if (movClubX < 0) {
101 		movClubX = 0;
102 		inc_club = -inc_club;
103 	}
104 	if (movDiamondX + diamondWidth*scaleWidth > width) {
105 		movDiamondX = width - diamondWidth*scaleWidth;
106 		inc_diamond = -inc_diamond;
107 	}
108 	if (movDiamondX < 0) {
109 		movDiamondX = 0;
110 		inc_diamond = -inc_diamond;
111 	}
112 	if (movHeart + heartWidth*heartScale > width) {
113 		movHeart = width - heartWidth*heartScale;
114 		inc_hearts = -inc_hearts;
115 	}
116 	if (movHeart < 0) {
117 		movHeart = 0;
118 		inc_hearts = -inc_hearts;
119 	}
120 	if (movSpade + spadeHeight*spadeScale > height) {
121 		movSpade = height - spadeHeight*spadeScale;
122 		inc_spade = -inc_spade;
123 	}
124 	if (movSpade < 0) {
125 		movSpade = 0;
126 		inc_spade = -inc_spade;
127 	}
128 }
129 
130 @Override
paint(GC gc, int width, int height)131 public void paint(GC gc, int width, int height) {
132 	if (!example.checkAdvancedGraphics()) return;
133 	Device device = gc.getDevice();
134 
135 	if (ace_club == null) {
136 		ace_club = GraphicsExample.loadImage(device, GraphicsExample.class, "ace_club.jpg");
137 		ace_spade = GraphicsExample.loadImage(device, GraphicsExample.class, "ace_spade.jpg");
138 		ace_diamond = GraphicsExample.loadImage(device, GraphicsExample.class, "ace_diamond.jpg");
139 		ace_hearts = GraphicsExample.loadImage(device, GraphicsExample.class, "ace_hearts.jpg");
140 	}
141 
142 	clubWidth = ace_club.getBounds().width;
143 	diamondWidth = ace_diamond.getBounds().width;
144 	heartWidth = ace_hearts.getBounds().width;
145 	spadeHeight = ace_spade.getBounds().height;
146 
147 	Transform transform;
148 
149 	// ace of clubs
150 	transform = new Transform(device);
151 	transform.translate((int)movClubX, (int)movClubY);
152 	transform.scale(scaleWidth, scaleWidth);
153 
154 	// rotate on center of image
155 	Rectangle rect = ace_club.getBounds();
156 	transform.translate(rect.width/2, rect.height/2);
157 	transform.rotate(rotationAngle);
158 	transform.translate(-rect.width/2, -rect.height/2);
159 
160 	gc.setTransform(transform);
161 	transform.dispose();
162 	gc.drawImage(ace_club, 0, 0);
163 
164 	// ace of diamonds
165 	transform = new Transform(device);
166 	transform.translate((int)movDiamondX, (int)movDiamondY);
167 	transform.scale(scaleWidth, scaleWidth);
168 	gc.setTransform(transform);
169 	transform.dispose();
170 	gc.drawImage(ace_diamond, 0, 0);
171 
172 	// ace of hearts
173 	transform = new Transform(device);
174 	transform.translate(movHeart, height/2);
175 	transform.scale(heartScale, 0.5f*scale);
176 	gc.setTransform(transform);
177 	transform.dispose();
178 	gc.drawImage(ace_hearts, 0, 0);
179 
180 	// ace of spades
181 	transform = new Transform(device);
182 	transform.translate(movSpade, movSpade);
183 	transform.scale(0.5f*scale, spadeScale);
184 	gc.setTransform(transform);
185 	transform.dispose();
186 	gc.drawImage(ace_spade, 0, 0);
187 }
188 }
189 
190