Created
November 2, 2021 03:51
-
-
Save SelvinPL/29929a8b22c60264b16594cff4fb612e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package pl.selvin.test; | |
| import java.awt.BorderLayout; | |
| import java.awt.Color; | |
| import java.awt.DisplayMode; | |
| import java.awt.EventQueue; | |
| import java.awt.Graphics; | |
| import java.awt.GraphicsDevice; | |
| import java.awt.GraphicsEnvironment; | |
| import java.awt.event.ActionEvent; | |
| import java.awt.event.ActionListener; | |
| import javax.swing.JFrame; | |
| import javax.swing.JPanel; | |
| import javax.swing.Timer; | |
| final public class SwingTest { | |
| public static void main(String... args) { | |
| GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); | |
| GraphicsDevice[] gs = ge.getScreenDevices(); | |
| for (int i = 0; i < gs.length; i++) { | |
| DisplayMode dm = gs[i].getDisplayMode(); | |
| int refreshRate = dm.getRefreshRate(); | |
| if (refreshRate == DisplayMode.REFRESH_RATE_UNKNOWN) { | |
| System.out.println("Unknown rate"); | |
| } | |
| System.out.println("Rate: " + refreshRate); | |
| int bitDepth = dm.getBitDepth(); | |
| int numColors = (int) Math.pow(2, bitDepth); | |
| } | |
| EventQueue.invokeLater(() -> { | |
| JFrame frame = new JFrame("Test"); | |
| frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
| frame.getContentPane().add(BorderLayout.CENTER, new DrawPanel(frame)); | |
| frame.setSize(300, 300); | |
| frame.setLocationByPlatform(true); | |
| frame.setVisible(true); | |
| }); | |
| } | |
| static class DrawPanel extends JPanel { | |
| private static final long serialVersionUID = 1L; | |
| private final JFrame frame; | |
| private final int delay = 5; | |
| private final boolean SWING_TIMER = true; | |
| private final java.util.Timer utilTimer = new java.util.Timer(); | |
| private int oneX = 7; | |
| private int oneY = 7; | |
| private boolean up = false; | |
| private boolean down = true; | |
| private boolean left = false; | |
| private boolean right = true; | |
| private final javax.swing.Timer swingTimer = new Timer(delay, actionEvent -> moveIt()); | |
| private double oldTime; | |
| public DrawPanel(JFrame frame) { | |
| this.frame = frame; | |
| if (SWING_TIMER) { | |
| swingTimer.start(); | |
| } else { | |
| utilTimer.scheduleAtFixedRate(new java.util.TimerTask() { | |
| @Override | |
| public void run() { | |
| moveIt(); | |
| } | |
| }, delay, delay); | |
| } | |
| oldTime = System.nanoTime(); | |
| } | |
| double getFps() { | |
| double newTime = System.nanoTime(); | |
| double delta = newTime - oldTime; | |
| double fps = 1000000000 / (delta); | |
| oldTime = newTime; | |
| return fps; | |
| } | |
| public void paintComponent(Graphics g) { | |
| g.setColor(Color.BLUE); | |
| g.fillRect(0, 0, this.getWidth(), this.getHeight()); | |
| g.setColor(Color.RED); | |
| g.fillRect(3, 3, this.getWidth() - 6, this.getHeight() - 6); | |
| g.setColor(Color.WHITE); | |
| g.fillRect(6, 6, this.getWidth() - 12, this.getHeight() - 12); | |
| g.setColor(Color.BLACK); | |
| g.fillRect(oneX, oneY, 6, 6); | |
| frame.setTitle(String.format("FPS: %.2f", getFps())); | |
| } | |
| private void moveIt() { | |
| if (oneX > getWidth() - 13) { | |
| right = false; | |
| left = true; | |
| } | |
| if (oneX < 7) { | |
| right = true; | |
| left = false; | |
| } | |
| if (oneY > getHeight() - 13) { | |
| up = true; | |
| down = false; | |
| } | |
| if (oneY < 7) { | |
| up = false; | |
| down = true; | |
| } | |
| if (up) oneY--; | |
| if (down) oneY++; | |
| if (left) oneX--; | |
| if (right) oneX++; | |
| repaint(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment