I have a java program that dispays the temperature in digit format ie 87.7F. I would like to make a thermometer using graphics to do this. I have only been using java for 2 weeks now and just wonding if someone could give me pointers on the best way to accomplish this.

Dani AI

Generated

A compact, practical path for a beginner: build a small custom Swing panel that paints a tube + bulb and maps the numeric temperature to a fill fraction. This follows 's suggestion to use a custom JPanel and 's advice about custom painting; 's JFreeChart idea is a good library alternative if a ready-made gauge is preferred.

import javax.swing.*;
import java.awt.*;
import java.text.DecimalFormat;

public class ThermometerPanel extends JPanel {
    private double temp = 72.0;
    private double min = -20.0, max = 120.0;

    public ThermometerPanel() {
        setPreferredSize(new Dimension(80, 300));
        setOpaque(true);
    }

    public void setTemperature(final double t) {
        SwingUtilities.invokeLater(() -> {
            temp = Math.max(min, Math.min(max, t));
            repaint();
        });
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);

        int w = getWidth(), h = getHeight();
        int tubeW = Math.max(10, w/5);
        int bulbR = tubeW * 2;
        int tubeX = (w - tubeW) / 2;
        int tubeTop = 10;
        int bulbY = h - bulbR - 10;
        int tubeHeight = bulbY - tubeTop;

        double frac = (temp - min) / (max - min);
        frac = Math.max(0, Math.min(1, frac));
        int fillH = (int)(frac * tubeHeight);
        int fillY = bulbY - fillH;

        g2.setColor(new Color(0xDDDDDD));
        g2.fillRoundRect(tubeX, tubeTop, tubeW, tubeHeight, tubeW, tubeW);

        g2.setColor(Color.RED);
        g2.fillRect(tubeX+2, fillY, tubeW-4, bulbY - fillY);
        g2.fillOval((w - bulbR)/2, bulbY, bulbR, bulbR);

        g2.setColor(Color.DARK_GRAY);
        g2.drawRoundRect(tubeX, tubeTop, tubeW, tubeHeight, tubeW, tubeW);
        g2.drawString(new DecimalFormat("#0.0").format(temp) + "F", 5, 15);

        g2.dispose();
    }
}

Notes and quick troubleshooting: always call super.paintComponent(g) and keep heavy computation out of paintComponent. Update the temperature on the Event Dispatch Thread (shown above) and use a Swing Timer to animate smooth rises/falls (increment toward the target and call repaint()). Clamp values to min/max before mapping to pixels. If nothing appears, ensure the panel is added to a packed JFrame and GUI creation runs inside SwingUtilities.invokeLater. For a faster route, use a JProgressBar or a chart/gauge library; for modern UIs, JavaFX provides built-in shapes and animations.

Recommended Answers

All 6 Replies

make your class extend JFrame , then create a jpanel , that will be your own custom Contentpane , which you can add to the JFrame ,
then inside the contenpane , have two textfeilds , and a button in between them , 1st one takes in input , and on pressing the button , the output is shown on the 2nd text feild. and regarding the decimal point part , use a double instead of a an int.

this is a good place to get started with swing components.
if you have the head first java , book , i learned basic gui from there , found it quite helpful... maybe you can try that out too.

note : youll find the reason i suggested using your own jpanel contentpane within the first few pages of the link above. :) see if you can dig it out.

use JFreeCharts, there is termomether as pre_defined graph (in version that I tried), if not then is so easy to create Shapes with rainbows colors, toggle with value

that would be a good way to solve it. however , as OP says , in his 2ndweek only... maybe he should practise up some of the basic gui before going there.

First, let's assume you know some basic graphics stuff - how to create and display a window (if not you need to study a tutorial on that first).

Then look through this tutorial on custom painting in Swing for the basic info you will need.
Using that you can draw your thermometer as a rectangle whose length is defined by the temperature. You can decorate that with colors, text and numbers etc as you wish.

However, you may be able to get something close enough by using a simple JProgressBar

commented: +1 for the JProgressBar idea. +3
commented: :) +0

hi friends can you tell me who we can make this software please help me for making this software please

Hello iqraw kaleem

There are people here who can help, but you must ask specific questions. Eg What software do you want to make? What do you know already? What exactly is stopping you from going further?

Please also note the DaniWeb Rules, including "Do not hijack old forum threads by posting a new question as a reply to an old one". Start a new thread for your question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.