Simulating a seven segment display?

Caporegime
Joined
12 Mar 2004
Posts
29,753
Location
England
Is there any easy way to do this using the netbeans gui builder? I was thinking of using something like coloured boxes to simulate the segments.
 
Soldato
Joined
9 May 2005
Posts
4,524
Location
Nottingham
Yeah it's possible, I used it to simulate an 8x8 LED Matrix, I used two bitmap images, one for the LED on and one for the LED off. The actual GUI was a grid of ImageIcons if I remember correctly, I stored them in a 2D array but you will probably be fine with a simple array as your only using 7 elements (8 if you want a decimal point). Here is a pic of the program I made:

pat2.jpg


If you plan how it is going to work then it should be fairly easy to implement. For example do you want a method that takes an integer or character then turns on the correct segments? You will need to use an 'if' or 'case' statement to switch the correct segments on. You could always cheat and create images for every number then just swap the image to the correct one rather than playing around with segments (not feasible in my program having 64 LED's but if you only want 10 numbers then it is probably easier).
 
Last edited:
Caporegime
Joined
12 Mar 2004
Posts
29,753
Location
England
I've already got all the code done so that's not a problem.

I can't find an image icon object in the add from pallete menu though.
 
Last edited:
Soldato
Joined
9 May 2005
Posts
4,524
Location
Nottingham
Sorry, went to bed after replying last night, I'm still setting up Vista x64 with my Java development tools, I'll open my old project up and post some stuff to help you out in a bit.
 
Soldato
Joined
9 May 2005
Posts
4,524
Location
Nottingham
OK it's not quite as I said, I did use an ImageIcon but it is inside a JLabel (JLabels can display an ImageIcon). The best way to do this is to create a new class that extends JLabel called something like "Segment" in your case then pretty much use the same code as below, changing where necessary:

Code:
/*
 * LED.java
 *
 * Created on 02 September 2007, 22:49
 * 
 */

import javax.swing.ImageIcon;
import javax.swing.JLabel;

/**
 * @author Robert Hunt
 * @version 1.0
 */
public class LED extends JLabel {
    
    private boolean state;
    private ImageIcon onState;
    private ImageIcon offState;
    
    /** Creates a new instance of LED */
    public LED() {
        onState = new ImageIcon(LED.class.getResource("images/on.jpg"));
        offState = new ImageIcon(LED.class.getResource("images/off.jpg"));
        // Set the default state icon
        setIcon(offState);
    }
    
    public void setState(boolean state){
        if (state)
            setIcon(onState);
        else
            setIcon(offState);
        // Update this LEDs state
        this.state = state;
    }
    
    public boolean getState(){
        return state;
    }  
    
}

Once you have written your class, make sure you have compiled it and it has no errors. Next you need to go into the GUI builder and add a new bean:

bean.jpg


Type in the name of your class without the extension:

bean_name.jpg


You should then be able to place this on your GUI:

bean_place.jpg


You can then name it whatever and access it like any other GUI component:

Code:
myLED.setState(true);

For my class the images were located in the project folder at:

<PROJECT_NAME>\src\images\on.jpg
<PROJECT_NAME>\src\images\off.jpg

Hope this helps, feel free to post if you get stuck.

-Rob
 
Caporegime
Joined
12 Mar 2004
Posts
29,753
Location
England
What version of netbeans are you using? In 5.5.1 when I choose to add the bean to the jFrame I get the error message "Cannot load component class LED from project xxx, The class must be compiled and must be on the classpath of the project this form belongs to."
 
Soldato
Joined
9 May 2005
Posts
4,524
Location
Nottingham
I'm using Netbeans 6.0.1 but it should still work. I got that error when I forgot to compile the class before trying to add it as a bean (try right clicking the class and select "compile"). The class must be in the same project as your working in and make sure you got the name correct.
 
Associate
Joined
19 Oct 2002
Posts
550
Location
Penryn
I don't know if you're sorted or not, but I've just written you a SevenSegmentDisplay class.

Code:
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

/**
 *
 * @author Dfhaii
 */
public class SevenSegmentDisplay extends JPanel {
    private final Color mDisplayColor = Color.RED;
    private static final JPanel[] mSegments = new JPanel[7];
    private static final JPanel[] mSpacers = new JPanel[7];
    private volatile int mNumberDisplayed;
    private static final Dimension mHorizontalSize = new Dimension(40,10);
    private static final Dimension mVerticalSize = new Dimension(10,40);
    
    private static final int mDefaultNumber = 0;
    
    public SevenSegmentDisplay(){
        this(mDefaultNumber);
    }
    
    
    public SevenSegmentDisplay(int Number){
        super(new GridBagLayout());
        setBackground(null);
        setOpaque(false);
        for(int i = 0; i < mSegments.length; i++){
            mSegments[i] = new JPanel();
            mSegments[i].setBackground(mDisplayColor);
            mSegments[i].setOpaque(true);
            mSegments[i].setVisible(false);
            
            mSpacers[i] = new JPanel();
            mSpacers[i].setBackground(null);
            mSpacers[i].setOpaque(false);
            mSpacers[i].setVisible(false);
        }
        
        GridBagConstraints GBConst = new GridBagConstraints();
        GBConst.gridx = 1;
        GBConst.gridy = 0;
        GBConst.gridwidth = 1;
        GBConst.gridheight = 1;
        GBConst.fill = GridBagConstraints.NONE;
        GBConst.anchor = GridBagConstraints.CENTER;
        GBConst.weightx = 1;
        GBConst.weighty = 1;
        SetRigidSize(mSegments[0], mHorizontalSize);
        SetRigidSize(mSpacers[0], mHorizontalSize);
        add(mSegments[0], GBConst);
        add(mSpacers[0], GBConst);
        
        GBConst.gridx = 0;
        GBConst.gridy = 1;
        SetRigidSize(mSegments[1], mVerticalSize);
        SetRigidSize(mSpacers[1], mVerticalSize);
        add(mSegments[1], GBConst);
        add(mSpacers[1], GBConst);
        
        GBConst.gridx = 2;
        SetRigidSize(mSegments[2], mVerticalSize);
        SetRigidSize(mSpacers[2], mVerticalSize);
        add(mSegments[2], GBConst);
        add(mSpacers[2], GBConst);
        
        GBConst.gridx = 1;
        GBConst.gridy = 2;
        SetRigidSize(mSegments[3], mHorizontalSize);
        SetRigidSize(mSpacers[3], mHorizontalSize);
        add(mSegments[3], GBConst);
        add(mSpacers[3], GBConst);
        
        GBConst.gridx = 0;
        GBConst.gridy = 3;
        SetRigidSize(mSegments[4], mVerticalSize);
        SetRigidSize(mSpacers[4], mVerticalSize);
        add(mSegments[4], GBConst);
        add(mSpacers[4], GBConst);
        
        GBConst.gridx = 2;
        SetRigidSize(mSegments[5], mVerticalSize);
        SetRigidSize(mSpacers[5], mVerticalSize);
        add(mSegments[5], GBConst);
        add(mSpacers[5], GBConst);
        
        GBConst.gridx = 1;
        GBConst.gridy = 4;
        SetRigidSize(mSegments[6], mHorizontalSize);
        SetRigidSize(mSpacers[6], mHorizontalSize);
        add(mSegments[6], GBConst);
        add(mSpacers[6], GBConst);
        
        SetNumber(Number);
    }
     
    private void SetRigidSize(Component toSize, Dimension size){
        toSize.setMinimumSize(size);
        toSize.setMaximumSize(size);
        toSize.setPreferredSize(size);
    }
    
    public void SetNumber(final int Number){
        if(Number < 0 || Number > 9)
            throw new IllegalArgumentException("It's a single display, use a valid number numbnuts");
        mNumberDisplayed = Number;
        Runnable DisplayUpdate = new Runnable(){
            public void run(){
                for(int i = 0; i < mSegments.length; i++){
                            mSegments[i].setVisible(false);
                        }
                switch(Number){
                    case 0:
                        for(int i = 0; i < mSegments.length; i++){
                            mSegments[i].setVisible(true);
                        }
                        mSegments[3].setVisible(false);
                        break;
                    case 1:
                        mSegments[2].setVisible(true);
                        mSegments[5].setVisible(true);
                        break;
                    case 2:
                        mSegments[0].setVisible(true);
                        mSegments[2].setVisible(true);
                        mSegments[3].setVisible(true);
                        mSegments[4].setVisible(true);
                        mSegments[6].setVisible(true);
                        break;
                    case 3:
                        mSegments[0].setVisible(true);
                        mSegments[2].setVisible(true);
                        mSegments[3].setVisible(true);
                        mSegments[5].setVisible(true);
                        mSegments[6].setVisible(true);
                        break;
                    case 4:
                        mSegments[1].setVisible(true);
                        mSegments[2].setVisible(true);
                        mSegments[3].setVisible(true);
                        mSegments[5].setVisible(true);
                        break;

                    case 5:
                        mSegments[0].setVisible(true);
                        mSegments[1].setVisible(true);
                        mSegments[3].setVisible(true);
                        mSegments[5].setVisible(true);
                        mSegments[6].setVisible(true);
                        break;
                    case 6:
                        mSegments[0].setVisible(true);
                        mSegments[1].setVisible(true);
                        mSegments[3].setVisible(true);
                        mSegments[4].setVisible(true);
                        mSegments[5].setVisible(true);
                        mSegments[6].setVisible(true);
                        break;
                    case 7:
                        mSegments[0].setVisible(true);
                        mSegments[2].setVisible(true);
                        mSegments[5].setVisible(true);
                        break;
                    case 8:
                        for(int i = 0; i < mSegments.length; i++){
                            mSegments[i].setVisible(true);
                        }
                        break;
                    case 9:
                        mSegments[0].setVisible(true);
                        mSegments[1].setVisible(true);
                        mSegments[2].setVisible(true);
                        mSegments[3].setVisible(true);
                        mSegments[5].setVisible(true);
                        break;
                }
                for(int i = 0; i < mSpacers.length; i++){
                            mSpacers[i].setVisible(!mSegments[i].isVisible());
                }
            }
        };
        
        if(SwingUtilities.isEventDispatchThread())
            DisplayUpdate.run();
        else
            SwingUtilities.invokeLater(DisplayUpdate);
    }
    
    public int GetDisplayedNumber(){
        return mNumberDisplayed;
    }
    
}

Which looks like
7seg.png
(I created multiple and added to the panel, 1 object per number)

Its usage is fairly simple to understand.
 
Top