Window will be blank during MouseWheelMotion event

I wrote this simple program that displays keystrokes, draws the phrase “Hello World”, where one cursor moves (with trail mode when pressed), and also cycles through the colors in which “Hello World” is selected when the mouse wheel scrolls. However, there is a problem with this: when the mouse wheel scrolls, the whole window becomes blank (shows a gray color by default from the moment the component first appears) and then will be redrawn with a color change (a very small change only for "Hello World", which does not seem to be requires the entire frame to be redrawn.

The time during which the void appears seems to correlate with the force with which the mouse wheel scrolls, if I scroll very easily, there is only a very small moment when everything is not displayed, however, scrolling can make the window very strong for 2-3 seconds.

I tried double buffering, believing that it might be some kind of screen flickering - but it has not changed, and I do not understand what might cause this strange effect. It is as if a frame image is being loaded during a wheel movement event. (Maybe there is a way to immediately exit the wheel event to reduce load time? (This is just my guess about possible solutions)).

The code is below. Any ideas would be greatly appreciated.

package keymouse; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferStrategy; import java.util.LinkedList; import javax.swing.JFrame; public class KeyMouse implements KeyListener, MouseMotionListener, MouseListener, MouseWheelListener, Runnable { boolean trailMode = false; boolean exists = false; display window; LinkedList wordList; LinkedList trailList; LinkedList colorList; Point mousePoint; int TRAIL_SIZE = 10; boolean init = true; int FONT_SIZE = 32; int mouseY; int mouseX; int y; int colorCount = 0; public static void main(String[] args) { KeyMouse k = new KeyMouse(); k.run(); } public KeyMouse() { window = new display(); window.addKeyListener(this); window.addMouseMotionListener(this); window.addMouseListener(this); window.addMouseWheelListener(this); window.setBackground(Color.WHITE); window.setForeground(Color.BLACK); wordList = new LinkedList(); trailList = new LinkedList(); colorList = new LinkedList(); colorList.add(Color.BLACK); colorList.add(Color.BLUE); colorList.add(Color.YELLOW); colorList.add(Color.GREEN); colorList.add(Color.PINK); } @Override public void keyTyped(KeyEvent e) { // do nothing } @Override public void keyPressed(KeyEvent e) { int keyCode; if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { stop(); } keyCode = e.getKeyCode(); addMessage("Pressed:" + e.getKeyText(keyCode)); } @Override public void keyReleased(KeyEvent e) { //do nothing } @Override public void mouseDragged(MouseEvent e) { Point p = new Point(e.getX(), e.getY()); addLocation(p); } @Override public void mouseMoved(MouseEvent e) { Point p = new Point(e.getX(), e.getY()); addLocation(p); } @Override public void mouseClicked(MouseEvent e) { } @Override public void mousePressed(MouseEvent e) { trailMode = true; } @Override public void mouseReleased(MouseEvent e) { trailMode = false; } @Override public void mouseEntered(MouseEvent e) { //do nothing } @Override public void mouseExited(MouseEvent e) { //do nothing } @Override public void mouseWheelMoved(MouseWheelEvent e) { System.out.println(e.getWheelRotation()); colorCount++; if (colorCount > 4) { colorCount = 0; } window.setForeground((Color) colorList.get(colorCount)); } @Override public void run() { window.createBufferStrategy(2); BufferStrategy strategy = window.getBufferStrategy(); while (true) { draw(strategy.getDrawGraphics()); strategy.show(); try { Thread.sleep(20); } catch (Exception ex) { } } } public void draw(Graphics g) { //draw background g.setColor(window.getBackground()); g.fillRect(0, 0, window.getWidth(), window.getHeight()); //draw Text g.setColor(window.getForeground()); g.setFont(new Font("sansserif", Font.BOLD, 32)); int count = trailList.size(); if (trailList.size() > 1 && trailMode == false) { count = 1; } if (exists == true) { for (int i = 0; i < count; i++) { Point p = (Point) trailList.get(i); g.drawString("Hello World", px, py); } } g.setColor(Color.BLACK); y = 56; for (int i = 0; i < wordList.size(); i++) { String word = (String) wordList.get(i); g.drawString((String) wordList.get(i), 100, y); y += 32; } } public void addMessage(String message) { if (y >= window.getHeight()) { wordList.remove(0); } wordList.add(message); } public void addLocation(Point h) { exists = true; trailList.addFirst(h); if (trailList.size() > TRAIL_SIZE) { trailList.removeLast(); } } public void printMessages() { for (int i = 0; i < wordList.size(); i++) { System.out.println(wordList.get(i)); } } private void stop() { System.exit(0); } 
+4
source share
1 answer

The lack of a complete example, I can not reproduce the effect you described. You can compare your code with this example, which has no explicit quenching.

In general

  • JPanel default double buffering; it is unusual to use a different buffer strategy.
  • AnimationTest illustrates Swing Timer and how to display the average drawing period.
  • MouseAdapter useful for overriding a small number of methods.
  • If possible, use generic type security options.

ColorWheel image

 import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.event.MouseAdapter; import java.awt.event.MouseWheelEvent; import java.util.LinkedList; import java.util.Queue; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; /** * @see https://stackoverflow.com/a/10970892/230513 */ public class ColorWheel extends JPanel { private static final int N = 32; private final Queue<Color> clut = new LinkedList<Color>(); private final JLabel label = new JLabel(); public ColorWheel() { for (int i = 0; i < N; i++) { clut.add(Color.getHSBColor((float) i / N, 1, 1)); } this.setBackground(clut.peek()); label.setText(getBackground().toString()); this.add(label); this.addMouseWheelListener(new MouseAdapter() { @Override public void mouseWheelMoved(MouseWheelEvent e) { setBackground(clut.peek()); label.setText(getBackground().toString()); clut.add(clut.remove()); } }); } @Override public Dimension getPreferredSize() { return new Dimension(320, 240); } private void display() { JFrame f = new JFrame("ColorWheel"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(this); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new ColorWheel().display(); } }); } } 
+3
source

All Articles