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

Help! I need to add pictures to this JAVA code: I don\'t know how to embedd pict

ID: 3818380 • Letter: H

Question

Help! I need to add pictures to this JAVA code:

I don't know how to embedd pictures from the internet for the toppings:bacon, chicken, vegetables.


Here's my code:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.*;

public class GUIInterface {

  
public static void main(String[] args)

{

JFrame f = new JFrame("Order List");

f.setSize(450, 250);

f.setLocation(450, 250);

f.addWindowListener(new WindowAdapter( ) {
  
public void wExit(WindowEvent E) { System.exit(0); }
});

JPanel Panel = new JPanel( );
final ButtonGroup Group = new ButtonGroup( );
JPanel Pan = new JPanel( );
TextArea area;
Pan.add(area=new TextArea("Place Your Burger Type Order:"));
JRadioButton but;
Panel.add(but = new JRadioButton("Burger"));
but.setActionCommand("Cheese");
Group.add(but);
Panel.add(but = new JRadioButton("Pizza"));
but.setActionCommand("Pizza-Type");
Group.add(but);
Panel.add(but = new JRadioButton("Sanwitch", true));
  
but.setActionCommand("Sanwitch-Type");
Group.add(but);
  
final JPanel cpanel = new JPanel( );

cpanel.add(new JCheckBox("Bacon"));

cpanel.add(new JCheckBox("Chicken"));

cpanel.add(new JCheckBox("Vegetables"));

JPanel item = new JPanel( );

JButton itemButton = new JButton("Place Your Burger Type Order:");
item.add(itemButton);

Container val = f.getContentPane( );

val.setLayout(new GridLayout(4, 1));
  
val.add(Pan);

val.add(Panel);

val.add(cpanel);

val.add(item);

itemButton.addActionListener(new ActionListener( ) {
  
public void actionPerformed(ActionEvent ae) {

String entree =
Group.getSelection().getActionCommand( );
  
  
  
  
System.out.println(entree + " Burger");
Component[] components = cpanel.getComponents( );
  
for (int i = 0; i < components.length; i++) {
JCheckBox cb = (JCheckBox)components[i];
  
if (cb.isSelected( ))
System.out.println("With " + cb.getText( ));
}
}
});

f.setVisible(true);
}
}

Explanation / Answer

First take the urls of the required images, suppose i am giving this exmaple code for the pizza and don’t use this link this for example only:
=======================================================================

URL url = new URL("https://i.ytimg.com/vi/RoBHW_I70xM/maxresdefault.jpg");

BufferedImage image = ImageIO.read(url);

JLabel label = new JLabel(new ImageIcon(image));

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(label);

f.pack();

f.setLocation(200, 200);

f.setVisible(true);

=======================================================================

1. After finalizing the required urls load the URL’s into BufferedImage class object by using the ImageIO class read method which takes the url object as pararmeter.

2. Then initialize a JLabel object with object returned by ImageIcon class constructor which takes the BufferedImage object as parameter.

3. Finally add image to at JFrame at required place as below:

        f.getContentPane().add(label);

        f.pack();

        f.setLocation(200, 200);

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote