Drawing Arc in Java
I need to draw a Pie Arc in Java with start angle 350 and end angle 20.The cordina开发者_高级运维te system I follow is as follows:-
|0
|
270-----------90
|
|180
The problem here is that the start angle is greater than the end angle.For the other way round I have managed to draw the arc.Any help would be great.
You will have a start angle and an 'extent' angle and not an end angle. So, I don't think you would be having problem drawing an arc.
import java.awt.Graphics;
import javax.swing.JFrame;
public class Test extends JFrame{
public static void main(String[] args){
new Test();
}
public Test(){
this.setSize(400,400);
this.setVisible(true);
}
public void paint(Graphics g) {
g.fillArc(100, 100, 100, 100, 70, 30);
}
}
Alternatively, you can use the Arc2D class as well. One more thing to note that in java, this is the default co-ordinate mechanism.
|90
|
180-----------0
|
|270
Use (450 - angle) % 360 to switch angles. Concept 450 = 180 + 270;
Extending on what @bragbog 's working code, I had to navigate through a similar situation where I had to transpose the co-ordinate system similar to that of the OP to the Java co-ordinate system.
This is what I came up with:
float coordChangeOffset = ((arcDegree % 180) - 45) * 2;
filterPanel.setArc(absModAngle(arcDegree - coordChangeOffset), 360 - sectorAngle);
private float absModAngle(float deg) {
return modAngle((deg + 360));
}
public class FilterPanel extends JPanel {
private final int x, y, w, h;
private int startAngle, arcFill;
public FilterPanel(int x, int y, int w, int h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
setBackground(UiColorPalette.TRANSPARENT);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) this.getGraphics();
g2d.setColor(UiColorPalette.FILTER_FILL);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.fillArc(x, y, w, h, startAngle, arcFill);
}
void setArc(float startAngle, float arcFill) {
this.startAngle = (int) startAngle;
this.arcFill = (int) arcFill;
System.err.out("Java Coordinate System - StartAngle: " + startAngle + ", arcFill: " + arcFill);
}
}
It may be confusing, but the Java system and the system I was working with, had 45 and 225 remain the same so the transpose being the systems are flipped on it's slope (where 45 and 225 have the same angle from either axis)
The absModAngle ensures my resulting angle is within my [0 - 360) range.
I created an additional image, but I don't have enough rep to add it. Essetentially
y = x - F(x), where F(x) is coordChangeOffset noted above ((x Mod 180) - 45) * 2
精彩评论