Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6774985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:51:11+00:00 2026-05-26T15:51:11+00:00

My code works only if i press the button 1 but when i press

  • 0

My code works only if i press the “button 1” but when i press “button 2” nothing happens.
I know something wrong with my paint() method and the if...else statement, but have no idea how to fix it.

thank you

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

public class AnAppletWithButtons extends Applet implements ActionListener
{
    Button button1, button2;
    int r, g , b;
    Color color;
    boolean drawLine = false;
    boolean fillOval = false;

    public void init()
    {
        String parmStringRED =  getParameter("red");
        r = Integer.parseInt(parmStringRED);
        String parmStringGREEN =  getParameter("green");
        g = Integer.parseInt(parmStringGREEN);
        String parmStringBLUE =  getParameter("blue");
        b = Integer.parseInt(parmStringBLUE);
        color = new Color (r,g,b);
        button1 = new Button("Button 1");
        add(button1);
        button1.addActionListener(this);

        button2 = new Button("Button 2");
        add(button2);
        button2.addActionListener(this);
        button1.setForeground(color);
        button2.setForeground(color);

        button1.setBackground(Color.yellow);
        button2.setBackground(Color.yellow);
    }

    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == button1) {
            System.out.println("Button 1 was pressed");
            drawLine = true;
        } else if (e.getSource() == button2) {
            System.out.println("Button 2 was pressed");
            fillOval = true;
        }
        repaint();
    }
    public void paint(Graphics g)
    {
        super.paint(g);   // for the background
        if(drawLine){
            System.out.println(drawLine);
            g.drawLine(0, 0, 400, 400);
        }
        else if(fillOval){
            System.out.println(fillOval);
            g.fillOval(10, 10, 390, 390);
        }

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:51:12+00:00Added an answer on May 26, 2026 at 3:51 pm

    Break up the else if in paint(Graphics g) into two ifs

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't know what is wrong in my code, but everytime i put widget
I have code that works but only once. I need an input char a
timer_gear exist only in case if I press some button (until 5 sec). But
ummm, probably a simple one... the code below only works with visible (show()) elements
See code below, for some reason it only works when I put a breakpoint
I have the following code which works fine. However, I only want to return
My code works great in IE8, Firefox and Safari. But it doesn't work properly
This code works, but i dont understand why. With DeferredLoadingEnabld = false, I would
Consider the following code : HTML: <div id='button' class='enabled'>Press here</div> <div id='log'></div> CSS: #button
The media player embedded code work only in IE.How I can do it able

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.