Loading...

osxでjava - resizeしてチラつくについて

Javaです。osxで動作するjavaです。

JFrameを使ってコードを試していてふと気になったことです。
このフレームをsetSizeして大きさを変更するとどうもflickerが発生しました。

おもしろいことに、osxのJFrameにデフォルトでついている右はしのresizeアイコンで大きさを変えるとちらつかないです。


理由が知りたくて、実行中に例外を発生させてどういった経路でペイントが走るのかみてみました。
なんとなく違うのはわかったのですが、どう解消すればよいのかノーアイデアです....

ちらつくときの例外
at t004a.TestResizableWindow$1.paintComponent(TestResizableWindow.java:65)
at javax.swing.JComponent.paint(JComponent.java:1006)
at javax.swing.JComponent.paintChildren(JComponent.java:843)
at javax.swing.JComponent.paint(JComponent.java:1015)
at javax.swing.JComponent.paintChildren(JComponent.java:843)
at javax.swing.JComponent.paint(JComponent.java:1015)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:559)
at javax.swing.JComponent.paintChildren(JComponent.java:843)
at javax.swing.JComponent.paint(JComponent.java:1015)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:34)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
at java.awt.Container.paint(Container.java:1722)
at javax.swing.JFrame.update(JFrame.java:393)
at sun.awt.RepaintArea.updateComponent(RepaintArea.java:267)
at sun.awt.RepaintArea.paint(RepaintArea.java:233)
at apple.awt.ComponentModel.handleEvent(ComponentModel.java:251)
at apple.awt.CWindow.handleEvent(CWindow.java:176)
at java.awt.Component.dispatchEventImpl(Component.java:4126)
at java.awt.Container.dispatchEventImpl(Container.java:2068)
at java.awt.Window.dispatchEventImpl(Window.java:1791)
at java.awt.Component.dispatchEvent(Component.java:3885)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:176)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)


ちらつかないときの例外
実際のペイント処理までが短いです。
at t004a.TestResizableWindow$1.paintComponent(TestResizableWindow.java:65)
at javax.swing.JComponent.paint(JComponent.java:1006)
at javax.swing.JComponent.paintChildren(JComponent.java:843)
at javax.swing.JComponent.paint(JComponent.java:1015)
at javax.swing.JComponent.paintChildren(JComponent.java:843)
at javax.swing.JComponent.paint(JComponent.java:1015)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:559)
at javax.swing.JComponent.paintChildren(JComponent.java:843)
at javax.swing.JComponent.paint(JComponent.java:1015)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:34)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
at java.awt.Container.paint(Container.java:1722)
at apple.awt.ComponentModel$1.run(ComponentModel.java:351)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:199)
at apple.awt.CToolkit$CPeerEvent.dispatch(CToolkit.java:1116)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:176)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)



使用したコード
package t004a;

import java.awt.BorderLayout;
import java.awt.Color;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestResizableWindow {

public static void main(String[] args) {

final JFrame window = new JFrame();
class DragMove extends MouseAdapter implements MouseMotionListener {

Point pressed;

public void mouseDragged(MouseEvent e) {
// System.out.println(e);
if (this.pressed != null) {
int dw = pressed.x - e.getX();
int dh = pressed.y - e.getY();
window.setSize(window.getWidth() - dw, window.getHeight()
- dh);
}
this.pressed = e.getPoint();
}

public void mouseMoved(MouseEvent e) {

}

@Override
public void mouseReleased(MouseEvent e) {
this.pressed = null;
}

@Override
public void mouseExited(MouseEvent e) {
this.pressed = null;
}
}

JPanel panel = new JPanel(){
{
setOpaque(true);
add(new JButton("aaaaaaaaaa"));
}

@Override
protected void paintComponent(Graphics g) {
try{
if(true) throw new RuntimeException();
}catch(Exception e){
e.printStackTrace();
}
super.paintComponent(g);
int x2 = this.getWidth();
int y2 = this.getHeight();
System.out.println("*** ");
Graphics2D g2d = (Graphics2D)g;
g2d.clearRect(0,0, x2, y2);
g2d.setColor(Color.BLUE);
g2d.drawLine(0, 0, x2, y2);
}

};
window.getContentPane().setLayout(new BorderLayout());
window.getContentPane().add(panel, BorderLayout.CENTER);

DragMove dragMove = new DragMove();
window.addMouseListener(dragMove);
window.addMouseMotionListener(dragMove);
window.setBounds(100, 100, 300, 300);
window.setAlwaysOnTop(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}

}
リアクション: 
osx 25760637744536743

コメントを投稿

ホーム item

このブログを検索

Random Posts

Popular Posts

Labels

ADS