import java.awt.*;
import javax.swing.*;
public class Game extends JFrame {
Image ball=Toolkit.getDefaultToolkit().getImage("images/ball1.jpg");
Image desk=Toolkit.getDefaultToolkit().getImage("images/desk2.jpg");
double x=100;
double y=100;
boolean right=true;
public void paint(Graphics g) {
g.drawImage(desk,100,100,null);
g.drawImage(ball,(int)x,(int)y,null);
if(right) {
x=x+10;
}
else {
x=x-10;
}
if(x>1200)
{
right=false;
}
if(x<0)
{
right=true;
}
}
void launchFrame() {
setSize(3000,2000);
setLocation(50,50);
setVisible(true);
while(true)
{
repaint();
try {
Thread.sleep(40);
}catch(Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[]args) {
System.out.println("");
Game game=new Game();
game.launchFrame();
}
}
import java.awt.*;
import javax.swing.*;
public class Game2 extends JFrame {
Image ball=Toolkit.getDefaultToolkit().getImage("images/ball1.jpg");
Image desk=Toolkit.getDefaultToolkit().getImage("images/desk2.jpg");
double x=100;
double y=100;
double degree=3.14/3;
public void paint(Graphics g) {
g.drawImage(desk,0,0,null);
g.drawImage(ball,(int)x,(int)y,null);
x=x+10*Math.cos(degree);
y=y+10*Math.sin(degree);
if(y>500||y<0)
{
degree=-degree;
}
if(x<40||x>156)
{
degree=-degree;
}
}
void launchFrame() {
setSize(3000,2000);
setLocation(50,50);
setVisible(true);
while(true)
{
repaint();
try {
Thread.sleep(40);
}catch(Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[]args) {
System.out.println("");
Game game=new Game();
game.launchFrame();
}
}