Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I'm not seeing my animation

Status
Not open for further replies.

carpeliam

Programmer
Mar 17, 2000
990
US
I have a JFrame with a JPanel, which has a custom JComponent in it. Every few seconds or so, I call repaint() on the JComponent using the animation loop method described here:
I know that my paintComponent() method is being called. All it's doing is calling g.fillRect(), but i don't see any changes on the screen. Why might that be?

Thanks much.

Liam Morley
lmorley@wpi.edu
"light the deep, and bring silence to the world.
light the world, and bring depth to the silence."
 
here's a very simplified version of the code. basically, what it does is this:<br><ol><li>makes a panel called &quot;main&quot;, adds it</li><li>when the user presses the &quot;start&quot; button, it makes a component of type <FONT FACE=monospace>MyComponent</font> and adds that to main- that component sets its location to (15, 15) in its constructor</li><li>a timer updates every second and tells the component to move diagonally down and to the right, and then call &quot;repaint&quot;</li><li>the paint function draws a square starting at the component's location (which was specified in the previous step)</li></ol><br><FONT FACE=monospace>import java.awt.*;<br>import java.awt.event.*;<br>import javax.swing.*;<br>import javax.swing.border.LineBorder;<br><br>public class MyProg extends JFrame implements ActionListener {<br>&nbsp;&nbsp;&nbsp;&nbsp;private JPanel main;<br>&nbsp;&nbsp;&nbsp;&nbsp;private MyComp myComponent;<br>&nbsp;&nbsp;&nbsp;&nbsp;private boolean paused;<br>&nbsp;&nbsp;&nbsp;&nbsp;private Timer timer;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;public MyProg() {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super(&quot;My Prog&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.paused = true;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getContentPane().setLayout(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;main = new JPanel();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;main.setBackground(Color.BLACK);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;main.setForeground(Color.YELLOW);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;main.setBorder(new LineBorder(Color.BLUE, 8));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;main.setPreferredSize(new Dimension(260, 260));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;main.setMaximumSize(new Dimension(260, 260));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getContentPane().add(main);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JMenuBar menu = new JMenuBar();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JMenuItem start = new JMenuItem(&quot;Start&quot;, 'S');<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;start.addActionListener(new ActionListener() {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void actionPerformed(ActionEvent e) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;start();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;menu.add(start);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setJMenuBar(menu);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;public static void main(String[] args) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MyProg f = new MyProg();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.setSize(260, 260);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.setBackground(Color.BLACK);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.pack();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.setResizable(false);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.setVisible(true);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;public void start() {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myComponent = new MyComp(new Point(15, 15), Color.YELLOW);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;main.add(myComponent);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myComponent.setVisible(true);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;timer = new Timer(1000, this);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.togglePause();<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;public void actionPerformed(ActionEvent ae) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myComponent.advance();<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;protected void togglePause() {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paused = !paused;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (paused)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pause();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unpause();<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;public void pause() {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (timer.isRunning()) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;timer.stop();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;public void unpause() {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!paused && !timer.isRunning())<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;timer.start();<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br><br>class MyComp extends JComponent {<br>&nbsp;&nbsp;&nbsp;&nbsp;public MyComp(Point origin, Color c) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.setLocation(origin);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.setForeground(c);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;public void advance() {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Point p = this.getLocation();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p.translate(1, 1);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.setLocation(p);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.revalidate();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;repaint();<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;public void paint(Graphics g) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g.setColor(Color.YELLOW);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g.fillRect(this.getX(), this.getY(), 45, 45);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}</font><br>ideally i'd like to make MyComponent extend Component, not JComponent... but when I do that (or when I take out <FONT FACE=monospace>revalidate()</font>), the call to <FONT FACE=monospace>repaint()</font> never calls <FONT FACE=monospace>paint()</font>. I'd <i>really</i> like to know why that is... no combination of validate(), invalidate(), and doLayout() have seemed to work for me, and apparently there's something I'm missing.<br><br>The first time that <FONT FACE=monospace>paint()</font> is called, getX() and getY() return 16 (which they are supposed to). However, the next time (and everytime thereafter), they return 127 and 13, respectively. I have no idea where those numbers are coming from. Also, it looks like my <FONT FACE=monospace>main</font> JPanel gets pushed over to the left (there's some grey space to the right of the panel) after calling <FONT FACE=monospace>revalidate()</font>. So obviously I'm doing something wrong... <p>Liam Morley<br><A HREF="mailto:"></A><br>&quot;light the deep, and bring silence to the world.<br>light the world, and bring depth to the silence.&quot;
 
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;

public class MyProg extends JFrame implements ActionListener {
    private JPanel main;
    private MyComp myComponent;
    private boolean paused;
    private Timer timer;
    private int updatedX, updatedY;

    public MyProg() {
        super(&quot;My Prog&quot;);
        this.paused = true;
        getContentPane().setLayout(
            new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
        main = new JPanel();
        main.setBackground(Color.BLACK);
        main.setForeground(Color.YELLOW);
        main.setBorder(new LineBorder(Color.BLUE, 8));
        main.setPreferredSize(new Dimension(260, 260));
        main.setMaximumSize(new Dimension(260, 260));
        getContentPane().add(main);

        JMenuBar menu = new JMenuBar();
        JMenuItem start = new JMenuItem(&quot;Start&quot;, 'S');
        start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                start();
            }
        });
        menu.add(start);
        setJMenuBar(menu);

    }

    public static void main(String[] args) {
        MyProg f = new MyProg();
        f.setSize(260, 260);
        f.setBackground(Color.BLACK);
        f.pack();
        f.setResizable(false);
        f.setVisible(true);
    }

    public void start() {
        myComponent = new MyComp(new Point(15, 15), Color.YELLOW);
        main.add(myComponent);
        myComponent.setBounds(0,0,260,260);
        myComponent.setVisible(true);
        timer = new Timer(200, this);
        this.togglePause();
    }

    public void actionPerformed(ActionEvent ae) {
        myComponent.advance();
    }

    protected void togglePause() {
        paused = !paused;
        if (paused)
            pause();
        else
            unpause();
    }

    public void pause() {
        if (timer.isRunning()) {
            timer.stop();
        }
    }

    public void unpause() {
        if (!paused && !timer.isRunning())
            timer.start();
    }
}
class MyComp extends JComponent {
    public MyComp(Point origin, Color c) {
        this.setLocation(origin);
        this.setForeground(c);
    }

    public void advance() {
        Point p = this.getLocation();
        p.translate(1, 1);
        this.setLocation(p);
        //repaint();
    }

    public void paint(Graphics g) {
        g.setColor(Color.YELLOW);
        g.fillRect(this.getX(), this.getY(), 45, 45);
    }
}

// try to change myComponent.setBounds(0,0,260,260);
// to myComponent.setBounds(0,0,100,100);
// you will see something funny
//please Click Thank Prosper for this valuable post if my code suits you.
 
actually i had stumbled onto that by mistake, and that's allowed me to make my class extend Canvas instead of JComponent or Component. So now the basic code works.. unfortunately the more complicated code still has the same problem (I don't see any changes). Interestingly, however, if I change the background color within the myComponent object, i can see my object moving around.. but i need to have it paint using the foreground in any case, so there's still a bug to be found. <p>Liam Morley<br><A HREF="mailto:"></A><br>&quot;light the deep, and bring silence to the world.<br>light the world, and bring depth to the silence.&quot;
 
aha, i found out why it's not visible in the more complex code- my bounds are dynamic. see if you can crack the following code..;) here is the same code as before, except with a growing rectangle.<br><FONT FACE=monospace>import java.awt.*;<br>import java.awt.event.*;<br>import javax.swing.*;<br>import javax.swing.border.LineBorder;<br><br>public class MyProg extends JFrame implements ActionListener {<br>&nbsp;&nbsp;&nbsp;&nbsp;private JPanel main;<br>&nbsp;&nbsp;&nbsp;&nbsp;private MyComp myComponent;<br>&nbsp;&nbsp;&nbsp;&nbsp;private boolean paused;<br>&nbsp;&nbsp;&nbsp;&nbsp;private Timer timer;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;public MyProg() {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super(&quot;My Prog&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.paused = true;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getContentPane().setLayout(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;main = new JPanel();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;main.setBackground(Color.BLACK);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;main.setForeground(Color.YELLOW);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;main.setBorder(new LineBorder(Color.BLUE, 8));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;main.setPreferredSize(new Dimension(260, 260));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;main.setMaximumSize(new Dimension(260, 260));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getContentPane().add(main);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JMenuBar menu = new JMenuBar();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JMenuItem start = new JMenuItem(&quot;Start&quot;, 'S');<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;start.addActionListener(new ActionListener() {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void actionPerformed(ActionEvent e) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;start();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;menu.add(start);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setJMenuBar(menu);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;public static void main(String[] args) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MyProg f = new MyProg();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.setSize(260, 260);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.setBackground(Color.BLACK);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.pack();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.setResizable(false);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.setVisible(true);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;public void start() {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myComponent = new MyComp(new Point(15, 15), Color.YELLOW);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;main.add(myComponent);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myComponent.setVisible(true);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;timer = new Timer(100, this);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.togglePause();<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;public void actionPerformed(ActionEvent ae) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myComponent.advance();<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;protected void togglePause() {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paused = !paused;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (paused)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pause();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unpause();<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;public void pause() {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (timer.isRunning()) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;timer.stop();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;public void unpause() {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!paused && !timer.isRunning())<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;timer.start();<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br><br>class MyComp extends Canvas {<br>&nbsp;&nbsp;&nbsp;&nbsp;public MyComp(Point origin, Color c) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.setForeground(c);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//this.setBackground(c);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.setBounds(origin.x, origin.y, 45, 0);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;public void advance() {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Point p = this.getLocation();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p.translate(1, 1);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.setBounds(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p.x,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p.y,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;45,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(this.getBounds().height &gt;= 45) ? 45 : this.getBounds().height + 1);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;public void paint(Graphics g) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g.fillRect(this.getX(), this.getY(), this.getWidth(), this.getHeight());<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}</font><br><br>the paint method is called every time, however you only see something on the screen if you uncomment the &quot;setBackground&quot; method. Though I really don't want to change the background color... The more complicated version of the code has several different rectangles.<br><br>There will also be times when my advance method will be called, but the bounds will not be changed (say for example, if a rectangle is drawn somewhere within the bounds).. so i will need to find a different solution, as paint() won't be called unless the bounds are changed. <p>Liam Morley<br><A HREF="mailto:"></A><br>&quot;light the deep, and bring silence to the world.<br>light the world, and bring depth to the silence.&quot;
 
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class MyProg extends JFrame implements ActionListener {
    private JPanel main;
    private MyComp myComponent;
    private boolean paused;
    private Timer timer;

    public MyProg() {
        super(&quot;My Prog&quot;);
        this.paused = true;
        getContentPane().setLayout(
            new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
        main = new JPanel();
        main.setBackground(Color.BLACK);
        main.setForeground(Color.YELLOW);
        main.setBorder(new LineBorder(Color.BLUE, 8));
        main.setPreferredSize(new Dimension(260, 260));
        main.setMaximumSize(new Dimension(260, 260));
        getContentPane().add(main);

        JMenuBar menu = new JMenuBar();
        JMenuItem start = new JMenuItem(&quot;Start&quot;, 'S');
        start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                start();
            }
        });
        menu.add(start);
        setJMenuBar(menu);
    }

    public static void main(String[] args) {
        MyProg f = new MyProg();
        f.setSize(260, 260);
        f.setBackground(Color.BLACK);
        f.pack();
        f.setResizable(false);
        f.setVisible(true);
    }

    public void start() {
        myComponent = new MyComp(new Point(15, 15), Color.YELLOW);
        main.add(myComponent);
        myComponent.setVisible(true);
        timer = new Timer(100, this);
        this.togglePause();
    }

    public void actionPerformed(ActionEvent ae) {
        myComponent.advance();
    }

    protected void togglePause() {
        paused = !paused;
        if (paused)
            pause();
        else
            unpause();
    }

    public void pause() {
        if (timer.isRunning()) {
            timer.stop();
        }
    }

    public void unpause() {
        if (!paused && !timer.isRunning())
            timer.start();
    }
}

class MyComp extends Canvas {
    public MyComp(Point origin, Color c) {
        this.setBackground(c);
        this.setBounds(origin.x, origin.y, 0, 0);
    }

    public void advance() {
        Point p = this.getLocation();
        p.translate(1, 1);
       this.setBounds(p.x,p.y,45,(this.getBounds().height >= 45) ? 45 : this.getBounds().height + 1);

    }

    public void paint(Graphics g) {
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
    }
}
//please Click Thank Prosper for this valuable post if my code suits you.
 
it seems as though you've simply uncommented the line i had left commented and removed the setForeground() line- i may have missed something. i mentioned in my last post that changing the background has that effect, but I can't use this because there are times when there will be multiple rectangles drawn within the bounds, and setting the background effectively just changes the color for the bounds- it has nothing to do with the rectangles drawn.<br><br>also, i'd rather know why i'm not seeing my changes in the foreground than have a fix that works for reasons unknown. <p>Liam Morley<br><A HREF="mailto:"></A><br>&quot;light the deep, and bring silence to the world.<br>light the world, and bring depth to the silence.&quot;
 
class MyComp extends Canvas {
public MyComp(Point origin, Color c) {
this.setForeground(c);
this.setBackground(c);
this.setBounds(origin.x, origin.y, 45, 0);
}

public void advance() {
Point p = this.getLocation();
p.translate(1, 1);
this.setBounds(p.x,p.y,45,(this.getBounds().height >= 45) ? 45 : this.getBounds().height + 1);
}

public void paint(Graphics g) {
g.fillRect(this.getX(), this.getY(), this.getWidth(), this.getHeight());
}
}

//I use sun jdk 1.42 and the statement this.setBackground(c); runs fine
 
perhaps i wasn't being clear. setting the background just sets everything in the background that color- the fact that that's the desired effect <i>in this case</i> is just a side effect. but this has been a simplified example. a more complex example where that wouldn't work would be as follows (using the same code as before, but with a modified paint method):<br><br><FONT FACE=monospace>&nbsp;&nbsp;&nbsp;&nbsp;public void paint(Graphics g) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g.fillRect(this.getX(), this.getY(), this.getWidth(), this.getHeight()/2);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g.fillRect(this.getX(), this.getY() + this.getHeight()/2, this.getWidth()/2, this.getHeight()/2);<br>&nbsp;&nbsp;&nbsp;&nbsp;}</font><br><br>then as you can see, setting the background does <i>not</i> give the desired effect. <p>Liam Morley<br><A HREF="mailto:"></A><br>&quot;light the deep, and bring silence to the world.<br>light the world, and bring depth to the silence.&quot;
 
public void paint(Graphics g) {
System.out.println(this.getX());
g.fillRect(0, 0, this.getWidth(), this.getHeight()/2);
g.fillRect(0, 0 + this.getHeight()/2, this.getWidth()/2, this.getHeight()/2);
}
}

//try 0,0 for coordinates
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top