推 PsMonkey:check 一下 repaint() 等畫面繪製的底層流程 04/10 17:58
抱歉我吧整段code貼上來
我目前用java.awt 將圖片load近來
我這段程式的功能是用滑鼠滾輪切換圖片
目前已經可以做到偵測到滑鼠滾輪
切換圖片的部分有個bug
就是我滑鼠假設我滾了一格 我設計上是要由example1.jpg 切換到 example2.jpg
但在視窗中(textArea)中圖不會改變 不過如果移動視窗或是改變視窗大小
圖就會變成我想要的(例:滑鼠滾輪動一格 圖片不會變 但改變視窗大小後
圖就變成example2.jpg了)
我在想說是不是我的Show_off_test中 改變圖片時 沒有辦法及時flush??
我用的OS是vista-32bit 會不會是OS的問題= =詭異的bug 大家幫幫我吧 謝謝
public class MouseWheelEventDemo extends JPanel
implements MouseWheelListener {
JTextArea textArea;
JScrollPane scrollPane;
final static String newline = "\n";
private static ImageDelegate mImage;
static JFrame frame = new JFrame("Project 2007.4.7");
static Graphics g;
static String[] names = {"example1.jpg","example2.jpg","example3.jpg","example4.jpg","example5.jpg"};
int count = 0;
public MouseWheelEventDemo() {
super(new BorderLayout());
textArea = new JTextArea();
textArea.setEditable(false);
scrollPane = new JScrollPane(textArea);
scrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setSize( new Dimension (1024,768));
add(scrollPane, BorderLayout.CENTER);
//Register for mouse-wheel events on the text area.
textArea.addMouseWheelListener(this);
setPreferredSize(new Dimension(1024, 768));
setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
}
public void mouseWheelMoved(MouseWheelEvent e){
// String message;
int notches = e.getWheelRotation();
if (notches == -1){
count--;
}
else{
count++;
}
if( count > 4 )
count = 0;
else if( count < 0)
count = 4;
ShowOff_test( names[ count ]);
System.out.println("TEST: " + names[ count ]);
}
public void paint( Graphics g ) {
Graphics2D g2 = (Graphics2D)g;
// Turn on antialiasing.
g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
g2.drawImage( MouseWheelEventDemo.mImage.getImage(), 0, 0, null );
//frame.dispose();
//g2.dispose();
}
public static void ShowOff_test( String filename) {
try {
final Image image = ImageIO.read( new File( filename ) );
mImage = (
new ImageDelegate() {
public Image getImage() { return image; }
}
);
//this.setSize( mImage.getWidth( null ), mImage.getHeight( null ) );
} catch ( Exception _ ) {
throw new RuntimeException( _ );
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new MouseWheelEventDemo();
newContentPane.setOpaque(false); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
//frame.update(mImage.getGraphics());
frame.dispose();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
ShowOff_test( names[ 0 ]);
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
--
想要賺大錢的= =
來找我吧~
介紹全台灣即將引爆最新的產業給您 非傳直銷唷
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.119.163.250