簡體   English   中英

為什么我的paintComponent無法正常工作?

[英]Why isn't my paintComponent working?

我要對程序進行的操作是單擊圖像時,矩形將與JOptionPane一起出現。 但是,JOptionPane是唯一彈出的窗口。

我試圖更改方法並添加更多的類,但沒有任何幫助>。<任何人都可以弄清楚我的問題嗎? 這是我的代碼片段。

下面是我稱為filechooser的位置,它使我可以選擇自己的照片。 另外,這里還有很多其他東西,例如標簽。

public Help(){

        fc.setDialogTitle("Choose an image file to begin:");
        int returnval = fc.showOpenDialog(null);
        if (returnval == JFileChooser.APPROVE_OPTION){ //when user selects a file, value returned will be JFileChooser.APPROVE_OPTION
            File file = fc.getSelectedFile(); //the File value of the selection is returned from a call on getSelectedFile
            try{
                image = ImageIO.read(file); //reads and loads File as image
            }
            catch (IOException e){}
                System.out.println("You chose to open this file: " + file.getName());
        }else
            System.out.println("No file selected.");

        icon = new ImageIcon(image);
        label = new JLabel(icon);
        tagName = new JLabel(input);

        label.addMouseListener(new ImagePanel());
        label.addMouseMotionListener(new ImagePanel());
        panel.add(tagName);
    }

最后是我的ImagePanel類,其中包含麻煩的paintComponent。 另外,還有幾個mouseListeners。

class ImagePanel extends JPanel implements MouseListener, MouseMotionListener{

        @Override
        public void mouseClicked(MouseEvent event) {
            // TODO Auto-generated method stub

                x = event.getX();
                y = event.getY();

                input = JOptionPane.showInputDialog("Enter tag name");
                tagName.setText("You have tagged: " + input);
                System.out.println(input);
        }

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

        }

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

        }

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

        }

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

        }

        public void paintComponent(Graphics g){
            super.paintComponent(g);

                if(image != null && isRectPresent){
                    g.setColor(Color.DARK_GRAY);
                    g.drawRect(x-50, y-50, 100, 100);
                }
        }   

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

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

您可以編譯代碼並親自查看。 如果您知道該怎么辦,請給我個提示:)非常感謝!

各種奇怪的東西:

label.addMouseListener(new ImagePanel());
label.addMouseMotionListener(new ImagePanel()); 

您不應該只是為了向組件添加偵聽器而創建新的JPanel。 您已經有一個面板實例。

addMouseMotionListener(this);  

切勿在繪制方法中向組件添加偵聽器。 您永遠無法控制何時調用繪畫方法,並且最終將添加多次相同的偵聽器。

一個注意事項:一個較小的示例將在前面得到答復。

使用其他名稱將鼠標事件x和y分配給ImagePanel中的自定義字段,例如:

int mx;
int my;

要嘗試的其他事情是忽略super.paintComponent。 此外,也許您想在g上使用更多方法:

Graphics2D g2 = (Graphics2D)g;

(分配基類x和y從來都不是一個好主意;最好將setBounds用於更改坐標等。)

  try{
       image = ImageIO.read(file); //reads and loads File as image
   }
   catch (IOException e){}

這里的代碼說:“讓我們嘗試讀取圖像。如果失敗(拋出異常),請忽略該問題並繼續使用該圖像。” 忽略問題總是不好的。 我們至少可以打印出問題並繼續。

  try{
      image = ImageIO.read(file); //reads and loads File as image
  }
  catch (IOException e){
     e.printStackTrace();//print the exception
  }

或打印問題並停止:

  try{
      image = ImageIO.read(file); //reads and loads File as image
  }
  catch (IOException e){
     e.printStackTrace();//print the exception
     System.exit(0);//stop executing
  }

實際問題很可能在這里:

if(image != null && isRectPresent){
   g.setColor(Color.DARK_GRAY);
   g.drawRect(x-50, y-50, 100, 100);
}

我認為問題在於if條件為false (沒有圖像(也許有異常讀取它...?)和/或isRectPresent為false ),因此它什么也不做! if處包含一個斷點,以調試模式啟動程序,並在程序到達此點時檢查變量imageisRectPresent (如果沒有到達目的地,則說明您遇到了其他問題。)祝您好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM