More programming with colour buttons.

William Overington

Copyright 2001 William Overington

Wednesday 11 July 2001

In the document Programming with colour buttons. a demonstration program was introduced.

It was mentioned that an interesting exercise is to try to obtain a display of a magenta field with an orange square in the bottom right hand corner by clicking on the buttons.

This can be achieved by clicking the sequence blue, green, yellow.

Thinking about the program later, I realized that whereas there are sixteen permutations of magenta and orange squares that can be achieved with four locations for squares, only fourteen of them can be accessed from the program in that previous document. Two that are achievable are all orange and all magenta. Four that are achievable are one orange and three magenta, with the orange square in one of the four locations. Another four that are achievable are three orange and one magenta, with the magenta square in one of the four locations. That leaves six possibilities for two orange and two magenta. Of these, only four can be achieved from the program. The four that are achievable are two orange vertically at the left with two magenta vertically at the right, two orange vertically at the right with two magenta vertically at the left, two orange horizontally along the top with two magenta horizontally along the bottom, two orange horizontally along the bottom with two magenta horizontally along the top. The two that are not achievable are orange at top left and bottom right with magenta at bottom left and top right, orange at bottom left and top right with magenta at top left and bottom right.

I wondered how a modified program, using four buttons, could be used to produce these two additional possibilities as well.

I decided that two successive clicks on the yellow button should produce the one and two successive clicks on the blue button should produce the other.

I then considered that such a modified program would perform a useful purpose for these documents as it is a test of debouncing of the buttons. In this web based version there should be no problem, as two successive mouse clicks are needed. For a DVB-MHP version there should be no problem, as two successive pushes of the colour button on the hand held infra-red control device would be needed and buttons would need to be debounced. However, it is as well to check such matters and the modified program converted to run on a DVB-MHP television would be a useful check that debouncing of the colour buttons is taking place in both the television set and the hand held infra-red control device.

Accordingly, the modified version of the program has been developed.

I have used my own method for monitoring the button pushes in the Java program. This may possibly not be the best programming style, I am unsure on that matter. Nevertheless, this method does work and appears to be capable of further expansion.

While programming, I decided to add the feature of two successive pushes of the red button clearing the display.

Also, I have added a feature whereby two and three successive pushes of the green button are relevant, as an interesting piece of programming. Two successive clicks of the green button produce a chequered pattern of smaller orange and magenta squares and three successive clicks of the green button produce a chequered pattern of yet smaller orange and magenta squares.

Here is a link to viewing the web based simulation. A Java enabled browser is required.

The web based simulation of more programming with colour buttons.

Here is the source code of the Java applet.

import java.awt.*;
import java.awt.event.*;
 
public class ast00301 extends java.applet.Applet implements
                                             ActionListener
 
