Jump to content

Need Help making a simple Billboard

- - - - -

  • Please log in to reply
2 replies to this topic

#1
heartybowl

heartybowl

    Newbie

  • Members
  • PipPip
  • 17 posts
I have the basic structure done. I just need help making the phrase change colors every
time someone clicks the "color" button. someone please help me.

here is my code:

import acm.program.*;

import acm.graphics.*;

import java.awt.*;

import java.util.Random;

import javax.swing.*;

import java.awt.event.*;

/**

 * Write a one-sentence summary of your program here.

 * Follow that with additional details about its purpose,

 * what it represents, and how to use it.

 * 

 * @author (place your name here)

 * @version (place the date here)

 */

public class BillBoard extends GraphicsProgram

{

    public static final int APPLICATION_WIDTH = 600;

    public static final int APPLICATION_HEIGHT = 200;

    private boolean done = false;

    private JButton show, color;

    private GLabel message;

    private JTextField type;

    

    public void init()

    {

        show = new JButton("Show It!");

        color = new JButton("Color");

        type = new JTextField(20);

        setBackground(Color.BLACK);

                

        message = new GLabel("Enter Message");

        Font a = new Font("ARIAL", Font.BOLD, 48);

        message.setFont(a);

        double width = message.getWidth();

        double height = message.getHeight();

        

        message.setColor(Color.RED);

        add(message, 300 - width / 2, 100 - height / 4);

        add(show, SOUTH);

        add(type, SOUTH);

        add(color, SOUTH);

        

        addActionListeners();


    }

    

    public void run()

    {

        final int FRAME_RATE = 1000 / 30;

       

        do

        {

            updateFrame();

            pause(FRAME_RATE);

        } while (! done);

    }

    

    public void updateFrame()

    {

        // I have no idea what to put here.

    }


    public void actionPerformed(ActionEvent e)

    {

        Object clicked = e.getSource();

       

        if (clicked == color)

        {

            int a = (int)Math.random() * 7;

            switch (a)

            {

                case 1:

                    message.setColor(Color.RED);

                    break;

                case 2:

                    message.setColor(Color.ORANGE);

                    break;

                case 3:

                    message.setColor(Color.YELLOW);

                    break;

                case 4:

                    message.setColor(Color.GREEN);

                    break;

                case 5:

                    message.setColor(Color.BLUE);

                    break;

                case 6:

                    message.setColor(Color.BLACK);

                    break;

                case 7:

                    message.setColor(Color.PINK);

                    break;

            }

                

        }

    }

}



#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
add extra braces for your random:

int a = (int) (Math.random() * 7);

+ This will generate a number from 0 to 6, not 1 to 7.

#3
kaila2000

kaila2000

    Newbie

  • Members
  • Pip
  • 3 posts
I noticed that you didn't have any ActionEvent for your 'show' button, so here's what you do for the 'show' button:
Code:
if (clicked == show)

        {

            String text = type.getText();

            message.setLabel(text);

        }





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users