选择圆形段(Selecting Circle Segements)

编程入门 行业动态 更新时间:2024-10-27 08:32:50
选择圆形段(Selecting Circle Segements)

我使用java在窗口中创建一个轮子/拨号选择器,它会在按钮上弹出按钮,让用户选择不同的片段之一来执行之前创建该片段时所指定的内容。

我被困在如何选择一个圆的不同部分,目前我使用自定义布局创建了围绕圆创建的JLabels,但它们的交互区域仅限于文本周围的矩形。

我如何将此区域更改为三角形/ pi片段?

有没有更好的方法来实现这一目标?

这是一幅图像,其中第4部分具有我想要实现的区域(用蓝色突出显示)。

I am creating a wheel / dial selector in windows using java, that would popup on a button press for the user to select one of the different segments to do what ever they have specified previously on creation of that segment.

I am stuck with how to select different segments of a circle, currently I have JLabels created around the circle using a custom layout but their interactive area is limited to a rectangle around the text.

How can I change this area to a triangle / pi segment?

Is there a better way to achieve this?

Here is an image where segment 4 has the area (highlighted in blue) that I want to achieve.

最满意答案

说实话,有很多方法可以做到这一点。 我能想到的最简单的方法之一就是利用2D图形Shape API。

为简单起见,以下示例简单地使用了Arc2D ,但一般概念也适用于基于Path的形状(如果您想使这种复杂)

高亮段

import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.geom.Arc2D; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class Test { public static void main(String[] args) { new Test(); } public Test() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new TestPane()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class TestPane extends JPanel { private Arc2D segment; private Arc2D selected = null; public TestPane() { segment = new Arc2D.Double(0, 0, 190, 190, -11.75, 23.5, Arc2D.PIE); addMouseMotionListener(new MouseAdapter() { @Override public void mouseMoved(MouseEvent e) { Point p = e.getPoint(); p.translate(-5, -5); selected = null; if (segment.contains(e.getPoint())) { selected = segment; } repaint(); } }); } @Override public Dimension getPreferredSize() { return new Dimension(200, 200); } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); g2d.translate(5, 5); if (selected != null) { g2d.setColor(Color.BLUE); g2d.fill(selected); } g2d.setColor(Color.RED); g2d.draw(segment); g2d.dispose(); } } }

查看有关使用几何体的更多详细信息

To be honest, there are any number of ways you might do this. One of the simplest ways I can think of is to take advantage of the 2D Graphics Shape API.

The following example simply uses a Arc2D for simplicity, but the general concept should work for Path based shapes as well (if you want to get that complicated)

Highlight segment

import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.geom.Arc2D; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class Test { public static void main(String[] args) { new Test(); } public Test() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new TestPane()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class TestPane extends JPanel { private Arc2D segment; private Arc2D selected = null; public TestPane() { segment = new Arc2D.Double(0, 0, 190, 190, -11.75, 23.5, Arc2D.PIE); addMouseMotionListener(new MouseAdapter() { @Override public void mouseMoved(MouseEvent e) { Point p = e.getPoint(); p.translate(-5, -5); selected = null; if (segment.contains(e.getPoint())) { selected = segment; } repaint(); } }); } @Override public Dimension getPreferredSize() { return new Dimension(200, 200); } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); g2d.translate(5, 5); if (selected != null) { g2d.setColor(Color.BLUE); g2d.fill(selected); } g2d.setColor(Color.RED); g2d.draw(segment); g2d.dispose(); } } }

Have a look at Working with Geometry for more details

更多推荐

本文发布于:2023-08-05 05:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1428436.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:圆形   Selecting   Segements   Circle

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!