Custom control does not display
Hi,
I've got a custom control that doesn't display, and I can't figure out why. (I need a custom control because it has to move about the screen.) Here's the code:
Code:
package view.JAccount;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.io.Serializable;
import javax.swing.JComponent;
public class JAccount extends JComponent implements Serializable {
private Integer accountID;
private AnEllipse shape;
private Text text;
public JAccount(Integer accountID) {
this.setAccountID(accountID);
MovingAdapter ma = new MovingAdapter();
addMouseMotionListener(ma);
addMouseListener(ma);
shape = new AnEllipse(150, 70, 80, 80);
text = new Text(150, 70);
setDoubleBuffered(true);
}
/**
* Get the value of accountID
*
* @return the value of accountID
*/
public Integer getAccountID() {
return accountID;
}
/**
* Set the value of accountID
*
* @param accountID new value of accountID
*/
public void setAccountID(Integer accountID) {
this.accountID = accountID;
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(new Color(142, 151, 225));
g2d.fill(shape);
}
class AnEllipse extends Ellipse2D.Float {
public AnEllipse(float x, float y, float width, float height) {
setFrame(x, y, width, height);
}
public boolean isHit(float x, float y) {
if (getBounds2D().contains(x, y)) {
return true;
} else {
return false;
}
}
public void addX(float x) {
this.x += x;
}
public void addY(float y) {
this.y += y;
}
public void addWidth(float w) {
this.width += w;
}
public void addHeight(float h) {
this.height += h;
}
}
class Text extends JComponent {
int x;
int y;
public Text(int x, int y) {
Graphics g = this.getGraphics();
g.setColor(Color.black);
g.drawString(String.valueOf(accountID), x, y);
}
public void addX(int x) {
this.x += x;
}
public void addY(int y) {
this.y += y;
}
}
class MovingAdapter extends MouseAdapter {
private int x;
private int y;
@Override
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
}
@Override
public void mouseDragged(MouseEvent e) {
int dx = e.getX() - x;
int dy = e.getY() - y;
if (shape.isHit(x, y)) {
shape.addX(dx);
shape.addY(dy);
text.addX(dx);
text.addY(dy);
repaint();
}
x += dx;
y += dy;
}
}
}
Is the paint() wrong? Do I need to be subclassing something other than JComponent? Should I have posted to the Swing forum? I'm a newbie at this, so anything could be wrong!:eek: