如何在javafx中显示图像的特定部分
如何在javafx中显示图像的特定部分,java,javafx,Java,Javafx,我有这张图片(所有这些效果都在一个.png文件中),我想显示第二张图片,例如,如何使用javafx中的Image和ImageView来显示此图像的特定部分?谢谢您可以利用节点的clip属性,以及图像视图的x属性。下面是一个演示,显示了时间轴中的图像部分,如gif动画图片: @Override public void start(Stage stage) { Group root = new Group(); Image image = new Image(this.getCla
我有这张图片(所有这些效果都在一个.png文件中),我想显示第二张图片,例如,如何使用javafx中的Image和ImageView来显示此图像的特定部分?谢谢您可以利用
节点的clip属性
,以及图像视图的x属性
。下面是一个演示,显示了时间轴中的图像部分,如gif动画图片:
@Override
public void start(Stage stage) {
Group root = new Group();
Image image = new Image(this.getClass().getResource("your.png").toExternalForm());
final int numberOfFrames = 6; // in image
double frameWidth = image.getWidth() / numberOfFrames;
Scene scene = new Scene(root, frameWidth, image.getHeight());
final ImageView view = new ImageView(image);
Rectangle mask = new Rectangle(frameWidth, image.getHeight());
view.setClip(mask);
Timeline timeline = new Timeline();
for (int i = 0; i <= numberOfFrames; i++) {
KeyFrame kf = new KeyFrame(Duration.seconds(i), new KeyValue(view.xProperty(), -frameWidth * i, Interpolator.DISCRETE));
timeline.getKeyFrames().add(kf);
}
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
root.getChildren().add(view);
stage.setScene(scene);
stage.show();
}
@覆盖
公众假期开始(阶段){
组根=新组();
Image Image=新图像(this.getClass().getResource(“your.png”).toExternalForm());
最终int numberOfFrames=6;//在图像中
double frameWidth=image.getWidth()/numberOfFrames;
场景=新场景(根、帧宽、image.getHeight());
最终图像视图=新图像视图(图像);
矩形掩码=新矩形(frameWidth,image.getHeight());
视图.setClip(掩码);
时间线=新时间线();
对于(inti=0;i这个答案太过分了。但是有了一组像你在问题中看到的那样好的图像,可能就是所谓的过分了:-)
基本设计与Uluk的相同,它只是调整ImageView的视口,而不是设置剪辑,但概念是相同的
小心=>Java8
导入javafx.animation.*;
导入javafx.application.application;
导入javafx.beans.property.*;
导入javafx.event.*;
导入javafx.geometry.Rectangle2D;
导入javafx.scene.scene;
导入javafx.scene.control.control;
导入javafx.scene.effect.*;
导入javafx.scene.image.*;
导入javafx.scene.layout.*;
导入javafx.scene.paint.Color;
导入javafx.stage.stage;
导入javafx.util.Duration;
类ExploAdableMageView扩展了ImageView{
私人最终矩形2D[]单元剪辑;
私有整数单元;
专用最终持续时间帧\时间=持续时间。秒(.5);
公共ExploadableImageView(图像explosionImage,int numCells){
this.numCells=numCells;
double cellWidth=explosionImage.getWidth()/numCells;
double cellHeight=explosionImage.getHeight();
cellClips=新矩形2D[numCells];
对于(int i=0;i
导入javafx.animation.*;
导入javafx.application.application;
导入javafx.beans.property.*;
导入javafx.geometry.*;
导入javafx.scene.scene;
导入javafx.scene.control.ToggleButton;
导入javafx.scene.image.*;
导入javafx.scene.layout.VBox;
导入javafx.stage.stage;
导入javafx.util.Duration;
类Sprite扩展了ImageView{
私人最终矩形2D[]单元剪辑;
私有整数单元;
私人最终时间表;
私有最终IntegerProperty frameCounter=新的SimpleIntegerProperty(0);
公共精灵(图像动画图像、整数单元格、持续时间帧时间){
this.numCells=numCells;
double cellWidth=animationImage.getWidth()/numCells;
double cellHeight=animationImage.getHeight();
cellClips=新矩形2D[numCells];
对于(int i=0;i{
frameCounter.set((frameCounter.get()+1)%numCells);
setViewport(cellClips[frameCounter.get()]);
})
);
}
公共空间playOnce(){
frameCounter.set(0);
timeline.setCycleCount(numCells);
timeline.stop();
timeline.playFromStart();
}
公众假期{
frameCounter.set(0);
timeline.setCycleCount(timeline.unfinite);
timeline.stop();
timeline.playFromStart();
}
公共停车场(){
frameCounter.set(0);
setViewport(cellClips[frameCounter.get()]);
timeline.stop();
}
}
公共类SpriteSample扩展了应用程序{
私有静态最终int NUM_CELLS_PER_ANIMATION=6;
私有静态最终持续时间帧\时间=持续时间。秒(.5);
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
公众假期开始(阶段){
//看起来imgur可能阻止了从Java应用程序直接访问以下png(不知何故)。
//但您仍然可以从该位置下载QMqbQ.png
//并将其本地保存在与CatWhack程序相同的目录中
//然后将新图像调用替换为:
//新图像(Sprite.class.getResourceAsStream(“QMqbQ.png”);
Image tilesheetImage=新图像(SpriteSample.class.getResourceAsStream(“QMqbQ.png”);
精灵精灵=新精灵(tilesheetImage、每个动画的单元格数、帧时间);
ToggleButton animationControl=新的ToggleButton(“动画”);
animationControl.setOnAction(事件->{
if(animationControl.isSelected()){
animationControl.setText(“停止”);
sprite.playing();
}否则{
设置文本(“动画”);
雪碧。停止();
}
});
VBox布局=新的VBox(10,精灵,动画控件);
布局。设置填充(新插图(10));
布局。设置对齐(位置中心);
舞台场景(新场景(l)
import javafx.animation.*;
import javafx.application.Application;
import javafx.beans.property.*;
import javafx.event.*;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Control;
import javafx.scene.effect.*;
import javafx.scene.image.*;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;
class ExploadableImageView extends ImageView {
private final Rectangle2D[] cellClips;
private int numCells;
private final Duration FRAME_TIME = Duration.seconds(.5);
public ExploadableImageView(Image explosionImage, int numCells) {
this.numCells = numCells;
double cellWidth = explosionImage.getWidth() / numCells;
double cellHeight = explosionImage.getHeight();
cellClips = new Rectangle2D[numCells];
for (int i = 0; i < numCells; i++) {
cellClips[i] = new Rectangle2D(
i * cellWidth, 0,
cellWidth, cellHeight
);
}
setImage(explosionImage);
setViewport(cellClips[0]);
}
public void explode(EventHandler<ActionEvent> onFinished) {
final IntegerProperty frameCounter = new SimpleIntegerProperty(0);
Timeline kaboom = new Timeline(
new KeyFrame(FRAME_TIME, event -> {
frameCounter.set((frameCounter.get() + 1) % numCells);
setViewport(cellClips[frameCounter.get()]);
})
);
kaboom.setCycleCount(numCells);
kaboom.setOnFinished(onFinished);
kaboom.play();
}
}
class ExplodableItem extends StackPane {
public ExplodableItem(Image objectImage, Image explosionImage, int numCells) {
ImageView objectView = new ImageView(objectImage);
ExploadableImageView explosionView = new ExploadableImageView(
explosionImage, numCells
);
setMinSize(
Math.max(
objectImage.getWidth(),
explosionView.getViewport().getWidth()
),
Math.max(
objectImage.getHeight(),
explosionView.getViewport().getHeight()
)
);
objectView.setPickOnBounds(false);
objectView.setOnMouseClicked(event -> {
getChildren().setAll(explosionView);
explosionView.explode(complete -> getChildren().setAll(objectView));
});
DropShadow drop = new DropShadow(10, Color.GOLD);
drop.setInput(new Glow());
objectView.setOnMouseEntered(event -> objectView.setEffect(drop));
objectView.setOnMouseExited(event -> objectView.setEffect(null));
getChildren().setAll(objectView);
}
}
public class CatWhack extends Application {
public static void main(String[] args) {
launch(args);
}
private static final int NUM_CELLS_PER_EXPLOSION = 6;
@Override
public void start(Stage stage) {
Image objectImage = new Image("http://icons.iconarchive.com/icons/iconka/meow/96/cat-box-icon.png"); // cat icon linkware: backlink to http://www.iconka.com required
// looks likes imgur may have blocked direct access to following png from a Java app (somehow).
// but you can still download the QMqbQ.png from that location
// and save it locally in the same directory as the CatWhack program
// then pick it up by replacing the new Image call with:
// new Image(CatWhack.class.getResourceAsStream("QMqbQ.png"));
Image explosionImage = new Image("http://i.stack.imgur.com/QMqbQ.png");
TilePane tiles = new TilePane();
tiles.setPrefColumns(4);
for (int i = 0; i <16; i++) {
tiles.getChildren().add(
new ExplodableItem(objectImage, explosionImage, NUM_CELLS_PER_EXPLOSION)
);
}
tiles.setMinSize(Control.USE_PREF_SIZE, Control.USE_PREF_SIZE);
stage.setTitle("Cat Whack - Click a cat to whack it!");
stage.setScene(new Scene(tiles));
stage.show();
}
}
import javafx.animation.*;
import javafx.application.Application;
import javafx.beans.property.*;
import javafx.geometry.*;
import javafx.scene.Scene;
import javafx.scene.control.ToggleButton;
import javafx.scene.image.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;
class Sprite extends ImageView {
private final Rectangle2D[] cellClips;
private int numCells;
private final Timeline timeline;
private final IntegerProperty frameCounter = new SimpleIntegerProperty(0);
public Sprite(Image animationImage, int numCells, Duration frameTime) {
this.numCells = numCells;
double cellWidth = animationImage.getWidth() / numCells;
double cellHeight = animationImage.getHeight();
cellClips = new Rectangle2D[numCells];
for (int i = 0; i < numCells; i++) {
cellClips[i] = new Rectangle2D(
i * cellWidth, 0,
cellWidth, cellHeight
);
}
setImage(animationImage);
setViewport(cellClips[0]);
timeline = new Timeline(
new KeyFrame(frameTime, event -> {
frameCounter.set((frameCounter.get() + 1) % numCells);
setViewport(cellClips[frameCounter.get()]);
})
);
}
public void playOnce() {
frameCounter.set(0);
timeline.setCycleCount(numCells);
timeline.stop();
timeline.playFromStart();
}
public void playContinuously() {
frameCounter.set(0);
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.stop();
timeline.playFromStart();
}
public void stop() {
frameCounter.set(0);
setViewport(cellClips[frameCounter.get()]);
timeline.stop();
}
}
public class SpriteSample extends Application {
private static final int NUM_CELLS_PER_ANIMATION = 6;
private static final Duration FRAME_TIME = Duration.seconds(.5);
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
// looks likes imgur may have blocked direct access to following png from a Java app (somehow).
// but you can still download the QMqbQ.png from that location
// and save it locally in the same directory as the CatWhack program
// then pick it up by replacing the new Image call with:
// new Image(Sprite.class.getResourceAsStream("QMqbQ.png"));
Image tilesheetImage = new Image(SpriteSample.class.getResourceAsStream("QMqbQ.png"));
Sprite sprite = new Sprite(tilesheetImage, NUM_CELLS_PER_ANIMATION, FRAME_TIME);
ToggleButton animationControl = new ToggleButton("Animate");
animationControl.setOnAction(event -> {
if (animationControl.isSelected()) {
animationControl.setText("Stop");
sprite.playContinuously();
} else {
animationControl.setText("Animate");
sprite.stop();
}
});
VBox layout = new VBox(10, sprite, animationControl);
layout.setPadding(new Insets(10));
layout.setAlignment(Pos.CENTER);
stage.setScene(new Scene(layout));
stage.show();
}
}
package com.pakzaban;
import javafx.fxml.FXML;
import javafx.geometry.Rectangle2D;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
public class Controller {
@FXML
public ImageView imageView;
public TextField numberField;
private final int IMAGE_WIDTH = 50;
private final int IMAGE_HEIGHT = 80;
private int imageNumber = 1;
public void onSetImagePressed(){
try {
imageNumber = Integer.parseInt(numberField.getText());
//SET THE WHOLE IMAGE INTO IMAGEVIEW
Image wholeImage = new Image("com/pakzaban/wholePicture.png");
imageView.setImage(wholeImage);
//SET THE VIEWPORT TO DESIRED PART OF THE IMAGE
Rectangle2D imagePart = new Rectangle2D((imageNumber - 1) * IMAGE_WIDTH, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
imageView.setViewport(imagePart);
}
catch (Exception e){
e.printStackTrace();
}
}
}