forked from srinathr91/TestJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame2.java
More file actions
68 lines (61 loc) · 1.75 KB
/
Game2.java
File metadata and controls
68 lines (61 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
/**
* Re-locating position of x,y
* (0,0) in C-application= (0,1000) in java application
* (x,y) in C-application= (x, 1000-y) in java application.
*/
public class Game2 extends JPanel {
public Color abc(){
// Will produce a random colour with more red in it (usually "pink-ish")
Random rand = new Random();
int R = (int)(Math.random()*256);
int G = (int)(Math.random()*256);
int B= (int)(Math.random()*256);
Color randomColor = new Color(R, G, B); //random color, but can be bright or dull
return randomColor;
}
@Override
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(abc());//Color.RED
/**
* We receive balls coordinate at the center
*/
//g2d.fillOval(0, 0, 40, 40);
/**
* Added balls
*/
g2d.fillOval(150, 320, 40, 40);
g2d.fillOval(650, 180, 40, 40);
g2d.fillOval(750, 200, 40, 40);
g2d.fillOval(800, 330, 40, 40);
g2d.fillOval(900, 274, 40, 40);
/**
* Coordinate:(900,900)= (900,1000-900)
* 900-20; 100-20
*/
g2d.setColor(abc());
g2d.drawOval(900-20, 1000-900-20, 40, 40);
g2d.setColor(abc());
//coordinates (500,300) and (100,750)
g2d.drawLine(500, 1000-300, 1000, 1000-750);
g2d.setColor(abc());//Color.CYAN
//coordinates (20,750) and (500,100)
g2d.drawLine(20, 1000-750, 500, 1000-100);
}
public static void main(String[] args) {
JFrame frame = new JFrame("My GUI");
frame.add(new Game2());
frame.setSize(1000, 1000);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}