Reputation: 500
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.
Upvotes: 1
Views: 182
Reputation: 205805
If jfreechchart is acceptable, a PiePlot
may serve. Starting from PieChartDemo1
, included in the distribution, the following changes produce the plot shown below. A ChartMouseListener
highlights the indicated section as the mouse is moved.
public static JPanel createDemoPanel() {
…
PiePlot plot = (PiePlot) chart.getPlot();
PieDataset data = plot.getDataset();
panel.addChartMouseListener(new ChartMouseListener() {
@Override
public void chartMouseMoved(ChartMouseEvent e) {
ChartEntity ce = e.getEntity();
if (ce instanceof PieSectionEntity) {
for (int i = 0; i < data.getItemCount(); i++) {
plot.setExplodePercent(data.getKey(i), 0);
}
PieSectionEntity item = (PieSectionEntity) ce;
plot.setExplodePercent(item.getSectionKey(), 0.25);
}
}
@Override
public void chartMouseClicked(ChartMouseEvent e) {}
});
return panel;
}
Upvotes: 3
Reputation: 347244
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)
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
Upvotes: 3