Fill part of the circle with color

Posted on 2008-11-19
Last Modified: 2013-12-22
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);






	private void paintTheBackground( Graphics g) {


		g.fillRect(0, 0, Size.width, Size.height);


		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.drawString("E", centerX - 45, centerY + 55);

		g.setColor( Color.yellow);

		g.drawString("1/2", centerX - 5, centerY - 50);


		g.drawString("F", centerX + 35, centerY + 55);




	public void paintComponent(Graphics g)


		Color oldColor;

		oldColor = g.getColor();

		paintTheBackground( g);



		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.drawString("FUEL", centerX - 10, centerY + 30);




	public static void main (String[] args)


		JFrame f = new JFrame();

		JPanel panel = new FuelGaugeDisplay(new Dimension(200,200));







Open in new window

Question by:selina3110
    LVL 13

    Accepted Solution

    You can use g.fillArc for that:
            g.fillArc(10, 10, (int) gaugeWidth - 20, (int) gaugeHeight - 20,90,135);

    Open in new window


    Author Comment

    Thanks! that's what im looking for
    LVL 2

    Expert Comment

    why not use jFreechart? They have gauges that are ready to use.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    Video by: Michael
    Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now