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).

!752

lib 1004512 // scenario

import com.sun.scenario.animation.Clip;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.lang.reflect.Method;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

p {
  VsyncSwingTest.main(args);
}

/**
 * @author Dmitri Trembovetski
 */
sclass VsyncSwingTest extends JPanel {
    volatile int translateX;
    private static boolean doRepaint;
    static boolean doVsync = true;

    public int getTranslateX() {
        return translateX;
    }

    public void setTranslateX(int translateX) {
        int curX = this.translateX;
        this.translateX = translateX;
        // getting fancy here, repaint() works just as well
        if (doRepaint) {
            repaint(50+Math.min(curX, translateX), 50, 100+Math.abs(curX - translateX), 300);
        } else {
            paintImmediately(50+Math.min(curX, translateX), 50, 100+Math.abs(curX - translateX), 300);
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        g.setColor(getBackground());
        Rectangle r = g.getClipBounds();
        g.fillRect(r.x, r.y, r.width, r.height);
        ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                         RenderingHints.VALUE_ANTIALIAS_ON);
        g.setColor(Color.black);
        g.fillRect(50+translateX, 50, 100, 300);
    }

    public VsyncSwingTest() {
        setBackground(Color.WHITE);
        setPreferredSize(new Dimension(600, 400));

        Clip c = Clip.create(1000, Clip.INDEFINITE, this, "translateX", 0, 400);
        c.start();
    }
    
    
    static void setVsyncRequested(JFrame f, boolean b) {
        try {
            Class<?> tmpClass =
                Class.forName("com.sun.java.swing.SwingUtilities3");
            Method tmpMethod = 
                tmpClass.getMethod("setVsyncRequested",
                                   Container.class, boolean.class);
            tmpMethod.invoke(tmpClass, f, Boolean.valueOf(b));
        } catch (Throwable ignore) {
            System.err.println("Warning: Error while requesting vsync: "+ignore);
        }
    }

    public static void main(String[] args) {
        int instances = 1;
        for (String arg : args) {
            if (arg.equalsIgnoreCase("-repaint")) {
                doRepaint = true;
            } else if (arg.equals("+i")) {
                instances++;
            } else if (arg.equals("-novsync")) {
                doVsync = false;
            }
        }

        if (doRepaint) {
            System.err.println("Using repaint()");
        } else {
            System.err.println("Using paintImmediately()");
        }

        if (doVsync) {
            System.err.println("Requesting VSYNC");
        } else {
            System.err.println("Not requesting VSYNC");
        }

        final int insts = instances;
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
//		frame.setUndecorated(true);
                JPanel p = new JPanel();
                p.setLayout(new GridLayout(insts, 1));
                VsyncSwingTest test;
                for (int i = 0; i < insts; i++) {
                    test = new VsyncSwingTest();
                    p.add(test);
                }
                frame.add(BorderLayout.CENTER, p);
                String[] arr = { "So How's the responsiveness?",
                    "two", "three", "four", "five", "six", "seven", "eight",
                    "1", "2", "3", "4", "5", "6", "7", "8"
                };
                frame.add(BorderLayout.NORTH, new JComboBox(arr));
                frame.add(BorderLayout.SOUTH, new JButton("some button"));

                frame.pack();
                setVsyncRequested(frame, doVsync);

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

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: 1177 / 1239
Referenced in: [show references]