Skip to content

Commit 05acb08

Browse files
authored
Add files via upload
0 parents  commit 05acb08

15 files changed

+819
-0
lines changed

BouncingBall.java

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import java.awt.*;
2+
import javax.swing.*;
3+
4+
public class BouncingBall extends JPanel {
5+
6+
// Box height and width
7+
int width;
8+
int height;
9+
10+
// Ball Size
11+
float radius = 40;
12+
float diameter = radius * 2;
13+
14+
// Center of Call
15+
float X = radius + 50;
16+
float Y = radius + 20;
17+
18+
// Direction
19+
float dx = 3;
20+
float dy = 3;
21+
22+
public BouncingBall() {
23+
24+
Thread thread = new Thread() {
25+
public void run() {
26+
while (true) {
27+
28+
width = getWidth();
29+
height = getHeight();
30+
31+
X = X + dx ;
32+
Y = Y + dy;
33+
34+
if (X - radius < 0) {
35+
dx = -dx;
36+
X = radius;
37+
} else if (X + radius > width) {
38+
dx = -dx;
39+
X = width - radius;
40+
}
41+
42+
if (Y - radius < 0) {
43+
dy = -dy;
44+
Y = radius;
45+
} else if (Y + radius > height) {
46+
dy = -dy;
47+
Y = height - radius;
48+
}
49+
repaint();
50+
51+
try {
52+
Thread.sleep(50);
53+
} catch (InterruptedException ex) {
54+
}
55+
56+
}
57+
}
58+
};
59+
thread.start();
60+
}
61+
62+
public void paintComponent(Graphics g) {
63+
super.paintComponent(g);
64+
g.setColor(Color.YELLOW);
65+
g.fillOval((int)(X-radius), (int)(Y-radius), (int)diameter, (int)diameter);
66+
}
67+
68+
public static void main(String[] args) {
69+
JFrame.setDefaultLookAndFeelDecorated(true);
70+
JFrame frame = new JFrame("Bouncing Ball");
71+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
72+
frame.setSize(1000, 1000);
73+
frame.setContentPane(new BouncingBall());
74+
frame.setVisible(true);
75+
}
76+
}

Calculator.java

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import java.awt.BorderLayout;
2+
import java.awt.Container;
3+
import java.awt.GridLayout;
4+
import java.awt.event.ActionEvent;
5+
import java.awt.event.ActionListener;
6+
import java.awt.event.WindowAdapter;
7+
import java.awt.event.WindowEvent;
8+
9+
import javax.swing.JButton;
10+
import javax.swing.JFrame;
11+
import javax.swing.JPanel;
12+
import javax.swing.JTextField;
13+
14+
15+
public class Calculator extends JPanel implements ActionListener {
16+
17+
private JTextField display = new JTextField("0");
18+
private double result = 0;
19+
private String operator = "=";
20+
private boolean calculating = true;
21+
22+
public Calculator() {
23+
setLayout(new BorderLayout());
24+
25+
display.setEditable(false);
26+
add(display, "North");
27+
28+
JPanel panel = new JPanel();
29+
panel.setLayout(new GridLayout(4, 4));
30+
31+
String buttonLabels = "789/456*123-0.=+";
32+
for (int i = 0; i < buttonLabels.length(); i++) {
33+
JButton b = new JButton(buttonLabels.substring(i, i + 1));
34+
panel.add(b);
35+
b.addActionListener(this);
36+
}
37+
add(panel, "Center");
38+
}
39+
40+
public void actionPerformed(ActionEvent evt) {
41+
String cmd = evt.getActionCommand();
42+
if ('0' <= cmd.charAt(0) && cmd.charAt(0) <= '9' || cmd.equals(".")) {
43+
if (calculating)
44+
display.setText(cmd);
45+
else
46+
display.setText(display.getText() + cmd);
47+
calculating = false;
48+
} else {
49+
if (calculating) {
50+
if (cmd.equals("-")) {
51+
display.setText(cmd);
52+
calculating = false;
53+
} else
54+
operator = cmd;
55+
} else {
56+
double x = Double.parseDouble(display.getText());
57+
calculate(x);
58+
operator = cmd;
59+
calculating = true;
60+
}
61+
}
62+
}
63+
64+
private void calculate(double n) {
65+
if (operator.equals("+"))
66+
result += n;
67+
else if (operator.equals("-"))
68+
result -= n;
69+
else if (operator.equals("*"))
70+
result *= n;
71+
else if (operator.equals("/"))
72+
result /= n;
73+
else if (operator.equals("="))
74+
result = n;
75+
display.setText("" + result);
76+
}
77+
78+
public static void main(String[] args) {
79+
JFrame.setDefaultLookAndFeelDecorated(true);
80+
JFrame frame = new JFrame();
81+
frame.setTitle("Calculator");
82+
frame.setSize(200, 200);
83+
frame.addWindowListener(new WindowAdapter() {
84+
public void windowClosing(WindowEvent e) {
85+
System.exit(0);
86+
}
87+
});
88+
89+
Container contentPane = frame.getContentPane();
90+
contentPane.add(new Calculator());
91+
frame.show();
92+
}
93+
}
94+

