Here's what i have to do, i have to write a billboard program where the message inside the box have to scroll from left to right. so...
•Add a run() method (with the standard animation loop), and an updateFrame() method.
•When you first position your label, position it off the right side of the screen. As you draw each frame of your animation, move it to the left by two pixels. When the entire label has scrolled off the screen to the left, position it back on the right-hand side again.
•Each time the updateFrame() method is called, simply change the position of the label. Do not create a new Glabel object.
here's 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.WHITE);
break;
case 7:
message.setColor(Color.PINK);
break;
}
}
if (clicked == show)
{
String text = type.getText();
message.setLabel(text);
}
}
}


Sign In
Create Account

Back to top









