Оптимизация перемещения JPanels в программе Java Swing ⇐ JAVA
Оптимизация перемещения JPanels в программе Java Swing
I'm trying to run some performance tests for some game I'm planning to program with Java Swing as a warmup on Swing API. Simply put, the game is based on some multiple windows moving in accordance and showing different entities of the game. So for the performance test, I wrote the a code that just randomly puts JPanels on the screen and then moves/resizes them randomly while constantly changing their background color all with a constant FPS of 60.
The problem is that the FPS drops below 10 when showing 4 windows or more and I don't know what I'm doing wrong/can do to enhance performance. I've tried to avoid abusing repaint/paintComponent and use of Thread Sleeps but I don't know what else I can do to get better performance out of this. I'm looking forward to like 15 windows all moving and changing the contentPane all at 60 FPS and this actually seems reasonably possible to me but currently I'm stuck.
Any help/hint/source would be appreciated.
package org.example; import javax.swing.*; import java.awt.*; import java.util.Random; public class Main { public static final int FPS = 60; public static final int HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height; public static final int WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width; public static final float ACCELERATION = 0.3F; public static final float SPEED = -0.01F; static int cnt=0; static boolean allow=false; static JFrame frame=new JFrame(); public static void main(String[] args) { spawnInitialFrame(); new Timer(500, e -> { if (cnt%3==0) { SwingUtilities.invokeLater(Main::spawnPanel); } cnt++; }){{setCoalesce(false);}}.start(); } public static void spawnInitialFrame(){ System.setProperty("sun.java2d.opengl", "true"); frame.setUndecorated(true); frame.setVisible(true); frame.setResizable(false); frame.setSize(new Dimension(WIDTH, HEIGHT)); frame.setLocationRelativeTo(null); frame.setLayout(null); frame.setBackground(new Color(0,0,0,0)); } public static void spawnPanel() { Random rng=new Random(); JPanel panel=new JPanel(){{changeColor(this);}}; panel.setSize(new Dimension(200,200)); int destX=WIDTH/2+rng.nextInt(-500,500); int destY=HEIGHT/2+rng.nextInt(-500,500); panel.setLocation(destX,destY); frame.add(panel); new Thread(() -> { while (true){ int a1=rng.nextInt(-100,100); int a2=rng.nextInt(-100,100); int a3=rng.nextInt(-100,100); int a4=rng.nextInt(-100,100); // a3=0;a4=0; // a1=0;a2=0; smoothen(panel,a1,a2,a3,a4); } }).start(); } public static void changeColor(JPanel panel){ Random rng=new Random(); new Timer(1000/FPS, e -> { panel.setBackground(new Color(rng.nextInt(100,200),rng.nextInt(100,200),rng.nextInt(100,200),255)); }){{setCoalesce(false);}}.start(); } public static void smoothen(JPanel panel, int moveX,int moveY,int progW,int progH) { final int[] cntMoveX = {0}; final int[] progMoveX = {0}; final int[] cntMoveY = {0}; final int[] progMoveY = {0}; final int[] cntWidth = {0}; final int[] progWidth = {0}; final int[] cntHeight = {0}; final int[] progHeight = {0}; Timer timer=new Timer(1000/FPS, e -> { int x=panel.getLocation().x; int y=panel.getLocation().y; int h=panel.getHeight(); int w=panel.getWidth(); if (abs(progMoveX[0])
Источник: https://stackoverflow.com/questions/781 ... ng-program
I'm trying to run some performance tests for some game I'm planning to program with Java Swing as a warmup on Swing API. Simply put, the game is based on some multiple windows moving in accordance and showing different entities of the game. So for the performance test, I wrote the a code that just randomly puts JPanels on the screen and then moves/resizes them randomly while constantly changing their background color all with a constant FPS of 60.
The problem is that the FPS drops below 10 when showing 4 windows or more and I don't know what I'm doing wrong/can do to enhance performance. I've tried to avoid abusing repaint/paintComponent and use of Thread Sleeps but I don't know what else I can do to get better performance out of this. I'm looking forward to like 15 windows all moving and changing the contentPane all at 60 FPS and this actually seems reasonably possible to me but currently I'm stuck.
Any help/hint/source would be appreciated.
package org.example; import javax.swing.*; import java.awt.*; import java.util.Random; public class Main { public static final int FPS = 60; public static final int HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height; public static final int WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width; public static final float ACCELERATION = 0.3F; public static final float SPEED = -0.01F; static int cnt=0; static boolean allow=false; static JFrame frame=new JFrame(); public static void main(String[] args) { spawnInitialFrame(); new Timer(500, e -> { if (cnt%3==0) { SwingUtilities.invokeLater(Main::spawnPanel); } cnt++; }){{setCoalesce(false);}}.start(); } public static void spawnInitialFrame(){ System.setProperty("sun.java2d.opengl", "true"); frame.setUndecorated(true); frame.setVisible(true); frame.setResizable(false); frame.setSize(new Dimension(WIDTH, HEIGHT)); frame.setLocationRelativeTo(null); frame.setLayout(null); frame.setBackground(new Color(0,0,0,0)); } public static void spawnPanel() { Random rng=new Random(); JPanel panel=new JPanel(){{changeColor(this);}}; panel.setSize(new Dimension(200,200)); int destX=WIDTH/2+rng.nextInt(-500,500); int destY=HEIGHT/2+rng.nextInt(-500,500); panel.setLocation(destX,destY); frame.add(panel); new Thread(() -> { while (true){ int a1=rng.nextInt(-100,100); int a2=rng.nextInt(-100,100); int a3=rng.nextInt(-100,100); int a4=rng.nextInt(-100,100); // a3=0;a4=0; // a1=0;a2=0; smoothen(panel,a1,a2,a3,a4); } }).start(); } public static void changeColor(JPanel panel){ Random rng=new Random(); new Timer(1000/FPS, e -> { panel.setBackground(new Color(rng.nextInt(100,200),rng.nextInt(100,200),rng.nextInt(100,200),255)); }){{setCoalesce(false);}}.start(); } public static void smoothen(JPanel panel, int moveX,int moveY,int progW,int progH) { final int[] cntMoveX = {0}; final int[] progMoveX = {0}; final int[] cntMoveY = {0}; final int[] progMoveY = {0}; final int[] cntWidth = {0}; final int[] progWidth = {0}; final int[] cntHeight = {0}; final int[] progHeight = {0}; Timer timer=new Timer(1000/FPS, e -> { int x=panel.getLocation().x; int y=panel.getLocation().y; int h=panel.getHeight(); int w=panel.getWidth(); if (abs(progMoveX[0])
Источник: https://stackoverflow.com/questions/781 ... ng-program
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение