问题
您可以向检测鼠标在其上移动的节点添加事件侦听器.如果在移动节点之前按下鼠标按钮,则不起作用.
题
有人知道如何在按下按钮时检测鼠标移动吗?到目前为止,我只是通过使用MOUSE_DRAGGED事件找到了解决方案,然后使用getPickResult()而不是使用getSource()并评估PickResult数据.
这是代码,包括Uluk的解决方案.旧的和新的解决方案可通过useNewVersion(Uluk的版本)boolean切换:
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.PickResult;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
boolean useNewVersion= true;
int rows = 10;
int columns = 20;
double width = 1024;
double height = 768;
@Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
// create grid
Grid grid = new Grid( columns, rows, width, …Run Code Online (Sandbox Code Playgroud) 我有一个JavaFX ContextMenu分配给鼠标右键单击滚动窗格.它会打开,但是当您在滚动窗格外部单击时它不会关闭.我可以在滚动窗格中添加另一个鼠标事件以隐藏它,但这只能解决1个问题.主要问题是,当我单击滚动窗格的任何组件时,上下文菜单仍保持打开状态.
示例:单击鼠标右键打开弹出窗口,然后单击按钮.弹出菜单仍处于打开状态.
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ScrollPane;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
final ContextMenu contextMenu = new ContextMenu();
MenuItem item1 = new MenuItem("About");
MenuItem item2 = new MenuItem("Preferences");
contextMenu.getItems().addAll(item1, item2);
Rectangle rect = new Rectangle( 100,100,150,150);
Button button = new Button( "Button Text");
// create …Run Code Online (Sandbox Code Playgroud) 问题
TableView的setTableMenuButtonVisible提供了一种更改表列可见性的机制.但是,该功能还有很多不足之处:
菜单应保持打开状态.我有15个表格列,单击菜单打开很痛苦 - >单击列 - >单击菜单打开 - >单击下一列 - > ...更改多列的可见性很痛苦
应该选择全部/取消选择所有功能
应该有一种方法来扩展菜单与自定义项目
取消选择所有列后,无法重新选择列,因为标题已消失,并且表格菜单已经消失
换句话说:表菜单的当前实现是相当无用的.
题
有没有人知道如何用适当的方式替换现有的tableview菜单?我见过一个带有".show-hide-columns-button"样式查找并添加事件过滤器的解决方案.然而那是2年前,也许事情发生了变化.
非常感谢你!
这就是我想要的方式,通过ContextMenu演示(即点击桌面上的鼠标右键):
public class TableViewSample extends Application {
private final TableView table = new TableView();
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
Scene scene = new Scene(new Group());
stage.setTitle("Table View Sample");
stage.setWidth(300);
stage.setHeight(500);
// create table columns
TableColumn firstNameCol = new TableColumn("First Name");
TableColumn lastNameCol = new TableColumn("Last Name");
TableColumn emailCol = new TableColumn("Email");
table.getColumns().addAll(firstNameCol, …Run Code Online (Sandbox Code Playgroud) 问题
处理表格时最基本的需求之一是复制/粘贴表格单元格的数据。JavaFX TableView 不支持开箱即用。
题
您如何访问表格单元格而不是数据对象,以便将剪贴板数据粘贴到选定的单元格中?
代码
这是我到目前为止所得到的:
表实用程序
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.control.TablePosition;
import javafx.scene.control.TableView;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.KeyEvent;
public class TableUtils {
/**
* Install the keyboard handler:
* + CTRL + C = copy to clipboard
* + CTRL + V = paste to clipboard
* @param table
*/
public static void installCopyPasteHandler(TableView<?> table) {
// install copy/paste keyboard handler
table.setOnKeyPressed(new TableKeyEventHandler());
}
/** …Run Code Online (Sandbox Code Playgroud) 情况
我有一个圆形的图像,其中填充了径向渐变,范围从白色到黑色/透明度.它作为一个粒子,需要在其生命周期中着色.
问题
我正在使用ColorAdjust为图像应用不同的颜色.问题是颜色不是我想要的颜色.如果我使用GREEN作为目标颜色,我会得到一个粉红色的球.
题
如何计算ColorAdjust的色调值以匹配给定的目标颜色?或者有更好的方法来着色图像?我无法使用形状本身,因为使用ImageView比使用形状更快.
码
这是代码.问题很可能是色调只是当前颜色的差异:
import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.RadialGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
HBox root = new HBox();
// create alpha masked image
Image image = createImage(createAlphaMaskedBall(200));
// create original imageview
ImageView original = new ImageView( image);
// create imageview with color adjustment …Run Code Online (Sandbox Code Playgroud) 任务
我想从头开始用JavaFX创建一个甘特图.
例
假设我有2台机器A和机器B,它们有2种状态在线(绿色)和离线(红色).我想以给定的时间间隔在水平彩色条中显示它们的状态,X轴是日期轴,Y轴是机器轴.
题
我从哪里开始?我需要使用哪些课程?如果有人有一个最小的例子并且可以分享它会很棒.
非常感谢你.
情况
我使用以下技术使用JavaFX创建了粒子系统:
每个粒子都是一个ImageView,其中包含一个具有径向渐变的Image:
粒子处理循环是一个AnimationTimer,其中粒子列表是通过列表的stream()。parallel()方法处理的,它实际上使整个系统得到了提升。像这样:
loop = new AnimationTimer() {
@Override
public void handle(long now) {
addParticle();
// apply force: gravity
allParticles.stream().parallel().forEach(Particle::applyForceGravity);
// move particle
allParticles.stream().parallel().forEach(Particle::move);
// update position in fx scene
allParticles.forEach(Particle::display);
// remove all particles that aren't visible anymore
removeDeadParticles();
}
};
Run Code Online (Sandbox Code Playgroud)
粒子的颜色在其生命周期内通过ColorAdjust进行更改。我使用火色进行测试,其中包含1700个粒子:
我学到了什么:
题
在性能方面,是否有更好的方法在JavaFX中实现粒子系统(其他Node类型,Thread等)?
如果有人想玩弄,我可以发布一些代码。
非常感谢您的专业知识!
编辑:由于被问到,您可以从此要点中获得完整的代码,该代码将节点用作带有色彩调整的粒子。顺便说一句,即使您预先渲染图像并且不使用色彩调节,性能仍然很低。
但是,这个问题更多是理论上的问题,因此没有必要深入研究代码。
如何在JavaFX 2.0演示中创建视频墙:
https://www.youtube.com/watch?v=UXSmJYFrulY#t=411
首先,它不一定是视频,也可以是图像.我想要的就是将节点放置在视频中,即像圆柱体或球体内部一样的弯曲形状.
或者是某个地方提供该演示的来源?
非常感谢你.
我有一个渐变,例如绿色到红色,范围从0到100。我需要从该渐变中查找任何给定值的颜色。目前,我正在画布上绘制线条,填充线条,拍摄快照并使用pixelreader来获取颜色。有谁知道更好的方法?对我来说似乎太过分了。
代码的简单版本:
private Color getColor( double value) {
Canvas canvas = new Canvas(100, 1);
GraphicsContext gc = canvas.getGraphicsContext2D();
Stop[] stops = new Stop[] { new Stop(0, Color.GREEN), new Stop(1, Color.RED)};
LinearGradient linearGradient = new LinearGradient(0, 0, 1, 0, true, CycleMethod.NO_CYCLE, stops);
gc.setFill(linearGradient);
gc.rect( 0, 0, canvas.getWidth(), canvas.getHeight());
gc.fill();
WritableImage image = new WritableImage((int) canvas.getWidth(), (int) canvas.getHeight());
image = canvas.snapshot(null, image);
PixelReader imageReader = image.getPixelReader();
Color imageColor = imageReader.getColor( (int) value, 0);
}
Run Code Online (Sandbox Code Playgroud)
非常感谢你!
问题
我想将漫反射贴图应用于 MeshView。当我将带有漫反射贴图的材质应用到 MeshView 时,它不可见。然而,应用于 Box 的相同材质是可见的。
题
我需要做什么才能将漫反射贴图应用到 MeshView?
代码
该代码生成带有随机噪声的图像。该图像用作 PhongMaterial 中的漫反射贴图。显示图像,其上方是应用了材质的框,框上方是应用了材质的 MeshView(金字塔)。该材料在金字塔上不可见。您可以使用鼠标拖动进行旋转。
import java.util.Random;
import javafx.application.Application;
import javafx.geometry.Point3D;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.SceneAntialiasing;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.shape.DrawMode;
import javafx.scene.shape.MeshView;
import javafx.scene.shape.TriangleMesh;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
public class Test extends Application {
private double mousePosX, mousePosY;
private double mouseOldX, mouseOldY;
private final Rotate rotateX = new Rotate(20, Rotate.X_AXIS);
private final Rotate …Run Code Online (Sandbox Code Playgroud) 我想创建一个网格作为JavaFX应用程序的背景。我当前的解决方案是在画布上绘制一个矩形,从该矩形创建图像图案并将其设置为填充。
问题:是否有更好的方法(最好通过CSS)来解决此问题?
当前版本:
public class BackgroundGrid extends Application {
double gridSize = 20;
@Override
public void start(Stage primaryStage) {
Scene scene = new Scene(new Group(), 800, 600);
primaryStage.setScene(scene);
primaryStage.show();
scene.setFill(createGridPattern());
}
public ImagePattern createGridPattern() {
double w = gridSize;
double h = gridSize;
Canvas canvas = new Canvas(w, h);
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setStroke(Color.BLACK);
gc.setFill(Color.LIGHTGRAY.deriveColor(1, 1, 1, 0.2));
gc.fillRect(0, 0, w, h);
gc.strokeRect(0, 0, w, h);
Image image = canvas.snapshot(new SnapshotParameters(), null);
ImagePattern pattern = new ImagePattern(image, 0, 0, …Run Code Online (Sandbox Code Playgroud) javafx ×11
javafx-3d ×2
tableview ×2
background ×1
contextmenu ×1
css ×1
gantt-chart ×1
gradient ×1
javafx-8 ×1
layout ×1
menu ×1
mouseevent ×1
particles ×1