当使用translate()方法时,JPanel中的TitledBorder会移动

时间:2017-05-26 08:13:03

标签: java swing jframe jpanel border

我创建了JPanel并将其添加到JFrameJPanelTitledBorder,但当我使用方法translate()时,例如,

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);
    }
}

3 个答案:

答案 0 :(得分:3)

您已在GPanel的实例上调用了paintComponent(),然后在JComponent的后一种实现中修改了图形上下文的转换。边界对此并不了解,因此显示的结果完全是预期的。相反,请遵循post的父JPanel实施建议:"将该组件放入JPanel并在bPanel上设置边框。"相关示例显示为setBorder()

在下面的变体中,

  • 封闭的Border现在有gPanel,而随附的translate()可以安全地操纵图形上下文。

  • translate()之前,红点以原点为中心;在setPreferredSize()之后,蓝点以原点为中心,但原点已移动。

  • 如果您真的要覆盖here,请不要使用JFrame

  • 不要不必要地延长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); } } }

getPreferredSize()

wget ftp://ftp.pbone.net/mirror/download.fedora.redhat.com/pub/fedora/epel/7/x86_64/b/byobu-5.73-4.el7.noarch.rpm
sudo yum install byobu-5.73-4.el7.noarch.rpm

答案 1 :(得分: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);
    });
  }
}

答案 2 :(得分:0)

试试这段代码:

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