2014-09-28 64 views
2

我使用NetBeans(JDK 1.7)在Windows 8.1中運行該應用程序:風俗畫的JPanel抵消繪圖

  • 採用Graphics對象draw a frog在400x400的JPanelpaintComponent();
  • 將面板添加到JFrame用於顯示目的;

這當然應該是一個非常簡單的任務,但我無法理解此應用程序中的特定行爲。

下面是問題:第一次顯示窗口時,面板顯示在一個偏移量(下圖),當它不應該。我試圖理解爲什麼會發生,以及如何解決它:

的一個箭頭鍵之後按下鍵盤上的圖紙被移位5個像素左/右/上/下。但是,當發生這種情況時,整個JPanel似乎被移動到窗口的(0,0)座標處(這是我從一開始就預期的),因此移動繪圖比它應該多得多:

我在面板的邊緣畫了一個黑色的邊框,使問題更加明顯。

這裏是Short, Self Contained, Correct (Compilable), Example

KAfrog.java

import javax.swing.SwingUtilities; 
import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent; 

public class KAfrog 
{   
    public static void main(String args[]) 
    {       
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI(); 
      } 
     } // end of anonymous innerclass 
     ); // end of invokeLater  
    }  

    private static void createAndShowGUI() 
    { 
     System.out.println("Created GUI on EDT? " + SwingUtilities.isEventDispatchThread()); 

     Window janela = new Window("Khan Academy Frog");   
     janela.addWindowListener(new WindowAdapter() { 
      public void windowClosing(WindowEvent e) 
      { 
       System.exit(0); 
      } 
     } // end of anonymous innerclass 
     ); // end of addWindowListener    
    }  
} 

Window.java

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.border.LineBorder; 

class DrawingPanel extends JPanel 
{  
    private int x = 50, y = 50; 

    public DrawingPanel() { 
     super(); 
     setBorder(new LineBorder(Color.BLACK)); 
    }  

    public Dimension getPreferredSize() { 
     return new Dimension(400, 400); 
    } 

    public void setX(int x) {  
     if (x < 0 || x >= 400) 
      return; 

     this.x = x;   
     repaint(); 
    } 

    public void setY(int y) { 
     if (y < 0 || y >= 400) 
      return; 

     this.y = y; 
     repaint(); 
    } 

    public int getX() { return this.x; } 

    public int getY() { return this.y; } 

    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     System.out.println("DrawingPanel::paintComponent: drawing at x:" + x + " y:" + y); 
     System.out.println("DrawingPanel::paintComponent: panel size " + getWidth() + "x" + getHeight());   

     // face 
     Color green = new Color(30, 204, 91); 
     g.setColor(green); 
     g.fillOval(x, y, 200, 100); 

     // mouth 
     g.setColor(Color.BLACK); // g.setColor(new Color(0, 0, 0)); 
     g.fillOval(x+25, y+25, 150, 50); 

     // left eye 
     g.setColor(green); 
     g.fillOval(x+30, y-30, 45, 45); // background 
     g.setColor(Color.WHITE); 
     g.fillOval(x+35, y-25, 35, 35); // white eye sockets 
     g.setColor(Color.BLACK); 
     g.fillOval(x+48, y-15, 10, 10); // black eyes pretos 

     // right eye 
     g.setColor(green); 
     g.fillOval(x+110, y-30, 45, 45); // background 
     g.setColor(Color.WHITE); 
     g.fillOval(x+115, y-25, 35, 35); // white eye sockets 
     g.setColor(Color.BLACK); 
     g.fillOval(x+128, y-15, 10, 10); // black eyes     
    } 
} 

public class Window extends JFrame 
{ 
    DrawingPanel frog_panel; 

    public Window(String title) 
    {    
     super(title);        
     frog_panel = new DrawingPanel();    

     addKeyListener(new KeyListener() { 
      public void keyPressed(KeyEvent e) { 
       if (e.getKeyCode() == KeyEvent.VK_LEFT) 
        frog_panel.setX(frog_panel.getX()-5); 

       if (e.getKeyCode() == KeyEvent.VK_RIGHT) 
        frog_panel.setX(frog_panel.getX()+5); 

       if (e.getKeyCode() == KeyEvent.VK_UP) 
        frog_panel.setY(frog_panel.getY()-5); 

       if (e.getKeyCode() == KeyEvent.VK_DOWN) 
        frog_panel.setY(frog_panel.getY()+5); 
      } 

      public void keyReleased(KeyEvent e) { } 
      public void keyTyped(KeyEvent e) { } 
     } 
     ); 

     add(frog_panel);  
     pack(); 
     setVisible(true);   
     System.out.println("Window::Window: size is " + getWidth() + "x" + getHeight());     
    }    
} 

有誰知道爲什麼會這樣,以及如何解決它?

回答

3

您將覆蓋public int getX() from javax。swing。JComponent。

它返回組件原點的當前x座標。此方法優於編寫component.getBounds()。x或component.getLocation()。x,因爲它不會導致任何堆分配。

重命名你的方法public int getX()public int getY()

這應該可以解決這個問題。

+0

+1非常感謝! – karlphillip 2014-09-28 20:25:30