Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

135
LINES

< > BotCompany Repo | #1004511 // Swing VSync Test [by Dmitri Trembovetski]

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Uses 486K of libraries. Click here for Pure Java version (161L/2K/6K).

1  
!752
2  
3  
lib 1004512 // scenario
4  
5  
import com.sun.scenario.animation.Clip;
6  
import java.awt.BorderLayout;
7  
import java.awt.Color;
8  
import java.awt.Container;
9  
import java.awt.Dimension;
10  
import java.awt.Graphics;
11  
import java.awt.Graphics2D;
12  
import java.awt.GridLayout;
13  
import java.awt.Rectangle;
14  
import java.awt.RenderingHints;
15  
import java.lang.reflect.Method;
16  
import javax.swing.JButton;
17  
import javax.swing.JComboBox;
18  
import javax.swing.JFrame;
19  
import javax.swing.JPanel;
20  
import javax.swing.SwingUtilities;
21  
22  
p {
23  
  VsyncSwingTest.main(args);
24  
}
25  
26  
/**
27  
 * @author Dmitri Trembovetski
28  
 */
29  
sclass VsyncSwingTest extends JPanel {
30  
    volatile int translateX;
31  
    private static boolean doRepaint;
32  
    static boolean doVsync = true;
33  
34  
    public int getTranslateX() {
35  
        return translateX;
36  
    }
37  
38  
    public void setTranslateX(int translateX) {
39  
        int curX = this.translateX;
40  
        this.translateX = translateX;
41  
        // getting fancy here, repaint() works just as well
42  
        if (doRepaint) {
43  
            repaint(50+Math.min(curX, translateX), 50, 100+Math.abs(curX - translateX), 300);
44  
        } else {
45  
            paintImmediately(50+Math.min(curX, translateX), 50, 100+Math.abs(curX - translateX), 300);
46  
        }
47  
    }
48  
49  
    @Override
50  
    protected void paintComponent(Graphics g) {
51  
        g.setColor(getBackground());
52  
        Rectangle r = g.getClipBounds();
53  
        g.fillRect(r.x, r.y, r.width, r.height);
54  
        ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
55  
                                         RenderingHints.VALUE_ANTIALIAS_ON);
56  
        g.setColor(Color.black);
57  
        g.fillRect(50+translateX, 50, 100, 300);
58  
    }
59  
60  
    public VsyncSwingTest() {
61  
        setBackground(Color.WHITE);
62  
        setPreferredSize(new Dimension(600, 400));
63  
64  
        Clip c = Clip.create(1000, Clip.INDEFINITE, this, "translateX", 0, 400);
65  
        c.start();
66  
    }
67  
    
68  
    
69  
    static void setVsyncRequested(JFrame f, boolean b) {
70  
        try {
71  
            Class<?> tmpClass =
72  
                Class.forName("com.sun.java.swing.SwingUtilities3");
73  
            Method tmpMethod = 
74  
                tmpClass.getMethod("setVsyncRequested",
75  
                                   Container.class, boolean.class);
76  
            tmpMethod.invoke(tmpClass, f, Boolean.valueOf(b));
77  
        } catch (Throwable ignore) {
78  
            System.err.println("Warning: Error while requesting vsync: "+ignore);
79  
        }
80  
    }
81  
82  
    public static void main(String[] args) {
83  
        int instances = 1;
84  
        for (String arg : args) {
85  
            if (arg.equalsIgnoreCase("-repaint")) {
86  
                doRepaint = true;
87  
            } else if (arg.equals("+i")) {
88  
                instances++;
89  
            } else if (arg.equals("-novsync")) {
90  
                doVsync = false;
91  
            }
92  
        }
93  
94  
        if (doRepaint) {
95  
            System.err.println("Using repaint()");
96  
        } else {
97  
            System.err.println("Using paintImmediately()");
98  
        }
99  
100  
        if (doVsync) {
101  
            System.err.println("Requesting VSYNC");
102  
        } else {
103  
            System.err.println("Not requesting VSYNC");
104  
        }
105  
106  
        final int insts = instances;
107  
        SwingUtilities.invokeLater(new Runnable() {
108  
            public void run() {
109  
                JFrame frame = new JFrame();
110  
//		frame.setUndecorated(true);
111  
                JPanel p = new JPanel();
112  
                p.setLayout(new GridLayout(insts, 1));
113  
                VsyncSwingTest test;
114  
                for (int i = 0; i < insts; i++) {
115  
                    test = new VsyncSwingTest();
116  
                    p.add(test);
117  
                }
118  
                frame.add(BorderLayout.CENTER, p);
119  
                String[] arr = { "So How's the responsiveness?",
120  
                    "two", "three", "four", "five", "six", "seven", "eight",
121  
                    "1", "2", "3", "4", "5", "6", "7", "8"
122  
                };
123  
                frame.add(BorderLayout.NORTH, new JComboBox(arr));
124  
                frame.add(BorderLayout.SOUTH, new JButton("some button"));
125  
126  
                frame.pack();
127  
                setVsyncRequested(frame, doVsync);
128  
129  
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
130  
                frame.setLocationRelativeTo(null);
131  
                frame.setVisible(true);
132  
            }
133  
        });
134  
    }
135  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, sawdedvomwva, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1004511
Snippet name: Swing VSync Test [by Dmitri Trembovetski]
Eternal ID of this version: #1004511/1
Text MD5: ace625bac2969c49d8ea4c7e3b9e45be
Transpilation MD5: b1204a4c44678a399b9eb6f7c304daea
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-08-18 23:27:31
Source code size: 4411 bytes / 135 lines
Pitched / IR pitched: No / No
Views / Downloads: 1457 / 1545
Referenced in: [show references]