Colors - if (source == backButton) //if backButton did, do...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
/* Author: Eric Adlam * Section: D5 * Instructor: Coach * Project Name: Hello World * Assignment: 3 * Part: 2 * Due Date: 9/11/09 * * Description: Java Applet which displays the * text "Goodbye world! Hello Java!". * Simple controls allow the text and * background color to change. */ package week2; p // My First Applet. Watch the colors change as you click the button / import java.applet.*; import java.awt.*; import java.awt.event.*; i public class Colors extends Applet implements ActionListener { int bgCode = 0, txtCode = 0; t Button backButton, textButton; Font f = new Font("Helvetica", Font.BOLD, 18); F public void init() { backButton = new Button("Background Color"); add(backButton); backButton.addActionListener(this); b textButton = new Button("Text Color"); add(textButton); textButton.addActionListener(this); } public void actionPerformed(ActionEvent e) { Object source = e.getSource(); //Who generated the event?
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: if (source == backButton) //if backButton did, do this { bgCode = ++bgCode % 5; switch(bgCode) { case 0: setBackground(Color.cyan); break; case 1: setBackground(Color.orange); break; case 2: setBackground(Color.red); break; case 3: setBackground(Color.green); break; case 4: setBackground(Color.white); break; } } else if(source == textButton) //if textButton did, do this { txtCode = ++txtCode % 5; repaint(); //Force a call to paint, so the new colors show up } } public void paint(Graphics g) { switch(txtCode) { case 0: g.setColor(Color.blue); break; case 1: g.setColor(Color.magenta); break; case 2: g.setColor(Color.black); break; case 3: g.setColor(Color.pink); break; case 4: g.setColor(Color.yellow); break; } g.setFont(f); g.drawString("Coach", 30, 120); } }...
View Full Document

Page1 / 2

Colors - if (source == backButton) //if backButton did, do...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online