0

I'm a newbie in Java. Created a little Applet and would like to add small pictures to the buttons. Tried to search around but could not find anything working with Applet. Getting a message picture cannot be converted to a string. Any help appreciated. Thanks

enter code here

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class Buttons extends Applet implements ActionListener {
    int num1 = 0;
    int num2 = 0;
    int num3 = 0;
    int num4 = 0;
    double sum = 5;
    Label total, vend, welc;
    TextArea result;

    public void init()
    {
        setLayout(new GridLayout(5, 0));

        welc = new Label ("  Welcome to the Buttons!");
        add(welc);
        vend = new Label ("Please press a button now.");
        add(vend);

        b1 = new Button("Bread");
        add(b1);
        b1.addActionListener(this);

        b2 = new Button("Butter");
        add(b2);
        b2.addActionListener(this);

        b3 = new Button("Soup");
        add(b3);
        b3.addActionListener(this);

        b4 = new Button("Water");
        add(b4);
        b4.addActionListener(this);

        b5 = new Button("Finish and pay");
        add(b5);
        b5.addActionListener(this);

        cancel = new Button("Cancel");
        add(cancel);
        cancel.addActionListener(new ActionListener(){
            @Override
                public void actionPerformed(ActionEvent e){
                Object obj=e.getSource();
                if (obj == cancel)
                {
                result.requestFocusInWindow();
                result.selectAll();
                result.setText("");
                num1 = 0; num2 = 0; num3 = 0; num4 = 0;
                }
        }
});

        total = new Label ("     You order total");
        add(total);
        result = new TextArea ( 10, 30);
        add(result);
        result.setEditable(false);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == b1){
            num1++;
            showStatus("You have chosen " + num1 + " bread");}
        if (e.getSource() == b2){
            num2++;
            showStatus("You have chosen " + num2 + " butter");}
        if (e.getSource() == b3){
            num3++;
            showStatus("You have chosen " + num3 + " soup");}
        if (e.getSource() == b4){
            num4++;
            showStatus("You have chosen " + num4 + " water");}
        if (e.getSource() == b5){
            result.requestFocusInWindow();
            sum = addition(num1, num2, num3, num4);
             result.setText(" Bread: "+ num1 +" selected\n"
            +" Butter: "+ num2 +" selected\n"
            +" Soup: "+ num3 +" selected\n"
            +" Water: "+ num4 +" selected\n"
            + "Total money due is: € " +Double.toString(sum));
            }
}

        public double addition(int x, int y, int z, int t)
            {
                return 1.2*x + 3.1*y + 2*z + 1.5*t;
    }
    Button b1, b2, b3, b4, b5, cancel;

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Take a look at this [link](http://stackoverflow.com/questions/4898584/java-using-an-image-as-a-button) – Coder Dec 01 '16 at 22:02
  • Thank you. It worked with java program but not in an applet. Is there a solution there? – Arunas Malakauskas Dec 01 '16 at 22:21
  • what's the error you are facing? – Coder Dec 01 '16 at 22:23
  • just try to place any picture on any button – Arunas Malakauskas Dec 01 '16 at 22:27
  • can you update your code to show your recent changes? – Coder Dec 01 '16 at 22:29
  • will do in the morning. Thanks – Arunas Malakauskas Dec 01 '16 at 22:30
  • import java.awt.event.*; import javax.swing.*; public class ImageButton{ ImageButton(){ JFrame f=new JFrame(); JButton b=new JButton(new ImageIcon("C:\\Users\\Peter\\Pictures\\cup.jpg")); b.setBounds(20,20,150, 150); f.add(b); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);} public static void main(String[] args) { new ImageButton();} } – Arunas Malakauskas Dec 02 '16 at 07:47
  • This one works for me but not on the applet – Arunas Malakauskas Dec 02 '16 at 07:48
  • Getting C:\Users\Peter\Desktop\VendingMachine.java:59: error: incompatible types: Icon cannot be converted to String Button b3 = new Button(icon); ^ Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output 1 error Tool completed with exit code 1 – Arunas Malakauskas Dec 02 '16 at 07:50
  • When trying to add up – Arunas Malakauskas Dec 02 '16 at 07:50
  • String iconPath = "C:\\Users\\Peter\\Pictures\\cup.jpg"; Icon icon = new ImageIcon(getClass().getResource(iconPath)); Button b3 = new Button(icon); – Arunas Malakauskas Dec 02 '16 at 07:50
  • You're mixing awt and swing stuff, that's probably getting messy. Try to use swing and nothing from awt might lead to a solution. awt is even more outdated that applets are today. – Stefan Hegny Dec 02 '16 at 07:52
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free). .. – Andrew Thompson Dec 02 '16 at 15:40
  • .. **3) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT components in favor of Swing.** Note that a Swing based `JButton` supports icons, while the AWT `Button` does not! – Andrew Thompson Dec 02 '16 at 15:40
  • Can anyone fix this for me? – Arunas Malakauskas Dec 02 '16 at 16:56

0 Answers0