Skip to content

Commit

Permalink
another Swing performance fix
Browse files Browse the repository at this point in the history
  • Loading branch information
klesun committed Aug 24, 2015
1 parent a73f2c0 commit 58f0edc
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 15 deletions.
4 changes: 3 additions & 1 deletion src/org/shmidusic/MainPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ public void replaceSheetMusic(SheetMusic sheetMusic)
}

public void chordChanged() {
pianoLayoutPanel.repaint();
pianoLayoutPanel.paintImmediately(pianoLayoutPanel.getVisibleRect()); // simple repaint() would sometimes lag for a half second, what was not likable
// this may also start lagging, but only if you unfocus window
statusField.setText(getStatusText());
statusField.paintImmediately(statusField.getVisibleRect());
sheetContainer.checkCam();
}

Expand Down
6 changes: 4 additions & 2 deletions src/org/shmidusic/sheet_music/staff/StaffComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,12 @@ public StaffComponent setFocus(int index)

if (was != null) {
was.setFocusedIndex(-1);
was.repaint();
was.paintImmediately(was.getVisibleRect()); // simple repaint() would sometimes lag for a half second, what was not likable
// was.repaint();
}
if (staff.getFocusedIndex() != -1) {
getFocusedChild().repaint();
// getFocusedChild().repaint();
getFocusedChild().paintImmediately(getFocusedChild().getVisibleRect()); // simple repaint() would sometimes lag for a half second, what was not likable
}

getModelParent().mainPanel.chordChanged();
Expand Down
14 changes: 2 additions & 12 deletions src/org/shmidusic/stuff/test/JavaFXTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,7 @@ private static void initAndShowGUI() {
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Platform.runLater(new Runnable() {
@Override
public void run() {
initFX(fxPanel);
}
});
Platform.runLater(() -> initFX(fxPanel));
}

private static void initFX(JFXPanel fxPanel) {
Expand All @@ -51,11 +46,6 @@ private static Scene createScene() {
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
initAndShowGUI();
}
});
SwingUtilities.invokeLater(() -> initAndShowGUI());
}
}
66 changes: 66 additions & 0 deletions src/org/shmidusic/stuff/test/SwingRepaintTimingTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.shmidusic.stuff.test;

// we shall experiment with the time parameter of JComponent::repaint()
// i presume time we specify is time when buffers will be swapped?

import org.shmidusic.stuff.tools.Fp;

import javax.swing.*;
import java.awt.*;

public class SwingRepaintTimingTest extends JPanel
{
private static JLabel counter;

public static void main(String[] args) {
JFrame window = new JFrame("zhopa");
window.setLayout(new BorderLayout());

counter = new JLabel("0");
window.add(counter, BorderLayout.EAST);

window.add(new SwingRepaintTimingTest(), BorderLayout.WEST);
window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
window.setSize(1600, 800);

window.setVisible(true);
}

private SwingRepaintTimingTest() {
super();
setPreferredSize(new Dimension(1440, 800));
setFocusable(true);
requestFocus();

addKeyListener(Fp.onKey(k -> repaintWithin(8000)));
}

protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(10, 10, 800, 800);

for (int i = 0; i < 50000; ++i) {
int x = (int) (Math.random() * 1440);
int y = (int) (Math.random() * 800);

g.setColor(new Color(x * 255 / 1440, y * 255 / 800, 0));
g.drawLine(10, 10, x, y);
}
}

/** repaints into hidden buffer and swaps em after millis */
// apparently does not repaint into hidden buffer
private void repaintWithin(int millis)
{
new Thread(() -> {
for (int i = millis / 1000; i >= 0; --i) {
counter.setText("" + i);
try { Thread.sleep(1000); } catch (InterruptedException exc) {}
}
}).start();

Timer swingTimer = new Timer(millis, k -> repaint());
swingTimer.setRepeats(false);
swingTimer.start();
}
}

0 comments on commit 58f0edc

Please sign in to comment.