我目前正在使用Java Swing。我似乎无法找到这两个弧重叠的任何可能原因。是否有任何阻止它们重叠的建议?
protected void paintComponent (Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(50));
int start = 0;
g2.setPaint(Color.GREEN);
g2.draw(new Arc2D.Double(100, 100, 300, 300, 0, 180, Arc2D.OPEN));
g2.setPaint(Color.BLUE);
g2.draw(new Arc2D.Double(100, 100, 300, 300, 180, 180, Arc2D.OPEN));
}
答案 0 :(得分:3)
这是因为装饰应用于子路径的末端。 ('结束')。
BasicStroke
的默认结束时间为CAP_SQUARE
。将其更改为CAP_BUTT
(即无装饰)并查看差异:
g2.setStroke(new BasicStroke(50, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
演示不同结束上限的可运行示例:
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.geom.Arc2D;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class Foo {
private int endCap = BasicStroke.CAP_SQUARE;
private JPanel graphicsPanel;
public void createAndShowGUI() {
graphicsPanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(50, getEndCap(), BasicStroke.JOIN_BEVEL));
g2.setPaint(Color.GREEN);
g2.draw(new Arc2D.Double(100, 100, 300, 300, 0, 180, Arc2D.OPEN));
g2.setPaint(Color.BLUE);
g2.draw(new Arc2D.Double(100, 100, 300, 300, 180, 180, Arc2D.OPEN));
}
};
graphicsPanel.setBackground(Color.WHITE);
JRadioButton buttButton = new JRadioButton("CAP_BUTT");
JRadioButton roundButton = new JRadioButton("CAP_ROUND");
JRadioButton squareButton = new JRadioButton("CAP_SQUARE");
addRadioButtonListener(buttButton, BasicStroke.CAP_BUTT);
addRadioButtonListener(roundButton, BasicStroke.CAP_ROUND);
addRadioButtonListener(squareButton, BasicStroke.CAP_SQUARE);
squareButton.doClick();
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(buttButton);
buttonGroup.add(roundButton);
buttonGroup.add(squareButton);
JPanel buttonPanel = new JPanel();
buttonPanel.add(buttButton);
buttonPanel.add(roundButton);
buttonPanel.add(squareButton);
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.add(buttonPanel, BorderLayout.NORTH);
contentPane.add(graphicsPanel);
JFrame frame = new JFrame("Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(contentPane);
frame.setSize(800, 600);
frame.setVisible(true);
}
private void addRadioButtonListener(JRadioButton button, int endCap) {
button.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
setEndCap(endCap);
graphicsPanel.repaint();
}
});
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> new Foo().createAndShowGUI());
}
public int getEndCap() {
return endCap;
}
public void setEndCap(int endCap) {
this.endCap = endCap;
}
}
顺便说一句,不要忘记在覆盖方法开始时拨打super.paintComponent(g)
。
答案 1 :(得分:1)
根据javadoc:
Arc2D.Double(double x, double y, double w, double h, double start, double extent, int type)
Constructs a new arc, initialized to the specified location, size, angular extents, and closure type.
您的x和y位置在两个通话中都是相同的。因此,您会看到重叠。为防止重叠,您需要为x和y提供不同的位置。