English 中文(简体)
• 如何使半环绕边界?
原标题:How to make half rounded border?

我有一个传承JPanel的圆形班子,提供四舍五入的外观。

JPanel user_icon = new RoundPanel(8);
user_icon.setBackground(Color.decode("#363b41"));
user_icon.setBorder(null);
user_icon.setBounds(160, 135, 40, 46);
container.add(user_icon);
user_icon.setLayout(null);
public class RoundPanel extends JPanel {

    private static final long serialVersionUID = 1L;
    private Shape shape;
    
    public RoundPanel(int size) {
        setOpaque(false);
    }
    
    protected void paintComponent(Graphics g) {
         g.setColor(getBackground());
         g.fillRoundRect(0, 0, getWidth()-1, getHeight()-1, 8, 8);
         super.paintComponent(g);
    }
    
    protected void paintBorder(Graphics g) {
         g.drawRoundRect(0, 0, getWidth()-1, getHeight()-1, 8, 8);
    }
    
    public boolean contains(int x, int y) {
         if (shape == null || !shape.getBounds().equals(getBounds())) {
             shape = new RoundRectangle2D.Float(0, 0, getWidth()-1, getHeight()-1, 8, 8);
         }
         return shape.contains(x, y);
    }
}

想加以修改,只有左角四舍五入。 (未四舍五入)

问题回答

我强烈建议通过与Geometry的工作,实际上,我还建议用时间通过整个。 页: 1

任何一个例子/解决办法都不会达到100%的要求,你必须准备投入时间,尝试不同的解决办法,并根据你们的需要调整解决办法,这标志着一个良好的发展者。

因此,经过细微的实验(通过我先前的答复进行了许多阅读),我得以来到......。

enter image description here

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的曲线总是混淆我。

您还可以考虑:





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签