?
Solved

Fill part of the circle with color

Posted on 2008-11-19
3
Medium Priority
?
1,073 Views
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);
		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

0
Comment
Question by:selina3110
3 Comments
 
LVL 13

Accepted Solution

by:
Bart Cremers earned 200 total points
ID: 23001794
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
 

Author Comment

by:selina3110
ID: 23002975
Thanks! that's what im looking for
0
 
LVL 2

Expert Comment

by:evil_lady_Dia
ID: 23488676
why not use jFreechart? They have gauges that are ready to use.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month15 days, 20 hours left to enroll

850 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