CheckInternet.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package checkinternet;
2+
import java.net.*;
3+
public class CheckInternet
4+
{
5+
public static void main(String[] args)
6+
{
7+
try
8+
{
9+
URL url=new URL("https://google.com");
10+
URLConnection con=url.openConnection();
11+
con.connect();
12+
System.out.println("Internet Connection Available");
13+
}
14+
catch (Exception e)
15+
{
16+
System.out.println("No Internet Connection");
17+
}
18+
}
19+
}

ColorBars.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package colorbars;
2+
import java.applet.*;
3+
import java.awt.*;
4+
/* @author Saket*/
5+
public class ColorBars extends Applet
6+
{
7+
Color colors[]={Color.black,Color.BLUE,Color.cyan,Color.ORANGE,Color.gray,Color.red,Color.MAGENTA,Color.yellow,Color.pink};
8+
public void paint(Graphics g)
9+
{
10+
int deltax=260/colors.length;
11+
for(int i=0;i<colors.length;i++)
12+
{
13+
g.setColor(colors[i]);
14+
g.fillRect(i*deltax,0,(i+1)*deltax,260);
15+
}
16+
}
17+
}

Counter.java

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
package counter;
3+
/*@author Saket*/
4+
import java.applet.*;
5+
import java.awt.*;
6+
public class Counter extends Applet implements Runnable
7+
{
8+
int count;
9+
Thread t;
10+
public void init()
11+
{
12+
count=0;
13+
t=new Thread(this);
14+
t.start();
15+
}
16+
public void run()
17+
{
18+
try
19+
{
20+
while(true)
21+
{
22+
repaint();
23+
Thread.sleep(1000);
24+
++count;
25+
}
26+
}
27+
catch(InterruptedException e)
28+
{
29+
e.printStackTrace();
30+
}
31+
}
32+
public void paint(Graphics g)
33+
{
34+
g.setFont(new Font("serif",Font.BOLD,36));
35+
FontMetrics fm=g.getFontMetrics();
36+
String str=" "+count;
37+
Dimension d=getSize();
38+
int x=d.width/2-fm.stringWidth(str)/2;
39+
g.drawString(str,x,d.height/2);
40+
}
41+
}

DigitalClock.java

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import java.awt.Font;
2+
import java.awt.Color;
3+
import java.awt.GridLayout;
4+
import java.awt.event.ActionEvent;
5+
import java.awt.event.ActionListener;
6+
import javax.swing.JFrame;
7+
import javax.swing.JLabel;
8+
import javax.swing.Timer;
9+
import javax.swing.SwingConstants;
10+
import java.util.*;
11+
import java.text.*;
12+
13+
public class DigitalClock {
14+
15+
public static void main(String[] arguments) {
16+
17+
ClockLabel dateLable = new ClockLabel("date");
18+
ClockLabel timeLable = new ClockLabel("time");
19+
ClockLabel dayLable = new ClockLabel("day");
20+
21+
JFrame.setDefaultLookAndFeelDecorated(true);
22+
JFrame f = new JFrame("Digital Clock");
23+
f.setSize(300,150);
24+
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
25+
f.setLayout(new GridLayout(3, 1));
26+
27+
f.add(dateLable);
28+
f.add(timeLable);
29+
f.add(dayLable);
30+
31+
32+
f.setVisible(true);
33+
}
34+
}
35+
36+
class ClockLabel extends JLabel implements ActionListener {
37+
38+
String type;
39+
SimpleDateFormat sdf;
40+
41+
public ClockLabel(String type) {
42+
this.type = type;
43+
setForeground(Color.black);
44+
45+
switch (type) {
46+
case "date" : sdf = new SimpleDateFormat(" dd MM yy");
47+
setFont(new Font("chiller", Font.PLAIN, 20));
48+
setHorizontalAlignment(SwingConstants.LEFT);
49+
break;
50+
case "time" : sdf = new SimpleDateFormat("hh:mm:ss a");
51+
setFont(new Font("chiller", Font.PLAIN, 40));
52+
setHorizontalAlignment(SwingConstants.CENTER);
53+
break;
54+
case "day" : sdf = new SimpleDateFormat("EEEE ");
55+
setFont(new Font("chiller", Font.PLAIN, 30));
56+
setHorizontalAlignment(SwingConstants.RIGHT);
57+
break;
58+
default : sdf = new SimpleDateFormat();
59+
break;
60+
}
61+
62+
Timer t = new Timer(1000, this);
63+
t.start();
64+
}
65+
66+
public void actionPerformed(ActionEvent ae) {
67+
Date d = new Date();
68+
setText(sdf.format(d));
69+
}
70+
}
71+
72+

0 commit comments

Comments
 (0)