gpt4 book ai didi

java - 为什么这两个弧重叠?

转载 作者:行者123 更新时间:2023-12-01 18:03:23 25 4
gpt4 key购买 nike

我目前正在使用 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));
}

最佳答案

这是因为子路径末尾应用了装饰。 (“端盖”)。

BasicStroke 的默认端盖是 CAP_SQUARE 。将其更改为CAP_BUTT (即没有装饰)并查看差异:

g2.setStroke(new BasicStroke(50, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));

CAP_SQUARE(默认)

![enter image description here

CAP_BUTT

![enter image description here

<小时/>

演示不同端盖的可运行示例:

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)

关于java - 为什么这两个弧重叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38925329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com