write a separate application class (which may extend JFrame and will include its
ID: 3849713 • Letter: W
Question
write a separate application class (which may extend JFrame and will include its own main method
Create a simple Swing application that animates a shape diagonally across its window once. You may set the size of the window and assume that it doesn’t change.
Extend your application from the previous question to do the following:
a. Have the animation work properly even if the window is resized; and << just these two plz if possible
b. Have the animation restart at the beginning once the shape reaches the other side. << just these two plz (Once the animation starts from left corner and reaches the right corner, the object disappears and re appears from where it first came from. like going around the globe, or thinking like the right corner is connected to the left corner
Explanation / Answer
A Sample Swing application code is given below :
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class Test extends JFrame {
public SampleSwingsAPP g = new SampleSwingsAPP();
public static void main( String[] args ) {
Test t = new Test();
t.setSize( 900, 700 );
t.setVisible( true );
t.getContentPane().add( t.g );
while ( true ) {
t.g.x = t.g.x + 1;
t.g.y = t.g.y + 1;
t.repaint();
try {
Thread.sleep( 100 );
} catch ( InterruptedException e ) {
}
}
}
public void paintComponent( Graphics g ) {
g.clearRect( 0, 0, 900, 700 );
}
}
class SampleSwingsAPP extends JComponent {
private static final long serialVersionUID = 1L;
public int x = 20;
public int y = 20;
public int width = 18;
public int height = 20;
public void paintComponent( Graphics g ) {
g.setColor( Color.red );
g.fillRect( x, y, width, height );
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.