Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 805497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:05:24+00:00 2026-05-15T00:05:24+00:00

I’m trying to make a custom swing control that is a meter. Swing Meter

  • 0

I’m trying to make a custom swing control that is a meter.
Swing Meter http://dl.dropbox.com/u/2363305/Programming/Java/swing_meter.gif

The arrow will move up and down. Here is my current code, but I feel I’ve done it wrong.

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LinearGradientPaint;
import java.awt.Polygon;
import java.awt.Stroke;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class meter extends JFrame {

Stroke drawingStroke = new BasicStroke(2);
Rectangle2D rect = new Rectangle2D.Double(105, 50, 40, 200);
Double meterPercent = new Double(0.57);

public meter() {

    setTitle("Meter");

    setLayout(null);

    setSize(300, 300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);

}

public void paint(Graphics g) {
    // Paint Meter
    Graphics2D g1 = (Graphics2D) g;
    g1.setStroke(drawingStroke);
    g1.draw(rect);

    // Set Meter Colors
    Point2D start = new Point2D.Float(0, 0);
    Point2D end = new Point2D.Float(0, this.getHeight());
    float[] dist = { 0.1f, 0.5f, 0.9f };
    Color[] colors = { Color.green, Color.yellow, Color.red };
    LinearGradientPaint p = new LinearGradientPaint(start, end, dist,
            colors);

    g1.setPaint(p);
    g1.fill(rect);

    // Make a triangle - Arrow on Meter
    int[] x = new int[3];
    int[] y = new int[3];
    int n; // count of points

    // Set Points for Arrow
    Integer meterArrowHypotenuse = (int) rect.getX();
    Integer meterArrowTip = (int) rect.getY()
            + (int) (rect.getHeight() * (1 - meterPercent));
    x[0] = meterArrowHypotenuse - 25;
    x[1] = meterArrowHypotenuse - 25;
    x[2] = meterArrowHypotenuse - 5;
    y[0] = meterArrowTip - 20; // Top Left
    y[1] = meterArrowTip + 20; // Bottom Left
    y[2] = meterArrowTip; // Tip of Arrow
    n = 3; // Number of points, 3 because its a triangle

    // Draw Arrow Border
    Polygon myTriShadow = new Polygon(x, y, n); // a triangle
    g1.setPaint(Color.black);
    g1.fill(myTriShadow);

    // Set Points for Arrow Board
    x[0] = x[0] + 1;
    x[1] = x[1] + 1;
    x[2] = x[2] - 2;
    y[0] = y[0] + 3;
    y[1] = y[1] - 3;
    y[2] = y[2];

    Robot robot = new Robot();

    Color colorMeter = robot.getPixelColor(x[2]+10, y[2]);

    // Draw Arrow
    Polygon myTri = new Polygon(x, y, n); // a triangle
    Color colr = new Color(colorMeter.getRed(), colorMeter.getGreen(), colorMeter.getBlue());
    g1.setPaint(colr);
    g1.fill(myTri);

}

public static void main(String[] args) {
    new meter();
}

}

Thanks for looking.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T00:05:25+00:00Added an answer on May 15, 2026 at 12:05 am

    In addition to @Jonas’ example, you might like to look at the article How to Write a Custom Swing Component.

    Addendum: On reflection, it looks a little intimidating, but you can extend BasicSliderUI and reuse some of your code in paintThumb() and paintTrack().

    JSlider slider = new JSlider();
    slider.setUI(new MySliderUI(slider));
    ...
    private static class MySliderUI extends BasicSliderUI {
    
        public MySliderUI(JSlider b) {
            super(b);
        }
    
        @Override
        public void paintTrack(Graphics g) {
            Graphics2D g2d = (Graphics2D) g;
            Rectangle r = trackRect;
            g2d.setPaint(new GradientPaint(
                r.x, r.y, Color.red, r.x + r.width, r.y + r.height, Color.blue));
            g.fillRect(r.x, r.y, r.width, r.height);
        }
    
        @Override
        public void paintThumb(Graphics g) {
            super.paintThumb(g); // replace with your fill() 
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.