Intro to Java. Write a Java application that can display a moving banner across
ID: 3835133 • Letter: I
Question
Intro to Java.
Write a Java application that can display a moving banner across a window. There will be a text field the user can enter the content of the marques and OK button will send the entered content to display. There will be also a color chooser the user can change the color of the marques, and a combo box or list the user can select the size of fonts for the marques (at least 5 different fonts ranged from 14 to 22). It’s up to you to decide which GUI components you choose to accomplish the color chooser. You may consider using a combo box or list with at least 5 colors to be chosen if you feel difficulty to use JColorChooser. Finally, there will be another button to exit the execution of the program
Explanation / Answer
please check with the following code :
rgb chooser
import java.awt.*;
import java.awt.event.*;
public class colorchooser extends Panel implements AdjustmentListener {
private Scrollbar redScroll, greenScroll, blueScroll; // Scroll bars.
private Label redLabel, greenLabel, blueLabel; // For displaying RGB values.
private Canvas colorCanvas; // Color patch for displaying the color.
public colorchooser() { // Constructor.
/* Create Scrollbars with possible values from 0 to 255. */
redScroll = new Scrollbar(Scrollbar.HORIZONTAL, 0, 10, 0, 265);
greenScroll = new Scrollbar(Scrollbar.HORIZONTAL, 0, 10, 0, 265);
blueScroll = new Scrollbar(Scrollbar.HORIZONTAL, 0, 10, 0, 265);
/* Create Labels showing current RGB and HSB values. */
redLabel = new Label(" R = 0");
greenLabel = new Label(" G = 0");
blueLabel = new Label(" B = 0");
/* Set background colors for Scrollbars and Labels, so they don't
inherit the gray background of the applet. */
redScroll.setBackground(Color.lightGray);
greenScroll.setBackground(Color.lightGray);
blueScroll.setBackground(Color.lightGray);
redLabel.setBackground(Color.white);
greenLabel.setBackground(Color.white);
blueLabel.setBackground(Color.white);
/* Set the panel to listen for changes to the Scrollbars' values */
redScroll.addAdjustmentListener(this);
greenScroll.addAdjustmentListener(this);
blueScroll.addAdjustmentListener(this);
/* Create a canvas whose background color will always be set to the
currently selected color. */
colorCanvas = new Canvas();
colorCanvas.setBackground(Color.black);
/* Create the applet layout, which consists of a row of
three equal-sized regions holding the Scrollbars,
the Labels, and the color patch. The background color
of the applet is gray, which will show around the edges
and between components. */
setLayout(new GridLayout(1,3,3,3));
setBackground(Color.gray);
Panel scrolls = new Panel();
Panel labels = new Panel();
add(scrolls);
add(labels);
add(colorCanvas);
/* Add the Scrollbars and the Labels to their respective panels. */
scrolls.setLayout(new GridLayout(3,1,2,2));
scrolls.add(redScroll);
scrolls.add(greenScroll);
scrolls.add(blueScroll);
labels.setLayout(new GridLayout(3,1,2,2));
labels.add(redLabel);
labels.add(greenLabel);
labels.add(blueLabel);
} // end init();
public Color getColor() {
// Get the color currently displayed by the component.
int r = redScroll.getValue();
int g = greenScroll.getValue();
int b = blueScroll.getValue();
return new Color(r, g, b);
}
public void setColor(Color c) {
// Set the component to display the given color.
// (Ignore this if c is null.)
if (c != null) {
int r = c.getRed(); // Get the color levels from the color.
int g = c.getGreen();
int b = c.getBlue();
redLabel.setText(" R = " + r); // Set the labels.
greenLabel.setText(" G = " + g);
blueLabel.setText(" B = " + b);
redScroll.setValue(r); // Set the scrollbars.
greenScroll.setValue(g);
blueScroll.setValue(b);
colorCanvas.setBackground(new Color(r,g,b)); // Set the canvas.
colorCanvas.repaint();
}
}
public Dimension getMinimumSize() {
// Specify the minimum reasonable size of this component.
return new Dimension(150,40);
}
public Dimension getPreferredSize() {
// Specify the preferred size of this component.
return new Dimension(280,80);
}
public void adjustmentValueChanged(AdjustmentEvent evt) {
// This is called when the user has changed the values on
// one of the scrollbars. All the scrollbars are checked,
// the labels are set to display the correct values,
// and the color patch is reset to correspond to the new color.
int r = redScroll.getValue();
int g = greenScroll.getValue();
int b = blueScroll.getValue();
redLabel.setText(" R = " + r);
greenLabel.setText(" G = " + g);
blueLabel.setText(" B = " + b);
colorCanvas.setBackground(new Color(r,g,b));
colorCanvas.repaint(); // Redraw the canvas in its new color.
} // end adjustmentValueChanged
public Insets getInsets() {
// The system calls this method to find out how much space to
// leave between the edges of the applet and the components that
// it contains. I want a 2-pixel border at each edge.
return new Insets(2,2,2,2);
}
} // end class RGBColorChooser
A small applet to test the component class:
/*
This is a trivial applet that tests the colorchooser class.
In particular, it tests the setColor() method in that class by
setting the color to a random color when the user clicks on a button.
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class TestRGB extends Applet implements ActionListener {
colorchooser rgb;
public void init() {
setLayout(new BorderLayout(5,5));
rgb = new colorchooser();
add(rgb, BorderLayout.CENTER);
Button button = new Button("Set Random Color");
button.addActionListener(this);
add(button, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent evt) {
int r = (int)(Math.random()*256) + 1;
int g = (int)(Math.random()*256) + 1;
int b = (int)(Math.random()*256) + 1;
rgb.setColor( new Color(r,g,b) );
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.