2014-04-28 67 views
-1

我想更新工具提示,所以我認爲在使用重繪方法時,每隔30秒重新繪製一次幀,但不起作用。有人可以helpme ..如何在特定時間後調用repaint()方法

GrphicsTut方法

import java.awt.Color; 
import java.awt.Font; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.Timer; 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 

public class GrphicsTut extends JFrame{ 
    Timer timer; 
    Image image; 
    Image image2; 
    int x1,y1,x2,y2; 
    Font fonte = new Font("TimesRoman ",Font.BOLD,100); 

    public GrphicsTut(){ 
     MouseListenerJ bkg = new MouseListenerJ(); 
     this.setTitle("Remember g for Graphics"); 
     this.setSize(600,500); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.setVisible(true); 
     this.addMouseMotionListener(bkg); 
     this.addMouseListener(bkg); 
     this.add(bkg); 
    } 

    public void paint(Graphics gr){ 
     gr.setFont(fonte); 
     gr.setColor(Color.black); 
     gr.drawLine(35,35, 410, 110); 
     gr.drawLine(410,110, 310, 410); 
     ImageIcon i=new ImageIcon("image/1005511030.jpg"); 
     image=i.getImage(); 
     gr.drawImage(image, 35, 35,null); 
     gr.drawImage(image, 400, 100,null); 
     gr.drawImage(image, 300, 400,null); 
     repaint(); 
    } 

    public static void main(String[] args) { 
     new GrphicsTut(); 
    } 
} 

這裏的方法MouseListner

package Carte; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.awt.event.MouseMotionListener; 
import javax.swing.ImageIcon; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
public class MouseListenerJ extends JPanel implements MouseListener, MouseMotionListener { 
    public MouseListenerJ(){} 
    public void paint(Graphics g){} 

    @Override 
    public void mouseClicked(MouseEvent me) { 
     // TODO Auto-generated method stub 
     if(me.getX()>=35 && me.getX()<=70 && me.getY()>=35 &&me.getY()<=70){ 
      //JOptionPane.showMessageDialog(null,"Que voulez vous faire"); 
      new Carte1(); 
     }  
    } 

    @Override 
    public void mouseEntered(MouseEvent me) { 
     // TODO Auto-generated method stub 
     //System.out.println("Entered at x "+me.getX()); 
    } 

    @Override 
    public void mouseExited(MouseEvent me) { 
     // TODO Auto-generated method stub 
     //System.out.println("Exited"); 
    } 

    @Override 
    public void mousePressed(MouseEvent me) { 
     // TODO Auto-generated method stub 
     System.out.println("Pressed at : "+me.getX()+" "+me.getY()); 
    } 

    @Override 
    public void mouseReleased(MouseEvent arg0) { 
     // TODO Auto-generated method stub 
     System.out.println("released"); 
    } 

    @Override 
    public void mouseDragged(MouseEvent arg0) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void mouseMoved(MouseEvent me) { 
     // TODO Auto-generated method stub 
     if(me.getX()>=35 && me.getX()<=70 && me.getY()>=35 && me.getY()<=70){ 
      //JOptionPane.showMessageDialog(null,"Que voulez vous faire"); 
      this.setToolTipText("text"); 
      System.out.println(me.getX()+" "+me.getY()); 
     } 
     else if(me.getX()>=400 && me.getX()<=470 && me.getY()>=100 && me.getY(<=170){ 
      this.setToolTipText("Text 2"); 
      System.out.println(me.getX()+" "+me.getY()); 
     } 
    } 
} 
+3

1)考慮告訴我們你的原意,因爲這看起來很糟糕2)不要在任何一個內部調用repaint() paint()方法,這將創建一個無限循環3)總是在paintXXX()方法中調用super.paintXXX()4)考慮重寫paintComponent而不是paint()5)不要在paint()方法中創建ImageIcon,相反,創建它,並重復使用相同的變量(在這種情況下,你甚至可以使它成爲常量) –

+0

謝謝你的回答。我的目標是將一些圖標放入圖形界面,然後使用mouseEvent獲取這些圖標的工具提示,或者如果我喜歡它,可以傳遞給其他界面。 – aziz

回答

1

考慮使用本地Swing組件爲你的目標。它們提供你想要的所有功能:

  • 處理鼠標點擊
  • 顯示工具提示
  • 顯示圖像

這裏是一個小片段向您展示如何使用JButton

import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.net.MalformedURLException; 
import java.net.URL; 

import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class TestButtons { 

    protected void initUI() throws MalformedURLException { 
     JFrame frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JPanel panel = new JPanel(new GridLayout(2, 2, 20, 20)); 
     panel.add(createButton("https://pbs.twimg.com/profile_images/378800000534649369/489a1e058bea59b62fd73c56f4bcb6c7.jpeg")); 
     panel.add(createButton("https://pbs.twimg.com/profile_images/378800000714838591/ebbde1563faae6da2be79df945a7a02b.jpeg")); 
     panel.add(createButton("https://pbs.twimg.com/profile_images/3477392906/f1907df0bd76668deac4a5e31a22fbe7.jpeg")); 
     panel.add(createButton("https://pbs.twimg.com/profile_images/2718799802/9567ed3c3299f6f6ab1ffcbcbfd93da5.jpeg")); 
     frame.add(panel); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    private JButton createButton(final String url) throws MalformedURLException { 
     final JButton button = new JButton(new ImageIcon(new URL(url))); 
     button.setToolTipText("You are looking at image located at " + url); 
     button.setBorderPainted(false); 
     button.setContentAreaFilled(false); 
     button.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       JOptionPane.showMessageDialog(button, 
         "You clicked on image located at " + url); 
      } 
     }); 
     return button; 
    } 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        new TestButtons().initUI(); 
       } catch (MalformedURLException e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

} 
+0

這種方法的問題是,我不能畫線,這就是爲什麼我選擇在Java中使用圖形。因爲我需要一些網絡設備的地圖。 [link](http://2.bp.blogspot.com/_ccujEqzvArs/TEkRXpW4MmI/AAAAAAAAsDE/jsejvf-zJ6I/s1600/bgp-multipath-test.png)_italic_ ** bold **'code'。 當我將鼠標懸停在圖像或鏈接(Line)上時,它將顯示一些信息,如IP地址,端口等。 – aziz

+1

@aziz _此方法的問題是我無法繪製line_? : - ? ???讓我對此表示驚訝:1)這仍然是完全可能的。 2)爲什麼不首先提到這一點? –

+0

因爲當我用方法drawImage(圖形)繪製圖像。我確切知道它的座標。所以我將使用它們通過使用drawLine(x1,y1,x2,y2)鏈接兩個圖像。但在這種情況下,您使用了GridLayout ..(或其他)..這是一個 [示例](http://2.bp.blogspot.com/_ccujEqzvArs/TEkRXpW4MmI/AAAAAAAAsDE/jsejvf-zJ6I/s1600/bgp- multipath-test.png)我想繪製的。 (我在最後一條評論中提到過) 爲什麼不首先提及:因爲我的問題是重繪方法(我想要做我的自我學習;)) 謝謝。 – aziz