OneCompiler

ns9

108

Q1]

import javax.swing.;
import java.awt.
;
import java.awt.event.*;

class BallPanel extends JPanel implements ActionListener {
private int x = 100;
private int y = 100;
private int diameter = 50;
private Timer timer;

public BallPanel() {
    timer = new Timer(50, this);
}

public void actionPerformed(ActionEvent e) {
    y += 5; // Change this value to adjust the speed of the ball
    repaint();
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.RED);
    g.fillOval(x, y, diameter, diameter);
}

public void startAnimation() {
    timer.start();
}

}

public class BallAnimation extends JFrame {
private BallPanel ballPanel;
private JButton startButton;

public BallAnimation() {
    setTitle("Ball Animation");
    setSize(400, 400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    ballPanel = new BallPanel();
    startButton = new JButton("Start");
    startButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ballPanel.startAnimation();
        }
    });

    JPanel buttonPanel = new JPanel();
    buttonPanel.add(startButton);

    setLayout(new BorderLayout());
    add(ballPanel, BorderLayout.CENTER);
    add(buttonPanel, BorderLayout.SOUTH);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new BallAnimation().setVisible(true);
        }
    });
}

}

Q2]

App.java

package Set.A.messageholder4;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
System.out.println( "Hello World!" );
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

    String message = (String) context.getBean("messageBean");

    System.out.println(message);

}

}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- Define a bean for the message -->
<bean id="messageBean" class="java.lang.String">
    <constructor-arg value="If you can't explain it simply, you don't understand it well enough" />
</bean>
</beans>