I was never able to make this code work for days now
public void paint(Graphics comp) {
comp.setColor(myColor);
comp.fillRoundRect(0, 0, this.getWidth(), this.getHeight(), 10, 25);
comp.setColor(Color.green);
comp.setFont(new Font("Times New Roman",Font.BOLD,22));
comp.drawString("test",0,0);
super.paint(comp);
}
if must just display a string on the panel. Here is the code for the whole panel
class Item extends JPanel{
private int index;
private JLabel myLabel;
private Color myColor;
private boolean movingDown;
int Destination;
@SuppressWarnings("serial")
private final Timer motion = new Timer(15, new ActionListener(){
public void actionPerformed(ActionEvent arg0){
setLocation(0, getY() + getMotion());
if ((Destination == getY() && movingDown) ||
(Destination >= getY() + getHeight() && !movingDown)){
motion.stop();
}
}
}){
public void start(){
if(movingDown)
Destination = getY() + getHeight();
else
Destination = getY();
super.start();
if (Destination < mainPosition*(1+.2) &&
Destination > mainPosition*(1-.2))
myColor = Color.green;
else
try{
if (!myColor.equals(Color.blue))
myColor = Color.blue;
}catch(NullPointerException e){
myColor = Color.blue;
}
}
};
Item(String item, int position, int index){
super(new BorderLayout());
this.setLocation(0, position);
if (position==mainPosition) myColor = Color.green;
else myColor = Color.blue;
this.setOpaque(false);
this.index = index;
}
public int getMotion() {
if (movingDown) return 1;
return -1;
}
public void setMovingDirection(int direction) {
movingDown = (direction==1);
motion.start();
}
public void moveUp(){
setMovingDirection(-1);
}
public void moveDown(){
setMovingDirection(1);
}
public void setLocation(int x, int y) {
if(y==mainPosition){
this.setSize(defaultSize);
}
else{
final int reduce;
if(mainPosition<y)
reduce = (y-mainPosition)/2;
else
reduce = mainPosition-y;
this.setSize(defaultSize.width - reduce,
defaultSize.height - reduce);
x = (defaultSize.width - this.getWidth())/2;
super.setLocation(x, y);
}
public void paint(Graphics comp) {
comp.setColor(myColor);
comp.fillRoundRect(0, 0, this.getWidth(), this.getHeight(), 10, 25);
comp.setColor(Color.green);
comp.setFont(new Font("Times New Roman",Font.BOLD,22));
comp.drawString("test",0,0);
super.paint(comp);
}
}