// Software written by William Overington
// WOverington@ngo.globalnet.co.uk
// 11 July 2001
// Copyright 2001 William Overington
//
 
  {
    int obeycode;
    int storedobeycode;
    Button colourButton1 = new Button("");
    Button colourButton2 = new Button("");
    Button colourButton3 = new Button("");
    Button colourButton4 = new Button("");
 
    public void init()
      {
        setLayout(null);
        setBounds(0,0,512,400);
        colourButton1.addActionListener(this);
        colourButton1.setLocation(50,350);
        colourButton1.setSize(25,25);
        colourButton1.setBackground(Color.red);
        add(colourButton1);
        colourButton2.addActionListener(this);
        colourButton2.setLocation(100,350);
        colourButton2.setSize(25,25);
        colourButton2.setBackground(Color.green);
        add(colourButton2);
        colourButton3.addActionListener(this);
        colourButton3.setLocation(150,350);
        colourButton3.setSize(25,25);
        colourButton3.setBackground(Color.yellow);
        add(colourButton3);
        colourButton4.addActionListener(this);
        colourButton4.setLocation(200,350);
        colourButton4.setSize(25,25);
        colourButton4.setBackground(Color.blue);
        add(colourButton4);
 
        setBackground(Color.black);
 
        obeycode=1;
        storedobeycode=1;
      }
 
    public void actionPerformed(ActionEvent event)
      {
        Object source = event.getSource();
 
        if (source == colourButton1)
          {
            if (storedobeycode == 51)
              {
                obeycode=1051;
              }
            else
              {
                obeycode=51;
              }
            repaint();
          }
 
        if (source == colourButton2)
          {
            if (storedobeycode == 52)
              {
                obeycode=1052;
              }
            else if (storedobeycode == 1052)
              {
                obeycode=2052;
              }
            else
              {
                obeycode=52;
              }
            repaint();
          }
 
        if (source == colourButton3)
          {
            if (storedobeycode == 53)
              {
                obeycode=1053;
              }
            else
              {
                obeycode=53;
              }
            repaint();
          }
 
        if (source == colourButton4)
          {
            if (storedobeycode == 54)
              {
                obeycode=1054;
              }
            else
              {
                obeycode=54;
              }
            repaint();
          }
      }
 
     public void paint(Graphics screen)
      {
 
        storedobeycode=obeycode;
 
        if (obeycode == 1)
          {
            screen.setColor(Color.white);
            screen.fillRect(0,289,512,112);
            screen.setColor(Color.gray);
            screen.fillRoundRect(0,325,512,75,20,20);
            screen.clipRect(0,0,512,288);
          }
        else if (obeycode == 51)
          {
            screen.setColor(Color.orange);
            screen.fillRect(156,44,100,200);
          }
        else if (obeycode == 1051)
          {
            screen.setColor(Color.black);
            screen.fillRect(156,44,200,200);
          }
        else if (obeycode == 52)
          {
            screen.setColor(Color.orange);
            screen.fillRect(256,44,100,200);
          }
        else if (obeycode == 1052)
          {
            for(int row=0;row < 4;row++)
              {
                for(int column=0;column < 4;column++)
                  {
                    int sum=row+column;
                    boolean even=((sum % 2) == 0);
                    if(even)
                      {
                        screen.setColor(Color.orange);
                      }
                    else
                      {
                        screen.setColor(Color.magenta);
                      }
                    screen.fillRect(156 + 50*column,
                                      44 + 50*row,
                                        50,50);
                  }
              }
          }
        else if (obeycode == 2052)
          {
            for(int row=0;row < 8;row++)
              {
                for(int column=0;column < 8;column++)
                  {
                    int sum=row+column;
                    boolean even=((sum % 2) == 0);
                    if(even)
                      {
                        screen.setColor(Color.orange);
                      }
                    else
                      {
                        screen.setColor(Color.magenta);
                      }
                    screen.fillRect(156 + 25*column,
                                      44 + 25*row,
                                        25,25);
                  }
              }
          }
        else if (obeycode == 53)
          {
            screen.setColor(Color.magenta);
            screen.fillRect(156,44,200,100);
          }
        else if (obeycode == 1053)
          {
            screen.setColor(Color.orange);
            screen.fillRect(156,44,100,100);
            screen.setColor(Color.magenta);
            screen.fillRect(256,44,100,100);
            screen.setColor(Color.magenta);
            screen.fillRect(156,144,100,100);
            screen.setColor(Color.orange);
            screen.fillRect(256,144,100,100);
          }
        else if (obeycode == 54)
          {
            screen.setColor(Color.magenta);
            screen.fillRect(156,144,200,100);
          }
        else if (obeycode == 1054)
          {
            screen.setColor(Color.magenta);
            screen.fillRect(156,44,100,100);
            screen.setColor(Color.orange);
            screen.fillRect(256,44,100,100);
            screen.setColor(Color.orange);
            screen.fillRect(156,144,100,100);
            screen.setColor(Color.magenta);
            screen.fillRect(256,144,100,100);
          }
        
        obeycode=1;      
      }
 
    public void update(Graphics screen)
      {
        paint(screen);
      }
 
  }

Please note the introduction of the variable named storedobeycode (that is "stored obeycode") and the fact that tests for the previous value are tested against the value stored in storedobeycode and not against the value stored in obeycode.

The reason for this, on the web, is that making obeycode have the value of 1 while the program is idle means that if the web page is scrolled and the program redraws the display, then the start up display is produced. This is because the display on the screen can, for some sequences of button pushes, be a result of more than one button push and redrawing the display based on using only the last used value of obeycode may not produce a correctly redrawn display: so a reset blank display is produced.

If this program is converted to run on a DVB-MHP platform, then no such scrolling might be possible, so the use of storedobeycode as an extra variable might not be needed. However, it does no harm to leave it in place and might be regarded as good software design, so I recommend leaving it as it is if this program is converted for use on a DVB-MHP terminal.

In this regard, it might be worth mentioning that the DVB-MHP system has, as well as a direct computing format, a method to display web format content, so it is possible that there are two possible methods of using the information structure of this program on a DVB-MHP terminal. The direct use will need some conversion. I am unsure at present whether the web format version would need any modification or whether, in fact, this very software could be used in a DVB-MHP web format situation.

An interesting exercise is to try to determine how many different displays may be produced by clicking various sequences of the buttons as now programmed in this modified program. I have found that this turns out to be quite a fascinating problem to investigate and an interesting puzzle.

The question that I still need to resolve is how to write a Java program that produces upon a DVB-MHP system the same graphic demonstration that appears in the upper panel of the simulation. However, learning how to convert the original program will help me to be able to convert this modified program. The two programs together might be a useful pair of test programs to run on a DVB-MHP terminal.

 

Astrolabe Channel

Copyright 2001 William Overington

This file is accessible as follows.

http://www.users.globalnet.co.uk/~ngo/ast00300.htm