
Da ich gesehen habe, dass bereits jemand ein Tutorial für ein Bewertungs-Panel in Swing erstellt hat, dachte ich mir, dass ich mein Bewertungs-Panel in SWT auch mal der Community preis gebe.

Vom optischen ist es ziemlich genau das selbe. Allerdings habe ich noch ein paar nette Funktionen zum selber gestalten eingebaut.


Wie Funktionierts?
- Wenn die Maus über einen Stern bewegt wird, so färbt sich dieser und die vorhergehenden ein
- Klickt man auf einen Stern, bleibt dieser und die vorhergehenden Sterne farbig
- Durch erneutes klicken auf den selben oder einen anderen Stern, wird die Auswahl geändert/abgewählt
Was für Funktionen bietet mir SWTRatingBar?
- Anzahl der Sterne
- Anzahl der Sternspitzen
- Größe der Spitzen
- Tiefe der Einbuchtungen
- Abstand vom oberen Rand
- Abstand vom linken Rand
- Farbe der Sterne

Mithilfe von diesen Funktionen ist diese Bar leicht modifizierbar.
Quellcode
Die Klasse SWTRatingBar:
Code java:
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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 | import java.awt.Polygon;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
public class SWTRatingBar extends Canvas implements PaintListener, MouseListener, MouseMoveListener
{
private static final int STAR_COUNT = 5;
private static final int STAR_FRACTIONS = 5;
private static final int STAR_FRACTIONS_SIZE = 10;
private static final int STAR_BAY_SIZE = 5;
private static final int DEFAULT_PADDING_TOP = 10;
private static final int DEFAULT_PADDING_LEFT = 10;
private static final Color DEFAULT_COLOR_SELECTED = new Color(null, 255, 220, 100);
private static final Color COLOR_NOT_SELECTED = new Color(null, 230, 230, 230);
private static final Color COLOR_BORDER = new Color(null, 150, 150, 150);
private final RatingBarModel model;
/**
* Anzahl der Sterne.
*/
private int starCount = STAR_COUNT;
/**
* Anzahl der Sternspitzen.
*/
private int starFractions = STAR_FRACTIONS;
/**
* Die Entfernung vom Sternmittelpunkt bis zur Sterspitze.
*/
private int starFractionsSize = STAR_FRACTIONS_SIZE;
/**
* Die Entfernung vom Sternmittelpunkt bis zur Einbuchtung.
*/
private int starBaySize = STAR_BAY_SIZE;
/**
* Abstand vom oberen Rand.
*/
private int paddingTop = DEFAULT_PADDING_TOP;
/**
* Abstand vom linken Rand.
*/
private int paddingLeft = DEFAULT_PADDING_LEFT;
/**
* Abstand zwischen den Sternen. Beginnt bei <code>x = 0</code>.
*/
private int padding = 0;
/**
* Farbe der Sterne.
*/
private Color colorSelected = DEFAULT_COLOR_SELECTED;
/**
* @param parent
* @param style
*/
public SWTRatingBar(Composite parent, int style) {
super(parent, style);
this.model = new RatingBarModel();
addPaintListener(this);
addMouseListener(this);
addMouseMoveListener(this);
}
/**
*
*/
public void init() {
model.init();
}
/* (non-Javadoc)
* @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent)
*/
public void paintControl(PaintEvent e) {
Image image = (Image) getData("double-buffer-image");
if (image == null
|| image.getBounds().width != getSize().x
|| image.getBounds().height != getSize().y) {
image =
new Image(
Display.getCurrent(),
getSize().x,
getSize().y);
setData("double-buffer-image", image);
}
// Initializes the graphics context of the image.
GC imageGC = new GC(image);
// clear Background
imageGC.setBackground(e.gc.getBackground());
imageGC.setForeground(e.gc.getForeground());
Rectangle imageSize = image.getBounds();
imageGC.fillRectangle(0, 0, imageSize.width + 1, imageSize.height + 1);
// draw Stars
for(int id = 0; id < model.stars.size(); id++) {
Star star = model.stars.get(id);
if(star.isSelected() || star.isHovered()) {
imageGC.setBackground(colorSelected);
} else {
imageGC.setBackground(COLOR_NOT_SELECTED);
}
imageGC.setForeground(COLOR_BORDER);
imageGC.fillPolygon(star.getPolygon());
imageGC.drawPolygon(star.getPolygon());
}
// Draws the buffer image onto the canvas.
e.gc.drawImage(image, 0, 0);
imageGC.dispose();
}
private Star createStar(int starNr) {
int[] starPolygon = new int[starFractions*4];
padding += paddingLeft;
int starMiddleX = padding + starFractionsSize + starFractionsSize * (2 * starNr);
int starMiddleY = paddingTop + starFractionsSize;
double angleFraction = Math.toRadians(-(360/starFractions));
int lastFractionX = starMiddleX;
int lastFractionY = starMiddleY-starFractionsSize;
double angleBay = Math.toRadians(-(360/starFractions));
int lastBayX = (int) (starMiddleX + (starMiddleX - starMiddleX) * Math.cos(Math.toRadians(360/(starFractions*2))) - ((starMiddleY-starBaySize) - starMiddleY) * Math.sin(Math.toRadians(360/(starFractions*2))));
int lastBayY = (int) (starMiddleY + (starMiddleX - starMiddleX) * Math.sin(Math.toRadians(360/(starFractions*2))) + ((starMiddleY-starBaySize) - starMiddleY) * Math.cos(Math.toRadians(360/(starFractions*2))));
for(int i = 0; i < starPolygon.length; i++) {
int x = -1;
int y = -1;
if(i % 4 == 0) {
x = (int) (starMiddleX + (lastBayX - starMiddleX) * Math.cos(angleBay) - (lastBayY - starMiddleY) * Math.sin(angleBay));
y = (int) (starMiddleY + (lastBayX - starMiddleX) * Math.sin(angleBay) + (lastBayY - starMiddleY) * Math.cos(angleBay));
lastBayX = x;
lastBayY = y;
} else {
x = (int) (starMiddleX + (lastFractionX - starMiddleX) * Math.cos(angleFraction) - (lastFractionY - starMiddleY) * Math.sin(angleFraction));
y = (int) (starMiddleY + (lastFractionX - starMiddleX) * Math.sin(angleFraction) + (lastFractionY - starMiddleY) * Math.cos(angleFraction));
lastFractionX = x;
lastFractionY = y;
}
starPolygon[i] = x;
starPolygon[++i] = y;
}
return new Star(starNr, starPolygon);
}
/* (non-Javadoc)
* @see org.eclipse.swt.events.MouseListener#mouseDoubleClick(org.eclipse.swt.events.MouseEvent)
*/
public void mouseDoubleClick(MouseEvent e) {}
/* (non-Javadoc)
* @see org.eclipse.swt.events.MouseListener#mouseDown(org.eclipse.swt.events.MouseEvent)
*/
public void mouseDown(MouseEvent e) {
model.mouseDown(e.x, e.y);
}
/* (non-Javadoc)
* @see org.eclipse.swt.events.MouseListener#mouseUp(org.eclipse.swt.events.MouseEvent)
*/
public void mouseUp(MouseEvent e) {}
/* (non-Javadoc)
* @see org.eclipse.swt.events.MouseMoveListener#mouseMove(org.eclipse.swt.events.MouseEvent)
*/
public void mouseMove(MouseEvent e) {
model.mouseMove(e.x, e.y);
}
/**
* @param starCount the starCount to set
*/
public void setStarCount(int starCount) {
this.starCount = starCount;
}
/**
* @return the starCount
*/
public int getStarCount() {
return starCount;
}
/**
* @param starFractions the starFractions to set
*/
public void setStarFractions(int starFractions) {
this.starFractions = starFractions;
}
/**
* @return the starFractions
*/
public int getStarFractions() {
return starFractions;
}
/**
* @param starFractionsSize the starFractionsSize to set
*/
public void setStarFractionsSize(int starFractionsSize) {
this.starFractionsSize = starFractionsSize;
}
/**
* @return the starFractionsSize
*/
public int getStarFractionsSize() {
return starFractionsSize;
}
/**
* @param starBaySize the starBaySize to set
*/
public void setStarBaySize(int starBaySize) {
this.starBaySize = starBaySize;
}
/**
* @return the starBaySize
*/
public int getStarBaySize() {
return starBaySize;
}
/**
* @param paddingTop the paddingTop to set
*/
public void setPaddingTop(int paddingTop) {
this.paddingTop = paddingTop;
}
/**
* @return the paddingTop
*/
public int getPaddingTop() {
return paddingTop;
}
/**
* @param paddingLeft the paddingLeft to set
*/
public void setPaddingLeft(int paddingLeft) {
this.paddingLeft = paddingLeft;
}
/**
* @return the paddingLeft
*/
public int getPaddingLeft() {
return paddingLeft;
}
/**
* @param colorSelected the colorSelected to set
*/
public void setColorSelectedStar(Color colorSelected) {
this.colorSelected = colorSelected;
}
/**
* @return the colorSelected
*/
public Color getColorSelectedStar() {
return colorSelected;
}
/**
* @return
*/
public int getSelectedStars() {
return model.getSelectedStars();
}
private final class RatingBarModel
{
private final List<Star> stars;
private Rectangle rectangle;
private boolean isFreezed = false;
private int lastSelectedStar = -1;
private int lastHouveredStar = -1;
/**
*
*/
public RatingBarModel() {
this.stars = new LinkedList<Star>();
}
/**
*
*/
public void init() {
for(int starNr = 0; starNr < getStarCount(); starNr++) {
Star star = createStar(starNr);
stars.add(star);
}
rectangle = new Rectangle(getPaddingLeft(), getPaddingTop(),
getStarFractionsSize() * 2 * stars.size() + (padding),
getStarFractionsSize()*2);
}
/**
* @param x
* @param y
*/
public void mouseMove(int x, int y) {
if(!rectangle.contains(x, y)) {
freez();
return;
}
isFreezed = false;
for(int i = 0; i < stars.size(); i++) {
Star star = stars.get(i);
if(star.intersect(x, y)) {
setStarHovered(star.getId());
return;
}
}
}
/**
* @param x
* @param y
*/
public void mouseDown(int x, int y) {
for(int i = 0; i < stars.size(); i++) {
Star star = stars.get(i);
if(star.intersect(x, y)) {
setStarSelected(star.getId());
return;
}
}
}
/**
* @param nr
*/
public void setStarHovered(int nr) {
for(int i = nr; i > -1; i--) {
stars.get(i).setHovered(true);
}
if(nr+1 < stars.size()) {
for (int i = nr+1; i < stars.size(); i++) {
stars.get(i).setHovered(false);
}
}
if(lastHouveredStar != nr) {
redraw();
lastHouveredStar = nr;
}
}
/**
* @param nr
*/
public void setStarSelected(int nr) {
if(lastSelectedStar == nr) {
for (int i = 0; i < stars.size(); i++) {
stars.get(i).setSelected(false);
}
lastSelectedStar = -1;
return;
}
for(int i = nr; i > -1; i--) {
stars.get(i).setSelected(true);
}
for (int i = nr+1; i < stars.size(); i++) {
stars.get(i).setSelected(false);
}
lastSelectedStar = nr;
}
/**
*
*/
public void freez() {
if(isFreezed) {
return;
}
isFreezed = true;
for(int i = 0; i < stars.size(); i++) {
Star star = stars.get(i);
if(!star.isSelected()) {
star.setHovered(false);
star.setSelected(false);
} else {
star.setHovered(false);
}
}
lastHouveredStar = -1;
redraw();
}
/**
* @return
*/
public int getSelectedStars() {
int count = 0;
for(int i = 0; i < stars.size(); i++) {
Star star = stars.get(i);
if(star.isSelected()) {
count++;
} else {
return count;
}
}
return 0;
}
}
private final class Star
{
private final Polygon polygon;
private final int[] polygonArray;
private final int id;
private boolean isSelected = false;
private boolean isHovered = false;
/**
* @param id
* @param polygonArray
*/
public Star(int id, int[] polygonArray) {
this.id = id;
this.polygonArray = polygonArray;
this.polygon = new Polygon();
for(int i = 0; i < polygonArray.length; i++) {
polygon.addPoint(polygonArray[i], polygonArray[++i]);
}
}
/**
* @param x
* @param y
* @return
*/
public boolean intersect(int x, int y) {
return polygon.contains(x, y);
}
/**
* @return the polygon
*/
public int[] getPolygon() {
return polygonArray;
}
/**
* @param isSelected
*/
public void setSelected(boolean isSelected) {
this.isSelected = isSelected;
}
/**
* @return the isSelected
*/
public boolean isSelected() {
return isSelected;
}
/**
* @param isHovered the isHovered to set
*/
public void setHovered(boolean isHovered) {
if(isSelected) {
return;
}
this.isHovered = isHovered;
}
/**
* @return the isHovered
*/
public boolean isHovered() {
return isHovered;
}
/**
* @return the id
*/
public int getId() {
return id;
}
}
} |
Test
Die Klasse SWTRatingBarDemo:
Code java:
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 | import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SWTRatingBarDemo
{
/**
* @param args
*/
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setSize(400, 120);
shell.setText("SWTRatingBar - Demo");
shell.setLayout(new FillLayout());
SWTRatingBar bar = new SWTRatingBar(shell, SWT.NONE);
bar.setColorSelectedStar(new Color(null, 255, 50, 100));
bar.setStarFractions(8);
bar.setStarCount(10);
bar.init();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
} |
Ich hoffe ihr seid mit dem Tutorial zufrieden. Falls ihr noch Anregungen oder Verbesserungsvorschläge für die RatingBar habt, nehme ich diese gerne entgegen.
Der Sourcecode hängt für euch auch nochmal im Anhang.
SWTRatingBar-src.zip
Viel Spaß und Vergnügen damit.



Kommentar schreiben

Bereiche
Kategorien
Forum - Programming





Artikel bewerten