Fill part of the circle with color

Hi experts, i'm creating a fuel-like gauge. How do i fill part of the circle with color? I wanted to fill from 'E' till '1/2' is red color.

Attached is my codes.
import java.awt.*;
import javax.swing.*;
 
public class FuelGaugeDisplay extends JPanel 	
{
	int targetValue = 0;
	int value 		= 0;
	int degreesPerSecond = 1;
	Dimension Size;
	double gaugeWidth;
	double gaugeHeight;
	int    centerX = (int)(gaugeWidth/2.0);
	int    centerY = (int)(gaugeHeight/2.0);
	double zeroAngle = 225.0;
	double maxAngle  = -45; 
	double range = zeroAngle - maxAngle;
	
	public FuelGaugeDisplay (Dimension size) 
	{
		Size = size;
		gaugeWidth 	= Size.width  * 0.75;
		gaugeHeight = Size.height * 0.75;
		centerX = (int)(gaugeWidth/2.0);
		centerY = (int)(gaugeHeight/2.0);
		setSize(Size);
		setMaximumSize(Size);
		setPreferredSize(Size);
	}
	
	private void paintTheBackground( Graphics g) {
		g.setColor(Color.black);
		g.fillRect(0, 0, Size.width, Size.height);
		g.setColor(Color.black);
		g.fillOval(0, 0, (int)gaugeWidth, (int)gaugeHeight);
		// now the lines and the arcs on the gauge
		g.setColor( Color.lightGray);
		g.drawLine(centerX, centerY, (int) gaugeWidth -25, (int)gaugeHeight-25);
		g.drawLine(centerX, centerY, 23, (int)gaugeHeight-25);
		g.setColor( Color.white);
		g.drawArc( 10, 10, (int)gaugeWidth-20, (int)gaugeHeight-20, -45, 270);
		g.setColor( Color.red);
		g.drawString("E", centerX - 45, centerY + 55);
		g.setColor( Color.yellow);
		g.drawString("1/2", centerX - 5, centerY - 50);
		g.setColor( Color.green);
		g.drawString("F", centerX + 35, centerY + 55);
		
	}	
	
	public void paintComponent(Graphics g)
	{
		Color oldColor;
		oldColor = g.getColor();
		paintTheBackground( g);
 
		g.setColor(Color.orange);
		int x1 = centerX, 	
		    x2 = x1, 
		    y1 = centerY, 	
		    y2 = y1;
		double angleToUse = 360.0;
	    x2 += (int)( Math.cos(Math.toRadians(angleToUse))*centerX-10);
	    y2 -= (int)( Math.sin(Math.toRadians(angleToUse))*centerY-10);
		g.drawLine(x1, y1, x2, y2 );
		g.setColor(Color.white);
		g.drawString("FUEL", centerX - 10, centerY + 30);
		g.setColor(oldColor);
	}
	
	public static void main (String[] args)
	{
		JFrame f = new JFrame();
		JPanel panel = new FuelGaugeDisplay(new Dimension(200,200));
		f.setSize(200,200);
		f.add(panel);
		f.setVisible(true);
	}
	
 
}

Open in new window

selina3110Asked:
Who is Participating?
 
Bart CremersConnect With a Mentor Java ArchitectCommented:
You can use g.fillArc for that:
        g.setColor(Color.RED);
        g.fillArc(10, 10, (int) gaugeWidth - 20, (int) gaugeHeight - 20,90,135);

Open in new window

0
 
selina3110Author Commented:
Thanks! that's what im looking for
0
 
evil_lady_DiaCommented:
why not use jFreechart? They have gauges that are ready to use.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.