This program implements 2 .png files and 1 gif. If you plan on helping me you ca
ID: 3559175 • Letter: T
Question
This program implements 2 .png files and 1 gif. If you plan on helping me you can use any random .png and .gif you have and I will update it in the program later
here is the assignment:
In Eclipse create a new package called ImageButton Download the images Image1, Image2 and Image3 from Canvas and import them to the package ImageButton (right-click ImageButton, import...) Notice the different file extensions. Create two public classes: ImageButton and ImageButtonApp ImageButton derives from JFrame In ImageButton do the following: Create two final fields: imgButton of type JButton clickImage of type Icon In the constructor do the following: Set the title to Lab Button Create two local variables of type Icon: Image1 and Image2. Initialize them with a new ImageIcon base on image1 and image2 - like this: Icon Image1 = new ImageIcon(getClass().getResource("Imagel.png")); Initialize the field clickImage with a new ImageIcon that is based on Image3 Initialize the field imageButton with a new JButton that accepts Image1 as the only argument Call the method setRolloverlmage on imageButton and pass Image2 as roll-over Image Add the imageButton to this (ImageButton, which is a JFrame) In ImageButtonApp do the following: Crate an instance of ImageButton Make sure that ImageButton (the JFrame) terminates when the user clicks the x-Button to exit Flint: call setDefaultCloseOperation Set the size to 660 Times 660 Set visibility to true Compile / Run .. you should see an image that changes every time you roll over the mouse Back in ImageButton do the following: Outside the constructor but still inside class ImageButton create a private class called ButtonClickEventHandler It implements the interface ActionListener Eclipse can help with the import statement. It can also create the method stub for the method actionPerformed. In actionPerformed replace the TODO comment with the following code: Disable rollorver on imageButton - At the end of the constructor body do the following: Create an instance of ButtonClickEventHandler and name it buttonEventHandler Associate the buttonEventHandler with the imgButton by calling the addActionListener method and by passing buttonEventHandler as an argument. Compile / Run .. when you click the button the image changes and mouse-rollover no longer worksExplanation / Answer
ImageButton.java :
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
class ImageButton extends JPanel{
JButton imgButton = new JButton();
ImageIcon clickImage;
public ImageButton(){
imgButton.setToolTipText("Lab Button");
Icon image1 = new ImageIcon(getClass().getResource("img1.png"));
Icon image2 = new ImageIcon(getClass().getResource("img2.png"));
clickImage = new ImageIcon(getClass().getResource("img3.png"));
imgButton = new JButton(image1);
imgButton.setRolloverIcon(image2);
ButtonClickEventHandler buttonEventHandler = new ButtonClickEventHandler();
imgButton.addActionListener(buttonEventHandler);
add(imgButton);
}
private class ButtonClickEventHandler implements ActionListener {
public void actionPerformed(ActionEvent ae) {
imgButton.setRolloverIcon(clickImage);
imgButton.setRolloverEnabled(false);
}
}
}
ImageButtonApp.java
import javax.swing.*;
public class ImageButtonApp{
public static void main(String[] a){
ImageButton i1 = new ImageButton();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(i1);
f.setSize(660,660);
f.setVisible(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.