Make a button with an image - java

In fact, I've managed to do that BUT i get a problem that can be easily seen in the following image:
As you can see, there's a border between the image "jugar" and the final of the button. All I want to do is to remove that, so only the background image and the button "jugar" are seen. Here's my code:
public final class GUI extends Application {
#Override
public void start(final Stage primaryStage) throws InterruptedException, FileNotFoundException {
primaryStage.setTitle("CARCASSONE");
StackPane layout = new StackPane();
ImageView jugar = new ImageView(new Image(new FileInputStream("JUGAR.png")));
final Button openButton = new Button(null, jugar);
openButton.;
layout.getChildren().add(openButton);
BackgroundImage bI = new BackgroundImage(new Image(new FileInputStream("CARCASSONE.png")), null, null, null, null);
layout.setBackground(new Background(bI));
openButton.setOnAction(
new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
jugarPartida(primaryStage);
}
});
Scene inici = new Scene(layout, 610, 900);
primaryStage.setScene(inici);
primaryStage.show();
}
I must use JavaFX, so jbutton can't be used.
Any idea to solve that? Just want to remove that annoying border.

The problem with a transparent background is that there’s still an invisible rectangular area that responds to mouse clicks.
You can set the button’s region shape and its clip to a shape that matches your image bounds, so the Button effectively does not exist outside of those bounds:
openButton.setStyle("-fx-padding: 0;");
SVGPath shape = new SVGPath();
shape.setContent("M 18 0 "
+ "H 251 C 265 0 277 7 277 25 "
+ "V 52 C 277 69 265 76 251 76 "
+ "H 18 C 12 76 0 69 0 52 "
+ "V 25 C 0 7 12 0 18 0 "
+ "z");
shape.setFill(Color.BLACK);
openButton.setShape(shape);
openButton.setClip(shape);
Quick SVG path tutorial:
M means moveto (start drawing at that position)
H means draw horizontal line to the specified X position
V means draw vertical line to the specified Y position
C means curveto (draw Bézier curve using specified control points; last coordinate pair is the curve’s final endpoint)
z means close the shape

What you can do is apply css to set the color of openbutton to something like rgba(255, 255, 255, 0.00). You can also do this directly in code.
Something like String style = "-fx-background-color: rgba(255, 255, 255, 0.00);";
openButton.setStyle(style);
The last 0.0 is will make it transparent.

Related

Add spot color (direct tone) to a PDF

Does any one have example code to add a direct tone to a PDF?
It is to a printer production to detect a rectangle to cut up.
this spot color is named « decoupe » and i need it for the rectangle.
I don’t need to modify the CMYK value of the separation.
I need to had a color named "decoupe" to the document and use this color to create a rectangle with this color, the printer detects this color to cut the document to the format. In the PDF document the line should be like that: 14 0 obj [/Separation /decoupe /DeviceCMYK << /Range [0 1 0 1 0 1 0 1] /C0 [0 0 0 0] /C1 [0.000000 0.000000 0.000000 0.000000] /FunctionType 2 /Domain [0 1] /N 1>>] endobj
This code adds a rectangle to an existing PDF with a spot color. I changed the c1 values to 1 1 1 1 so that something gets visible (yours was 0 0 0 0).
public static void main(String[] args) throws IOException
{
PDDocument doc = PDDocument.load(....);
COSArray array = new COSArray();
array.add(COSName.SEPARATION);
array.add(COSName.getPDFName("decoupe"));
array.add(COSName.DEVICECMYK); // alternate color
COSDictionary fdict = new COSDictionary();
fdict.setInt(COSName.FUNCTION_TYPE, 2);
COSArray range = new COSArray();
range.add(COSInteger.get(0));
range.add(COSInteger.get(1));
range.add(COSInteger.get(0));
range.add(COSInteger.get(1));
range.add(COSInteger.get(0));
range.add(COSInteger.get(1));
range.add(COSInteger.get(0));
range.add(COSInteger.get(1));
COSArray domain = new COSArray();
domain.add(COSInteger.get(0));
domain.add(COSInteger.get(1));
COSArray c0 = new COSArray();
c0.add(COSFloat.get("0"));
c0.add(COSFloat.get("0"));
c0.add(COSFloat.get("0"));
c0.add(COSFloat.get("0"));
COSArray c1 = new COSArray();
c1.add(COSFloat.get("1"));
c1.add(COSFloat.get("1"));
c1.add(COSFloat.get("1"));
c1.add(COSFloat.get("1"));
fdict.setItem(COSName.DOMAIN, domain);
fdict.setItem(COSName.RANGE, range);
fdict.setItem(COSName.C0, c0);
fdict.setItem(COSName.C1, c1);
fdict.setInt(COSName.N, 1);
PDFunctionType2 func = new PDFunctionType2(fdict);
array.add(func); // tint transform
PDColorSpace spotColorSpace = new PDSeparation(array);
PDPage page = doc.getPage(0);
PDPageContentStream cs = new PDPageContentStream(doc, page, AppendMode.APPEND, true, true);
PDColor color = new PDColor(new float[]{0.5f}, spotColorSpace);
cs.setStrokingColor(color);
cs.setLineWidth(10);
cs.addRect(50, 50, 300, 300);
cs.stroke();
cs.close();
doc.save(...);
}
What I used as help: the source code of PDSeparation.java, and the CreateGradientShadingPDF.java example from the source code download, that one has a type 2 function that I could easily copy and modify.

