Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

a.import java.awt.*; import java.awt.event.*; import javax.swing.*; public class

ID: 673767 • Letter: A

Question

a.import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class JFontSizes extends JFrame implements ActionListener
{
     JButton button = new JButton("Press Me");
     String aPhrase = new String("This is a phrase.");
     int i = 6;
     Font aFont = new Font("Monospaced", Font.BOLD, i);
     int x = 50, y = 75;
     final int GAP = 15;
     final int WIDTH = 800;
     final int HEIGHT = 600;

     public JFontSizes()
     {
    Container con = getContentPane();
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         con.setLayout(new FlowLayout());
         con.add(button);
         button.addActionListener(this);
         setSize(WIDTH, HEIGHT);
    }
    
    public void actionPerformed(ActionEvent e)
    {
    Graphics text = getGraphics();
    text.drawString(aPhrase, x += GAP, y += GAP);
    for (i = 6; i < 20; i++)
    {
    Font aNewFont = new Font("Monospaced", Font.BOLD, i);
    i++;
    text.drawString(aPhrase, x, y);
    }
    repaint();
   
//    text.setFont(new Font("Monospaced", Font.BOLD, 6));
    }

    public void paint(Graphics g)
    {
    super.paint(g);
        g.drawString(aPhrase, x += GAP, y += GAP);
       
    }

    public static void main(String[] args)
    {
        JFontSizes jfSize = new JFontSizes();
        jfSize.setVisible(true);
    }
}

b. Modify the JChangeSizeAndColor application so that it continuously changes the size, color, and location of a phrase as long as the user continues to click the button. Save the application as JChangeSizeAndColor2.java

Explanation / Answer

Solution :

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class JChangeSizeAndColor extends JFrame implements ActionListener {

private JLabel phrase, nn;
private JButton click;
JFrame frame;

Font font = new Font("Verdana", Font.BOLD, 20);
int c = 0;

JChangeSizeAndColor() {
frame = new JFrame();
frame.setTitle("Size and Color");

phrase = new JLabel("");
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0, 2));
panel.add(phrase);
nn = new JLabel("");

click = new JButton("Click me!!");
frame.setLayout(new GridLayout(3, 1));
frame.add(click);
frame.add(panel);
frame.add(nn);
frame.setSize(500, 200);
click.addActionListener(this);
frame.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == click) {
if (c == 0) {
c = 1;
phrase.setText("Go ahead...Make my day!");
nn.setText("");
phrase.setForeground(Color.BLUE);
return;
}
if (c == 1) {
c = 0;
phrase.setText("");
nn.setText("Go ahead...Make my day!?");
nn.setForeground(Color.GRAY);
nn.setFont(font);

}
}
}

public static void main(String args[]) {
JChangeSizeAndColor jChangeSizeAndColor = new JChangeSizeAndColor();
}
}