Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Java - Change background color with button?

I wish to change the color of my background with two buttons. Its not working for some reason :\

import java.awt.*;

import java.applet.*;

import javax.swing.*;

import java.awt.event.*;

public class RandomLoopGraphics extends JApplet

{

private JButton b1 = new JButton("Green"), b2 = new JButton("Blue");

private JTextField txt = new JTextField(10);

private ButtonListener bl = new ButtonListener();

public void init()

{

b1.addActionListener(bl);

b2.addActionListener(bl);

Container cp = getContentPane();

cp.setLayout(new FlowLayout());

cp.add(b1);

cp.add(b2);

cp.add(txt);

/*

//Background

//Declares RGB colors

int red, green, blue;

red = 155;

green = 70;

blue = 174;

//Now it initializes the color for the background

Color background = new Color(red, green, blue);

setBackground(background);

*/

}

public void paint(Graphics g)

{

//FONT

Font NEWFONT = new Font("Comic Sans MS", Font.BOLD, 25);//Declaring Font type and specifics

g.setFont(NEWFONT);//SETS FONT

//CIRCLE

int start = 37;//INITIALIZING START POSITION

for (int i=1; i<=10; i++)

{

g.setColor(Color.orange);//SET ORANGE COLOR

g.drawString("CIRLCE", 70, 90);//Draws the text and places it

g.drawOval(start + 25*i, 10, 50, 50);//DRAWS A CIRCLE

}

//LINE

g.setColor(Color.green);//SET GREEN COLOR

g.drawLine(0, 105, 400, 105); //DRAWS A LINE

//RECTANGLE

int rectstart = 55;//INITIALIZING START POSITION

for (int r=1; r<=23; r++)

{

g.setColor(Color.red); //SETS RED COLOR

g.drawString("RECTANGLE", 65, 200);//Draws the text and places it

g.drawRect (rectstart + 10*r, 125, 50, 50);//DRAWS A RECTANGLE

}

}

class ButtonListener implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

Object source = e.getSource();

Color color = getBackground();

if(source == b1){

setBackground(Color.green);

}

else if(e.getSource() == b2){

color = Color.blue;

}

//repaint();

String name = ((JButton) e.getSource()).getText();

txt.setText("asd" + name);

}

}

public static void main(String[] args)

{

run(new RandomLoopGraphics(), 200, 75);

}

public static void run(JApplet applet, int width, int height)

{

JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(applet);

frame.setSize(width, height);

applet.init();

applet.start();

frame.setVisible(true);

}

}

Update:

invalidate and repaint both dont work :\

1 Answer

Relevance
  • McFate
    Lv 7
    9 years ago
    Favorite Answer

    You change the color, but don't require repaint?

    I'd recommend restoring the repaint(); call which you have commented out. If that doesn't work, try invalidate(); (and maybe both invalidate() and repaint()).

    @M

Still have questions? Get your answers by asking now.