Hbox location of GridPanels

I am Trying to have an HBox with two GridPanels, a bigger one in the left and a smaller one in the right (starting at the end of the right side the scene) however I cannot manage to get them in the positions I want to. This is how it looks right now. As you can see, the red GridPane is on top of the other one. The red panel should go all the wait to the right.
Moreover, I would like them to stay in the same position even if some buttons or text are added to either one of the GridPanes.
Here is my code:
//TEST BUTTONS PANEL
testButtonPane = new GridPane();
testButtonPane.setHgap(10);
testButtonPane.setVgap(9);
testButtonPane.setPadding(new Insets(140, 100, 0, 100));
questionLabel = new Label("Question:");
questionLabel.setPrefWidth(500.0);
questionLabel.setPrefHeight(50.0);
questionLabel.setStyle("-fx-font: 30 timesnewroman; -fx-base: #AE3522");
testButtonPane.add(questionLabel,1,1);
testButtonPane.add(backButton,1,10);
backButton = new Button("BACK");
backButton.setOnAction(e -> primaryStage.setScene(sceneCreateTest));
backButton.setPrefWidth(140.0);
backButton.setPrefHeight(50.0);
backButton.setStyle("-fx-font: 30 timesnewroman; -fx-base: #AE3522");
testButtonPane.add(backButton,1,10);
//SCORE PANEL
scoreButtonPane = new GridPane();
scoreButtonPane.setHgap(10);
scoreButtonPane.setVgap(9);
scoreButtonPane.setPadding(new Insets(10, 10, 0, 10));
scoreButtonPane.setStyle("-fx-background-color: #AE3522;-fx-border-color: black");
statusTitleLabel = new Label("STATUS");
statusTitleLabel.setPrefWidth(160.0);
statusTitleLabel.setPrefHeight(50.0);
statusTitleLabel.setStyle("-fx-font: 30 timesnewroman; -fx-text-fill:black ;");
scoreButtonPane.add(statusTitleLabel,1,1,2,1);
scoreLabel = new Label("Score: ");
scoreLabel.setPrefWidth(130.0);
scoreLabel.setPrefHeight(50.0);
scoreLabel.setStyle("-fx-font: 20 timesnewroman; -fx-text-fill:black ;");
scoreButtonPane.add(scoreLabel,1,2);
timerLabel = new Label("Time: ");
timerLabel.setPrefWidth(130.0);
timerLabel.setPrefHeight(50.0);
timerLabel.setStyle("-fx-font: 20 timesnewroman; -fx-text-fill:black ;");
scoreButtonPane.add(timerLabel,1,3);
QInLabel = new Label("Question: ");
QInLabel.setPrefWidth(130.0);
QInLabel.setPrefHeight(50.0);
QInLabel.setStyle("-fx-font: 20 timesnewroman; -fx-text-fill:black ;");
scoreButtonPane.add(QInLabel,1,4);
//Makes possible to have two panes in the same scene/layout
HBox hBoxTest = new HBox();
hBoxTest.setSpacing(10.0);
hBoxTest.setPadding(new Insets(140,10,50,10));
hBoxTest.getChildren().addAll(testButtonPane, scoreButtonPane);
The test buttons panel should go in the left and the score panel is the red one and should go in the right. I hope you can help me. Thank you so much in advance. =)

