JavaFX realize the image 3D Rollover Effect Method Example _java

Source: Internet
Author: User
Tags addall cos sin

Implementation steps: 1, define the Flipview object. Contains the following properties:

Copy Code code as follows:

Front view
Public Node Frontnode;
Negative view
Public Node Backnode;
Whether to flip
Boolean flipped = false;
Flip Angle
Doubleproperty time = new Simpledoubleproperty (MATH.PI/2);
Positive Flip Effect
Perspectivetransform fronteffect = new Perspectivetransform ();
Reverse Flip effect
Perspectivetransform backeffect = new Perspectivetransform ();

The Create method returns what needs to be displayed:

Copy Code code as follows:

private void Create () {
Time.addlistener (New ChangeListener () {
@Override
public void changed (OBSERVABLEVALUE<? extends Number> arg0,
Number arg1, number arg2) {
SETPT (Fronteffect, Time.get ());
SETPT (Backeffect, Time.get ());
}
});
Anim.getkeyframes (). AddAll (Frame1, frame2);
Backnode.visibleproperty (). Bind (
Bindings.when (Time.lessthan (0)). Then (true). otherwise (false);

Frontnode.visibleproperty (). Bind (
Bindings.when (Time.lessthan (0)). Then (false). otherwise (true);
SETPT (Fronteffect, Time.get ());
SETPT (Backeffect, Time.get ());
Frontnode.seteffect (Fronteffect);
Backnode.seteffect (Backeffect);
GetChildren (). AddAll (Backnode, Frontnode);
}

The above code needs to be noted: As the time value changes, the values of Fronteffect and Backeffect will change as well. 2, the implementation of the Perspectivetransform effect using the Math.sin () and Math.Cos () method to simulate the 3D angle transform. Specifically implemented as follows:
Copy Code code as follows:

private void Setpt (Perspectivetransform pt, double t) {
Double width = 200;
Double height = 200;
Double radius = WIDTH/2;
Double back = HEIGHT/10;
Pt.setulx (Radius-math.sin (t) * radius);
Pt.setuly (0-math.cos (t) * back);
Pt.seturx (radius + math.sin (t) * radius);
Pt.setury (0 + math.cos (t) * back);
Pt.setlrx (radius + math.sin (t) * radius);
Pt.setlry (Height-math.cos (t) * back);
PT.SETLLX (Radius-math.sin (t) * radius);
pt.setlly (height + math.cos (t) * back);
}

3, the angle transformation in 1 seconds time from 3.14/2 to -3.14/2.
Copy Code code as follows:

Keyframe frame1 = new Keyframe (Duration.zero, New KeyValue (Time,
MATH.PI/2, interpolator.linear));
Keyframe frame2 = new Keyframe (duration.seconds (1),
New EventHandler () {
@Override
public void handle (ActionEvent event) {
flipped =!flipped;
}
}, new KeyValue (Time,-MATH.PI/2, interpolator.linear));

4, Flipview object creation: Through the constructor can be very convenient to create Flipview objects.

Copy Code code as follows:

ImageView image1 = new ImageView (New Image (GetClass ()
. getResourceAsStream ("Lion1.png"));
ImageView image2 = new ImageView (New Image (GetClass ()
. getResourceAsStream ("Lion2.png"));
Flipview flip = new Flipview (Image1, image2);

5, the effect chart:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.