我强烈建议通过与Geometry的工作,实际上,我还建议用时间通过整个。 页: 1
任何一个例子/解决办法都不会达到100%的要求,你必须准备投入时间,尝试不同的解决办法,并根据你们的需要调整解决办法,这标志着一个良好的发展者。
因此,经过细微的实验(通过我先前的答复进行了许多阅读),我得以来到......。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Path2D;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Test");
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
protected class TestPane extends JPanel {
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
g2d.setColor(Color.RED);
RoundedShape shape = new RoundedShape(
20,
100, 100,
RoundedShape.Corner.TOP_LEFT,
RoundedShape.Corner.TOP_RIGHT,
RoundedShape.Corner.BOTTOM_RIGHT,
RoundedShape.Corner.BOTTOM_LEFT
);
g2d.translate(10, 10);
g2d.draw(shape);
g2d.dispose();
}
}
public class RoundedShape extends Path2D.Double {
enum Corner {
TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT;
}
private double radius = 20;
private Set<Corner> corners;
private Dimension size;
public RoundedShape(double radius, int width, int height, Corner... corners) {
this.corners = new HashSet(Arrays.asList(corners));
this.radius = radius;
this.size = new Dimension(width, height);
build();
}
protected void build() {
moveTo(0, 0);
if (corners.contains(Corner.TOP_LEFT)) {
moveTo(0, radius);
curveTo(0, radius, 0, 0, radius, 0);
}
if (corners.contains(Corner.TOP_RIGHT)) {
lineTo(size.width - radius, 0);
// Draw the curve here
curveTo(size.width - radius, 0,
size.height, 0,
size.width, radius);
} else {
lineTo(size.width, 0);
}
if (corners.contains(Corner.BOTTOM_RIGHT)) {
lineTo(size.width, size.height - radius);
// Draw the curve here
curveTo(size.width, size.height - radius,
size.height, size.width,
size.width - radius, size.height);
} else {
lineTo(size.width, size.height);
}
if (corners.contains(Corner.BOTTOM_LEFT)) {
lineTo(radius, size.height);
// Draw the curve here
curveTo(radius, size.height,
0, size.height,
0, size.height - radius);
} else {
lineTo(0, size.height);
}
closePath();
}
}
}
我建议查看“利用Mouse drag在贾瓦提炼一个曲线”,因为与Bézier的曲线总是混淆我。
您还可以考虑: