0

Hi new here at Stack Overflow. Well i am trying to make an Applet that will show the image package of the selected button and im new to java programming. Can someone help me how to use the code to display the image? here's my code:

public void init()
{
    // Buttons/Labels/Etc
    setBackground(Color.white);
    this.setLayout(null);
    packageB = new Button("Budget");
    add(packageB);
    packageB.setBounds(200, 20, 80, 20);
    packageP = new Button("Premium");
    add(packageP);
    packageP.setBounds(300, 20, 80, 20);
    sButton = new Button("Silver");
    add(sButton);
    sButton.setBounds(100, 20, 80, 20);
    sButton.setVisible(false);
    gButton = new Button("Gold");
    add(gButton);
    gButton.setBounds(200, 20, 80, 20);
    gButton.setVisible(false);
    pButton = new Button("Platinum");
    add(pButton);
    pButton.setBounds(300, 20, 80, 20);
    pButton.setVisible(false);
    dButton = new Button("Diamon");
    add(dButton);
    dButton.setBounds(400, 20, 80, 20);
    dButton.setVisible(false);
    backButton = new Button("<-- Back");
    add(backButton);
    backButton.setBounds(500, 20, 80, 20);
    backButton.setVisible(false);
    notice = new Label("CHOOSE YOUR PACKAGE!");
    add(notice);
    notice.setBounds(215, 0, 200, 20);
    notice2 = new Label("WHAT PACKAGE DO YOU WANT?");
    add(notice2);
    notice2.setBounds(205, 0, 200, 20);
    notice2.setVisible(false);
    sLabel = new Label("Test Label");
    add(sLabel);
    sLabel.setBounds(205, 0, 200, 20);
    sLabel.setVisible(false);

    //adds an ActionListener to all of the buttons to be able to receive commands
    packageB.addActionListener(this);
    packageP.addActionListener(this);
    backButton.addActionListener(this);
    sButton.addActionListener(this);
    gButton.addActionListener(this);
    pButton.addActionListener(this);
    dButton.addActionListener(this);

}

public void paint(Graphics g)
{
    this.resize(600,400);
    g.setColor(Color.black);
    g.fillRect(60,50,460,300); 
}

// receiver of the ActionListener method
public void actionPerformed(ActionEvent e)
{
    if(e.getSource() == packageB)
    {
        packageB.setVisible(false);
        packageP.setVisible(false);
        notice.setVisible(false);
        notice2.setVisible(true);
        backButton.setVisible(true);
    }
    else if (e.getSource() == packageP)
    {
        packageB.setVisible(false);
        packageP.setVisible(false);
        backButton.setVisible(true);
        notice.setVisible(false);
        notice2.setVisible(true);
        sButton.setVisible(true);
        gButton.setVisible(true);
        pButton.setVisible(true);
        dButton.setVisible(true);
    }
    else if (e.getSource() == backButton)
    {
        packageB.setVisible(true);
        packageP.setVisible(true);
        notice2.setVisible(false);
        notice.setVisible(true);
        backButton.setVisible(false);
        sButton.setVisible(false);
        gButton.setVisible(false);
        pButton.setVisible(false);
        dButton.setVisible(false);
    }
    else if (e.getSource() == sButton)
    {
        sButton.setBackground(Color.red);
        gButton.setBackground(Color.lightGray);
        pButton.setBackground(Color.lightGray);
        dButton.setBackground(Color.lightGray);
        sLabel.setVisible(true);
    }
    else if (e.getSource() == gButton)
    {
        sButton.setBackground(Color.lightGray);
        gButton.setBackground(Color.red);
        pButton.setBackground(Color.lightGray);
        dButton.setBackground(Color.lightGray);
    }
    else if (e.getSource() == pButton)
    {
        sButton.setBackground(Color.lightGray);
        gButton.setBackground(Color.lightGray);
        pButton.setBackground(Color.red);
        dButton.setBackground(Color.lightGray);
    }
    else if (e.getSource() == dButton)
    {
        sButton.setBackground(Color.lightGray);
        gButton.setBackground(Color.lightGray);
        pButton.setBackground(Color.lightGray);
        dButton.setBackground(Color.red);
    }
    // Add New ActionListener Here!
    /*else if (e.getSource() == TestButton)
    {
    }*/
}

}

  • 1
    Avoid using `null` layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify – MadProgrammer Jan 15 '15 at 13:21
  • 1
    Don't use `paint` this way, you should NEVER modify the state of any component from within the `paint` method, you could simply use `setBackground(Color)` from within the `init` method to get the same effect, you're also breaking the paint chain which could lead to any number of weird graphical glicthes – MadProgrammer Jan 15 '15 at 13:22
  • Just so your aware, not many people are actively using AWT based GUIs anymore. You should consider looking into Swing or JavaFX instead - IMHO – MadProgrammer Jan 15 '15 at 13:23
  • 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) Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Jan 16 '15 at 05:56
  • 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 using components in favor of Swing. – Andrew Thompson Jan 16 '15 at 05:57

0 Answers0