Click here to Skip to main content
15,040,212 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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.setBounds(0, 0, defaultSize.width, defaultSize.height);
        this.setLocation(0, position);
        if (position==mainPosition) myColor = Color.green;
        else myColor = Color.blue;
        this.setOpaque(false);
//      this.add(myLabel=new JLabel(item,JLabel.CENTER));
//      myLabel.setForeground(Color.white);
//      myLabel.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,22));
        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);
    }

}
Posted
Updated 6-Jun-11 21:12pm
v2
Comments
   
Not a question. You never able to display a string -- may be you're not ready for asking questions.
--SA
Neville Nazerane 7-Jun-11 2:17am
   
Well speaking of 'never', sorry I forget to mention I did get this to work....

<pre><pre lang="cs">new JPanel(null){
{
this.setOpaque(false);
this.add(new JLabel("False"));
}
public void paint(Graphics g) {
Graphics2D comp = (Graphics2D) g;
comp.setColor(Color.green);
comp.setFont(new Font("Times New Roman",Font.BOLD,40));
comp.drawString("test",50,150);
super.paint(g);
}
}</pre>
</pre>

what I meant by never is in this panel it never worked. Thanks for the reply anyways.
Neville Nazerane 7-Jun-11 2:19am
   
now why does the same code not work in my Panel?

1 solution

You need to repaint your JPanel/Component/Canvas after you've added the line.

JComponent.repaint() @ Oracle.com[^]

But please sort your code, constructors on top, functions underneath and anonymous objects should not even be in there. It's not surprising that you've lost control over this.
   
Comments
Neville Nazerane 7-Jun-11 3:22am
   
ok.. about the solution thank you, but that was not the problem, but I did get it solved :)
it gets repainted each time I need on its own. Thanks anyways...

about the second part, I did now entirely get you. Should not be there as in? Could you pls show me what you mean by shortening my code if you don't mind?
TorstenH. 7-Jun-11 3:36am
   
http://www.oracle.com/technetwork/java/codeconv-138413.html

Check out the Checkstyle Plugin ( http://checkstyle.sourceforge.net/ ), it will force you to stick to the coding guideline.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900