2013-05-03 40 views
0

好吧,所以我用下面的paint方法創建了一個JPanel,它起初工作得很好,但是當JPanel被調整大小時(在JFrame中)不會將它繪製到Frame的中心。組件Paint方法不能繪製到JPanel中間?

@Override 
    protected void paintComponent(Graphics g) { 
     Graphics2D graphics = (Graphics2D) g; 
     Dimension dimension = frame.getSize(); 
     Insets insets = getInsets(); 
     int w = (int) ((dimension.getWidth() - insets.left - insets.right)/2); 
     int h = (int) ((dimension.getHeight() - insets.top - insets.bottom)/2); 
     graphics.translate(w, h); 
     graphics.drawString("Origin", 0, 0); 
     double y = 0; 
     for (double x = -25; x <= 25; x += .01) { 
      y = -Math.pow(x, 2); 
      int gx = (int) x; 
      int gy = (int) y; 
      System.out.println("Parabola Coordinate: " + x + ", " + y); 
      g.drawRect(gx, gy, 0, 0); 
     } 
    } 
+0

我擔心graphics.translate(w,h);'在'Graphics'上下文的活動副本上。這意味着任何繪製在它之後的東西也會被翻譯成這一點。您需要複製'Graphics'上下文(即'Graphics#create()'或反轉翻譯... – MadProgrammer 2013-05-03 03:25:05

+0

paintComponent重寫的第一行需要是'super.paintComponent(g);'。 – VGR 2013-05-03 23:02:37

回答

4

更改paintComponent看起來更像

@Override 
protected void paintComponent(Graphics g) { 
    // Create a copy of the graphics context... 
    Graphics2D graphics = (Graphics2D) g.create(); 
    // Don't rely on the frame, rely on your own components size... 
    //Dimension dimension = frame.getSize(); 
    Insets insets = getInsets(); 
    int w = (int) ((getWidth() - insets.left - insets.right)/2); 
    int h = (int) ((getHeight() - insets.top - insets.bottom)/2); 
    graphics.translate(w, h); 
    graphics.drawString("Origin", 0, 0); 
    double y = 0; 
    for (double x = -25; x <= 25; x += .01) { 
     y = -Math.pow(x, 2); 
     int gx = (int) x; 
     int gy = (int) y; 
     System.out.println("Parabola Coordinate: " + x + ", " + y); 
     // You were using the "un-translated" reference, don't know if that was deliberate 
     graphics.drawRect(gx, gy, 0, 0); 
    } 
    // Dispose of the copy and safe resources... 
    graphics.dispose(); 
} 

確保您還使用合適的佈局管理器!

enter image description hereenter image description here

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Insets; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class BadPaint21 { 

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

    public BadPaint21() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     public TestPane() { 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(200, 200); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      Graphics2D graphics = (Graphics2D) g.create(); 
//   Dimension dimension = frame.getSize(); 
      Insets insets = getInsets(); 
      int w = (int) ((getWidth() - insets.left - insets.right)/2); 
      int h = (int) ((getHeight() - insets.top - insets.bottom)/2); 
      graphics.translate(w, h); 
      graphics.drawString("Origin", 0, 0); 
      double y = 0; 
      for (double x = -25; x <= 25; x += .01) { 
       y = -Math.pow(x, 2); 
       int gx = (int) x; 
       int gy = (int) y; 
       System.out.println("Parabola Coordinate: " + x + ", " + y); 
       graphics.drawRect(gx, gy, 0, 0); 
      } 
      graphics.dispose(); 
     } 
    } 
} 
0

塗裝面積的大小應根據面板不幀的大小:

//Dimension dimension = frame.getSize(); 
Dimension dimension = getSize(); 

此外,如果你想中心一幅畫,那麼你需要了解繪畫的大小。因此,例如高度範圍從-625至0。你需要考慮到625的最大大小高度,以y軸上的代碼中心應該是:

int h = (int) ((dimension.getHeight() - insets.top - insets.bottom - 625)/2); 

而且,因爲計算的值都是負值,您需要進行額外的翻譯以確保座標全部爲正值。所以翻譯將是:

graphics.translate(w, h + 625); 

相同的基本邏輯也適用於水平翻譯。