-
Notifications
You must be signed in to change notification settings - Fork 0
/
Frame.java
151 lines (128 loc) · 4.35 KB
/
Frame.java
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* Geometry Project
* Authors: Raul M. Crivineanu, Mihai Moiseanu
* FMI UNIBUC
*/
import java.awt.event.MouseAdapter;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.util.*;
import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
public class Frame extends javax.swing.JFrame implements ActionListener, MouseListener{
static int atm = 0;
static int c = 0;
static JLabel clear, lbl1;
Jarvis j = new Jarvis();
ArrayList<Point> points = new ArrayList<>();;
int x,y;
public Frame() {
setResizable(false);
setTitle("Problema 4 - Proiect geometrie");
initComponents();
jPanel1.addMouseListener(this);
}
public void mouseClicked(MouseEvent e) {
//System.out.println(e.getX() + " " + e.getY());
x=e.getX();
y=e.getY();
Graphics g=jPanel1.getGraphics();
g.setColor(Color.RED);
drawCenteredCircle(g, x, y, 10);
points.add(new Point(e.getX(), e.getY()));
lbl1 = new JLabel("." + atm);
lbl1.setForeground(new Color(255, 0, 0));
jPanel1.add(lbl1);
lbl1.setLocation(e.getX()+5, e.getY()+5);
lbl1.setSize(36, 14);
atm = atm + 1;
}
public void drawCenteredCircle(Graphics g, int x, int y, int r) {
x = x-(r/2);
y = y-(r/2);
g.fillOval(x,y,r,r);
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new JPanel(null);
jPanel1.setPreferredSize(new Dimension(500, 100));
jPanel1.setBackground(Color.DARK_GRAY);
jPanel1.setForeground(new Color(255, 0, 0));
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
JButton btnx = new JButton("Desenare frontiera");
btnx.setForeground(new Color(0, 0, 0));
btnx.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
ArrayList<Integer> n = new ArrayList<>();
n = j.convexHull(points);
Graphics g = jPanel1.getGraphics();
if (c == 0)
g.setColor(Color.RED);
else
g.setColor(Color.BLUE);
for (int i = 0; i < n.size() - 1; i++) {
g.drawLine(points.get(n.get(i)).x, points.get(n.get(i)).y, points.get(n.get(i + 1)).x,
points.get(n.get(i + 1)).y);
}
c++;
}
});
JButton btnreset = new JButton("Resetare puncte");
btnreset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
points.clear();
atm = 0;
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
jPanel1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
jPanel1.removeAll();
jPanel1.revalidate();
repaint();
jPanel1.repaint();
c = 0;
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
layout.setHorizontalGroup(layout.createParallelGroup(Alignment.LEADING)
.addComponent(jPanel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup().addGap(53)
.addComponent(btnx, GroupLayout.PREFERRED_SIZE, 173, GroupLayout.PREFERRED_SIZE).addGap(45)
.addComponent(btnreset, GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE).addGap(52)));
layout.setVerticalGroup(layout.createParallelGroup(Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, GroupLayout.DEFAULT_SIZE, 239, Short.MAX_VALUE)
.addPreferredGap(ComponentPlacement.UNRELATED).addGroup(layout
.createParallelGroup(Alignment.BASELINE).addComponent(btnreset).addComponent(btnx))
.addContainerGap()));
jPanel1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
getContentPane().setLayout(layout);
pack();
}// </editor-fold>
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Frame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private JPanel jPanel1;
// End of variables declaration
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}