Answered! Complete Programming Exercise 15.6 Alternate Two Messages on page 622. In addition to the stated requirements,…

Complete Programming Exercise 15.6 Alternate Two Messages on page 622. In addition to the stated require ments, alternate the color and font of the two text messages. Heres some example output 15.6 Alternate two messages Write a program to display the text Java is fun and Java is powerful alternately with a mouse click. Program 5 by Dolly Samson I love surfing Program 5 by Dolly Samson I love skiing Be creative in your use of wording, text features and color! BE SURE to document your code and include your name and date at the top of your Java file

Complete Programming Exercise 15.6 Alternate Two Messages on page 622. In addition to the stated requirements, alternate the color and font of the two text messages. Heres some example output: Write a program to display the text Java is fun and Java is powerful alternately with a mouse click. Be creative in your use of wording, text features and color! BE SURE to document your code and include your name and date at the top of your Java file.

Expert Answer

Here is the code for the question . Please don’t forget to rate the answer if it helped. Thank you very much.

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class ClickPanel extends JPanel{

//list of messages, colors and fonts to alternate

String message[]={“Java is Fun”,”Java is powerful”};

Color colors[]={Color.BLUE, Color.RED};

String fonts[] = {“Arial”,”Verdana”};

 

//the x and y coordinate where message should display

int x, y;

int current; //current msg, color, font

 

public ClickPanel() {

 

addMouseListener(new MouseAdapter() {

@Override

public void mouseClicked(MouseEvent e) {

 

//pass the mouse click location to changeText()

changeText(e.getX(),e.getY());

}

 

});

 

}

@Override

public void paint(Graphics g) {

//get the graphics for this panel and display the current message in the

// current color and font

Graphics g2d = (Graphics2D) g;

Font f = new Font(fonts[current],Font.BOLD, 14);

g2d.setFont(f);

g2d.setColor(colors[current]);

g2d.drawString(message[current],x, y );

}

 

//is called when mouse is clicked. Its sets the x, y values to new values

//and changes the current value to alternate between 0 and 1 and calls repaint()

void changeText(int x1, int y1)

{

x = x1;

y = y1;

 

current = 1 – current;

repaint();

 

}

 

public static void main(String[] args) {

JFrame f = new JFrame(“Program 5”);

f.setSize(400, 400);

f.getContentPane().add(new ClickPanel());

f.setVisible(true);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

Still stressed from student homework?
Get quality assistance from academic writers!