SWT drop cursor position on canvas

I'm making a simple "paint" application in JAVA. I would have wanted that when the person clicks the canvas, and make a drag and drop, a listener get the drop cursor location, but I don't find how make a drop listener. How can I find the location of the cursor when the user stop his click?
I have the following code for the drag :
Canvas paintC = new Canvas(shell, SWT.NONE);
paintC.addDragDetectListener(new DragDetectListener() {
public void dragDetected(DragDetectEvent arg0) {
Point controlRelativePos = new Point(arg0.x, arg0.y);
displayRelativePos1 = paintC.toDisplay(controlRelativePos);
GC gc = new GC(paintC);
gc.setBackground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));
gc.fillRectangle(arg0.x, arg0.y, 90, 60);
}
});
Should I a drag function in order to get the latest position?
Edit: I've tried this, but it didn't work :
dropTarget.addDropListener(new DropTargetAdapter() {
#Override
public void drop(DropTargetEvent event) {
displayRelativePos2 = dropTarget.getDisplay().getCursorLocation();
hauteur = displayRelativePos2.y - displayRelativePos1.y;
largeur = displayRelativePos2.x - displayRelativePos1.x;
GC gc = new GC(paintC);
gc.setBackground(SWTResourceManager.getColor(SWT.COLOR_RED));
gc.fillRectangle(displayRelativePos1.x, displayRelativePos1.y, largeur, hauteur);
nbFormesAff = nbFormes +1;
forme = "Rectangle" + nbFormesAff;
pos = displayRelativePos1.x + ", " + displayRelativePos1.y +"\nhauteur:" + hauteur +" largeur:"+ largeur;
}
DropTargetEvent has x and y fields which contain the Display relative location of the cursor.
Point displayRelativeDrop = new Point(event.x, event.y);
Your fillRectangle must use points which are relative to the Control (paintC) not the display. Use Control.toControl(point) to convert from display relative to control relative.
You should also not try to draw the control in the drop method. Just call redraw on the control and do the drawing in a paint listener.

java awt color chooser does't show in proper way in JavaFx

#FXML
Private void handleItemBackAction(ActionEvent eve)
{
java.awt.Color color=JColorChooser.showDialog(null,"Select a color",java.awt.Color.CYAN);
String hex = Integer.toHexString(color.getRGB() & 0xffffff);
hex="#"+hex;
Text.setText(hex);
ShortcutButton.setStyle("-fx-background-color: " + hex + ";");
}
When I run this window and click on button at first time color chooser goes behind my actual pane.
When I click on button second time while running it shows at top of all other pane which is correct and so on it works properly.
Then why color chooser not shows in front first time on button click?
The first argument to JColorChooser.showDialog is the parent component of the dialog. You told that method to show the dialog with no parent, so it doesn't know about your other windows.
Instead of using JColorChooser.showDialog, you'll need to embed a JColorChooser instance inside a JavaFX dialog or window:
JColorChooser colorChooser = new JColorChooser(java.awt.Color.CYAN);
SwingNode colorChooserNode = new SwingNode();
colorChooserNode.setContent(colorChooser);
Alert dialog = new Alert(Alert.AlertType.NONE);
// Guarantees dialog will be above (and will block input to) mainStage.
dialog.initOwner(mainStage);
dialog.setTitle("Select a color");
dialog.getDialogPane().setContent(colorChooserNode);
dialog.getDialogPane().getButtonTypes().setAll(
ButtonType.OK, ButtonType.CANCEL);
Optional<ButtonType> response = dialog.showAndWait();
if (response.filter(r -> r == ButtonType.OK).isPresent()) {
int rgb = colorChooser.getColor().getRGB();
String hex = String.format("#%06x", rgb & 0xffffff);
Text.setText(hex);
ShortcutButton.setBackground(new Background(
new BackgroundFill(Color.valueOf(hex), null, null)));
} else {
System.out.println("User canceled");
}
Of course, you're probably better off using ColorPicker in your main window, so you don't have to create an explicit dialog at all:
final ColorPicker colorPicker = new ColorPicker(Color.CYAN);
colorPicker.setOnAction(e -> {
Color color = colorPicker.getValue();
String hex = String.format("#%02x02x02x",
(int) (color.getRed() * 255),
(int) (color.getGreen() * 255),
(int) (color.getBlue() * 255));
Text.setText(hex);
ShortcutButton.setBackground(
new Background(new BackgroundFill(color, null, null)));
});
myLayoutPane.getChildren().add(colorPicker);
As an aside, Java variable names should always start with a lowercase letter, to make them easy to distinguish from class names. Consider changing Text to text, and ShortcutButton to shortcutButton.

How can I associate custom widgets with TreeItems in SWT?

I have created a custom widget--an SWT Group that consists of several buttons, labels, images and perhaps several other widgets I might need to add in the future. I would like to make this custom widget a tree item, so that I can get the indentation and the expand/collapse functionality of a tree.
Here is an image that shows what I am trying to achieve:
I created this example with the Google Web Toolkit and I'd like to implement it with SWT.
The SWT TreeItem has methods to set the text and the image, but I could not find a way to make the tree item be a custom widget. If it is not possible to associate custom widgets with SWT tree items, suggestions about other ways to organize custom widgets in a tree with indentation and expand/collapse functionality would be very helpful too. Thanks!
The PGroup widget from the Eclipse Nebula project does the job. It lets you enclose SWT Composites and supports collapsing and expanding. Indentation can be achieved by using layouts (e.g., GridLayout) for the contents of the PGroup and layout data (e.g., GridData) to specify the indentation of a component inside the PGroup.
The ExpandBar (which is a standard SWT widget) also seems like a possible solution, but I have not experimented with it.
It seems to be possible to use an event SWT.PaintItem to add custom drawing:
https://www.eclipse.org/articles/Article-CustomDrawingTableAndTreeItems/customDraw.htm
Source:
Embed custom widget in SWT Tree or Table
1 Display display = new Display();
2 Shell shell = new Shell(display);
3 shell.setBounds(10, 10, 350, 200);
4 Image xImage = new Image (display, 16, 16);
5 GC gc = new GC(xImage);
6 gc.setForeground(display.getSystemColor(SWT.COLOR_RED));
7 gc.drawLine(1, 1, 14, 14);
8 gc.drawLine(1, 14, 14, 1);
9 gc.drawOval(2, 2, 11, 11);
10 gc.dispose();
11 final int IMAGE_MARGIN = 2;
12 final Tree tree = new Tree(shell, SWT.CHECK);
13 tree.setBounds(10, 10, 300, 150);
14 TreeItem item = new TreeItem(tree, SWT.NONE);
15 item.setText("root item");
16 for (int i = 0; i < 4; i++) {
17 TreeItem newItem = new TreeItem(item, SWT.NONE);
18 newItem.setText("descendent " + i);
19 if (i % 2 == 0) newItem.setData(xImage);
20 item.setExpanded(true);
21 item = newItem;
22 }
23 tree.addListener(SWT.MeasureItem, new Listener() {
24 public void handleEvent(Event event) {
25 TreeItem item = (TreeItem)event.item;
26 Image trailingImage = (Image)item.getData();
27 if (trailingImage != null) {
28 event.width += trailingImage.getBounds().width + IMAGE_MARGIN;
29 }
30 }
31 });
32 tree.addListener(SWT.PaintItem, new Listener() {
33 public void handleEvent(Event event) {
34 TreeItem item = (TreeItem)event.item;
35 Image trailingImage = (Image)item.getData();
36 if (trailingImage != null) {
37 int x = event.x + event.width + IMAGE_MARGIN;
38 int itemHeight = tree.getItemHeight();
39 int imageHeight = trailingImage.getBounds().height;
40 int y = event.y + (itemHeight - imageHeight) / 2;
41 event.gc.drawImage(trailingImage, x, y);
42 }
43 }
44 });
45 shell.open();
46 while (!shell.isDisposed()) {
47 if (!display.readAndDispatch()) display.sleep();
48 }
49 xImage.dispose();
50 display.dispose();

Categories