2017-05-26 57 views
3

我已創建JPanel並將其添加到JFrameJPanel具有TitledBorder,但是當我使用的方法translate(),例如使用translate()方法時,JPanel中的TitledBorder會移動

g2.translate(getWidth()/2, getHeight()/2); 

在整個組件,包括邊界被翻譯。效果如圖1所示。 Figure 1

所需結果如下圖所示。 Figure 2

代碼粘貼在下面。

import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import javax.swing.BorderFactory; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

class GPanel extends JPanel { 
    private void doDrawing(Graphics g) { 
     Graphics2D g2 = (Graphics2D) g; 
     g2.translate(getWidth()/2, getHeight()/2); 
    } 

    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     doDrawing(g); 
    } 
} 

class Main extends JFrame { 
    public Main() { 
    } 

    public static void main(String[] args) { 
     Main ex = new Main(); 
     ex.setSize(new Dimension(400, 400)); 
     GPanel panel = new GPanel(); 
     panel.setBorder(BorderFactory.createTitledBorder("Title")); 
     ex.add(panel); 
     ex.setLocationRelativeTo(null); 
     ex.setVisible(true); 
    } 
} 
+0

這可能是由JFrame上使用的佈局管理器造成的嗎?如果您創建一個標題爲邊界的JPanel來保存GPanel,錯誤是否會持續? – DCON

回答

3

你對GPanel一個實例調用setBorder()然後修改圖形上下文的在後者的實施paintComponent()變換。邊界不知道這一點,所以顯示的結果是完全可以預料的。請按照的父JComponent實施建議進行操作:「將組件放在JPanel中,並將邊框設置爲JPanel。」相關示例顯示爲here

在下面的變型中,

  • 包圍bPanel現在具有Border,和封閉gPanel可以安全地操作該圖形上下文。

  • translate()之前,紅點以原點爲中心;在translate()之後,藍點以原點爲中心,但原點已移動。

  • 當您確實想要覆蓋getPreferredSize()時,請勿使用setPreferredSize()

  • 請勿延長JFrame不必要的。

image

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import javax.swing.BorderFactory; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

class GTest { 

    public static void main(String[] args) { 
     JFrame f = new JFrame(); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     GPanel gPanel = new GPanel(); 
     JPanel bPanel = new JPanel(); 
     bPanel.setBorder(BorderFactory.createTitledBorder("Title")); 
     bPanel.add(gPanel); 
     f.add(bPanel); 
     f.pack(); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    } 

    private static class GPanel extends JPanel { 

     private static final int N = 16; 

     private void doDrawing(Graphics g) { 
      Graphics2D g2 = (Graphics2D) g; 
      g2.setPaint(Color.red); 
      g.fillOval(-N, -N, 2 * N, 2 * N); 
      g2.translate(getWidth()/2, getHeight()/2); 
      g2.setPaint(Color.blue); 
      g.fillOval(-N, -N, 2 * N, 2 * N); 
     } 

     @Override 
     public void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      doDrawing(g); 
     } 

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

試試這個代碼:

private void doDrawing(Graphics g) { 
     Graphics2D g2 = (Graphics2D) g; 
     g2.translate(0, 0); 
    } 
+0

謝謝,但此代碼不能解決問題。我需要將原點翻譯爲'(width/2,height/2)' – Andrej

3

您可能需要通過g2.translate(-x, -y);執行g2.translate(x, y);恢復採取了行動:

Graphics2D g2 = (Graphics2D) g; 
double x = getWidth()/2d; 
double y = getHeight()/2d; 
g2.translate(x, y); 
g2.setPaint(Color.BLUE); 
g2.fill(s); 
g2.translate(-x, -y); 

另一種常見的方法是使用一個新的Graphics對象,它是一個副本GPanel的Graphics對象:

Graphics2D g2 = (Graphics2D) g.create(); 
g2.translate(getWidth()/2, getHeight()/2); 
g2.setPaint(Color.BLUE); 
g2.fill(s); 
g2.dispose(); 

MAIN2。java

import java.awt.*; 
import java.awt.geom.*; 
import javax.swing.*; 

class GPanel extends JPanel { 
    private final Rectangle s = new Rectangle(0, 0, 16, 16); 
    private void doDrawing(Graphics g) { 
    g.setColor(Color.RED); 
    g.fillRect(s.x, s.y, s.width, s.height); 

//  Graphics2D g2 = (Graphics2D) g; 
//  double x = getWidth()/2d; 
//  double y = getHeight()/2d; 
//  g2.translate(x, y); 
//  g2.setPaint(Color.BLUE); 
//  g2.fill(s); 
//  g2.translate(-x, -y); 

    Graphics2D g2 = (Graphics2D) g.create(); 
    g2.translate(getWidth()/2, getHeight()/2); 
    g2.setPaint(Color.BLUE); 
    g2.fill(s); 
    g2.dispose(); 
    } 

    @Override 
    public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    doDrawing(g); 
    } 
} 

public class Main2 extends JFrame { 
    public static void main(String[] args) { 
    EventQueue.invokeLater(() -> { 
     GPanel panel = new GPanel(); 
     panel.setBorder(BorderFactory.createTitledBorder("Title")); 
     JFrame ex = new JFrame(); 
     ex.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     ex.getContentPane().add(panel); 
     ex.setSize(400, 400); 
     ex.setLocationRelativeTo(null); 
     ex.setVisible(true); 
    }); 
    } 
} 
相關問題