Why the color doesn't change follow what I want.

B

Bruce .J Sam

My two java file are list below:
/* ************************************************************ */
//MyContainer.java
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JApplet;
import javax.swing.JFrame;


public class MyContainer extends JApplet {
private MyButton[] myButton = new MyButton[2];
public MyContainer() {
myButton[0] = new MyButton(5, 5, 200, 30);
myButton[1] = new MyButton(5, 40, 200, 30);
getContentPane().setLayout(null);
for (int i = 0; i < myButton.length; i++) {
System.out.println("Add myButton " + (i+1));
getContentPane().add(myButton);
myButton.addMouseListener(myButton);
}
}
public void paint(Graphics g) {
for (int i = 0; i < myButton.length; i++) {
myButton.paint(g);
}
}

public static void main(String[] args) {
MyContainer container = new MyContainer();
JFrame f = new JFrame();
f.getContentPane().add(container);
f.pack();
f.setSize(new Dimension(300, 200));
f.show();
}
}
/* ************************************************************ */
//MyButton.java
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;


public class MyButton extends Component implements MouseListener {
private Color m_clickedColor = new Color(0,128,255);
private Color m_pressedColor = new Color(128,0,255);
private Color m_releasedColor = new Color(128,64,0);
private Color m_enteredColor = new Color(0,128,0);
private Color m_exitedColor = new Color(255,0,0);
private Color m_rectColor;



public MyButton(int x, int y, int width, int height) {
setBounds(x, y, width, height);
m_rectColor = m_exitedColor;
}

public void mouseClicked(MouseEvent arg0) {
System.out.println("Mouse is clicked!");
m_rectColor = m_clickedColor;
repaint();
}

public void mousePressed(MouseEvent arg0) {
System.out.println("Mouse is pressed!");
m_rectColor = m_pressedColor;
repaint();
}

public void mouseReleased(MouseEvent arg0) {
System.out.println("Mouse is released!");
m_rectColor = m_releasedColor;
repaint();
}

public void mouseEntered(MouseEvent arg0) {
System.out.println("Mouse is entered!");
m_rectColor = m_enteredColor;
repaint();
}

public void mouseExited(MouseEvent arg0) {
System.out.println("Mouse is exited!");
m_rectColor = m_exitedColor;
repaint();
}

public void paint(Graphics g) {
System.out.println(this);
System.out.println(m_rectColor);
g.setColor(m_rectColor);
g.fillRect(getBounds().x, getBounds().y,
getBounds().width, getBounds().height);
}
}
/* ************************************************************* */
What I want to do is when I pressed the button, clicked or so on,
the color of the button will change.But in fact, the program doesn't
work well.
 
J

John McGrath

What I want to do is when I pressed the button, clicked or so on,
the color of the button will change.But in fact, the program doesn't
work well.

It is generally a good idea to say what you expect it to do and what it
actually does. Saying that the program "doesn't work well" does not give
people much to go on.
public class MyContainer extends JApplet {
public void paint(Graphics g) {
for (int i = 0; i < myButton.length; i++) {
myButton.paint(g);
}
}


For Swing components, you should generally override paintComponent(), not
paint(), and you should call the superclass method when you override it.
Also, it is not necessary to paint child components - that will happen
automatically.
public static void main(String[] args) {
MyContainer container = new MyContainer();
JFrame f = new JFrame();
f.getContentPane().add(container);
f.pack();
f.setSize(new Dimension(300, 200));
f.show();
}

You should construct your UI in the Event Dispatch Thread. To do this,
you just need to wrap the code in a Runnable and call
EventQueue.invokeLater() on the Runnable.
}
/* ************************************************************ */
//MyButton.java

When posting an example, it is a good idea to post a single file, if at
all possible. In this case, all you would have had to do is to combine
the two files and remove the "public" from the declaration of MyButton.
public void paint(Graphics g) {
g.fillRect(getBounds().x, getBounds().y,
getBounds().width, getBounds().height);

The Graphics context passed to paint methods has the upper left position
of the current component as its origin.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top