The Java Program below needs to be modified so that when the user clicks on the
ID: 3684077 • Letter: T
Question
The Java Program below needs to be modified so that when the user clicks on the yellow placemark in the first picture (Fortress.jpg), the second picture (Bala_Hissar.jpg) should pop up on the screen.
The current code already outputs the first picture. The pictures are below the code.
import java.awt.*;
import javax.swing.*;
public class TransitMap
{
private JScrollPane scrollPaneLevel;
public TransitMap()
{
JPanel topPanelLevel = new JPanel();
topPanelLevel.setLayout( new BorderLayout() );
Icon imageLevel = new ImageIcon("Fortress.jpg" );
JLabel labelLevel = new JLabel(imageLevel);
scrollPaneLevel = new JScrollPane();
scrollPaneLevel.getViewport().add(labelLevel);
topPanelLevel.add( scrollPaneLevel, BorderLayout.CENTER );
}
public static void main(String[] args)
{
JFrame frame = new JFrame("Satellite View");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon image = new ImageIcon("Fortress.jpg");
JLabel imageLabel = new JLabel(image);
JButton tooltipButton = new JButton(image);
tooltipButton.setToolTipText("tooltip");
JScrollPane sp = new JScrollPane(imageLabel);
tooltipButton.setToolTipText("Bala Hissar Forest");
sp.setPreferredSize(new Dimension(450, 400));
frame.getContentPane().add(sp);
frame.getContentPane().add(tooltipButton);
frame.pack();
frame.setVisible(true);
}
}
Fortress.jpg
Bala_Hissar.jpg
Explanation / Answer
Hi, Please find the updated code below.
TransitMap.java
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
public class TransitMap
{
JButton tooltipButton;
ImageIcon image1 = new ImageIcon("Bala_Hissar.jpg");
JLabel imageLabel;
public TransitMap()
{
JPanel topPanelLevel = new JPanel();
topPanelLevel.setLayout( new BorderLayout() );
JFrame frame = new JFrame("Satellite View");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon image = new ImageIcon("Fortress.jpg");
imageLabel = new JLabel(image);
tooltipButton = new JButton(image);
tooltipButton.setToolTipText("tooltip");
imageLabel.setToolTipText("Bala Hissar Forest");
imageLabel.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent e)
{
int x=e.getX();
int y=e.getY();
if(x > 610 && x < 660 && y >340 && y < 390){
imageLabel.setIcon(image1);
}
}
});
frame.getContentPane().add(imageLabel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
new TransitMap();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.