Написал графическое приложение на Java, через главные пункты меню вызываются окна с параметрами рисования фигур (ромба, треугольника, эллипса). После задания параметров всех трех фигур они прорисовываются и содержимое треугольника закрашивается зеленым цветом (функция InTriangle). Написал также функицию принадлежности ромбу InRhomb, как функцию принадлежности одному из трегольников, разбиваемых диагональю ромба. В цикле по разрешенгию экрана проверяю принадлежности и ромбу и треугольнику (в случае такового закарашиваю зеленым цветом), но почему-то не закрашивается. Почему?
Привожу код моулей.
Модуль главного окна
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. 152. 153. 154. 155. 156. 157. 158. 159. 160. 161. 162. 163. 164. 165. 166. 167. 168. 169. 170. 171. 172. 173. 174. 175. 176. 177. 178. 179. 180. 181. 182. 183. 184. 185. 186. 187. 188. 189. 190. 191. 192. 193. 194. 195. 196. 197. 198. 199. 200. 201. 202. 203. 204. 205. 206. 207. 208. 209. 210. 211. 212. 213. 214. 215. 216. 217. 218. 219. 220. 221. 222. 223. 224. 225. 226. 227. 228. 229. 230. 231. 232. 233. 234. 235. 236. 237. 238. 239. 240. 241. 242. 243. 244. 245. 246. 247. 248. 249. 250. 251. 252. 253. 254. 255. 256. 257. 258. 259. 260. 261. 262. 263. 264. 265. 266. 267. 268. 269. 270. 271. 272. 273. 274. 275. 276. 277. 278. 279. 280. 281. 282. 283. 284. 285. 286. 287. 288. 289. 290. 291. 292. 293. 294. 295. 296.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package my.draw;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.BorderFactory;
/**
*
* @author Asus
*/
public class MainWindow extends javax.swing.JFrame {
/**
* Creates new form MainWindow
*/
public static String strforlen;
public static String strforangle;
public static String strforx1;
public static String strforx2;
public static String strforx3;
public static String strfory1;
public static String strfory2;
public static String strfory3;
public static String strforx;
public static String strfory;
public static String strforwidth;
public static String strforheight;
public static Integer len;
public static Integer angle;
public static Integer x1;
public static Integer x2;
public static Integer x3;
public static Integer y1;
public static Integer y2;
public static Integer y3;
public static Integer x;
public static Integer y;
public static Integer width;
public static Integer height;
public static Integer Max(Integer a, Integer b)
{
Integer maximum;
if (a>=b)
{
maximum=a;
}
else
{
maximum=b;
}
return maximum;
}
public static Integer Min(Integer a, Integer b)
{
Integer minimum;
if (a<=b)
{
minimum=a;
}
else
{
minimum=b;
}
return minimum;
}
public static Boolean InTriangle(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
{
Double a1, b1, a2, b2, a3, b3, a4, b4, a5, b5, a6, b6;
Double crossingx12, crossingy12, crossingx23, crossingy23, crossingx31, crossingy31;
a1=(double)(y2-y1)/(x2-x1);
b1=y1-a1*x1;
a2=(double)(y3-y2)/(x3-x2);
b2=y2-a2*x2;
a3=(double)(y3-y1)/(x3-x1);
b3=y3-a3*x3;
a4=(double)(y4-y3)/(x4-x3);
b4=y4-a4*x4;
a5=(double)(y4-y1)/(x4-x1);
b5=y4-a5*x4;
a6=(double)(y4-y2)/(x4-x2);
b6=y4-a6*x4;
crossingx12=(b1-b4)/(a4-a1);
crossingy12=a4*crossingx12+b4;
crossingx23=(b2-b5)/(a5-a2);
crossingy23=a5*crossingx23+b5;
crossingx31=(b3-b6)/(a6-a3);
crossingy31=a6*crossingx31+b6;
Boolean flag1, flag2, flag3;
flag1=(Min(x1,x2)<=crossingx12)&(crossingx12<=Max(x1,x2))&(Min(y1,y2)<=crossingy12)&(crossingy12<=Max(y1,y2));
flag2=(Min(x2,x3)<=crossingx23)&(crossingx23<=Max(x2,x3))&(Min(y2,y3)<=crossingy23)&(crossingy23<=Max(y2,y3));
flag3=(Min(x3,x1)<=crossingx31)&(crossingx31<=Max(x3,x1))&(Min(y3,y1)<=crossingy31)&(crossingy31<=Max(y3,y1));
if (flag1&flag2&flag3)
{
return true;
}
else
{
return false;
}
}
public static Boolean InRhomb(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, int x5, int y5)
{
if (MainWindow.InTriangle(x1, y1, x2, y2, x3, y3, x5, y5) | MainWindow.InTriangle(x1, y1, x4, y4, x3, y3, x5, y5))
{
return true;
}
else
{
return false;
}
}
public MainWindow() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenuItem2 = new javax.swing.JMenuItem();
jMenuItem3 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowActivated(java.awt.event.WindowEvent evt) {
formWindowActivated(evt);
}
});
jMenu1.setText("Нарисовать");
jMenuItem1.setText("Ромб");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem1);
jMenuItem2.setText("Треугольник");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem2ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem2);
jMenuItem3.setText("Эллипс");
jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem3ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem3);
jMenuBar1.add(jMenu1);
jMenu2.setText("Выход");
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 279, Short.MAX_VALUE)
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
// TODO add your handling code here:
SettingsOfRhomb rhombwindowsettings=new SettingsOfRhomb();
rhombwindowsettings.setVisible(true);
//SettingsOfTriangle trianglewindowsettings=new SettingsOfTriangle();
//trianglewindowsettings.setVisible(true);
}//GEN-LAST:event_jMenuItem1ActionPerformed
private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
// TODO add your handling code here:
if ((!"".equals(strforlen)) & (!"".equals(strforangle)))
{
len=Integer.parseInt(strforlen);
angle=Integer.parseInt(strforangle);
}
if ((!"".equals(strforx1))&(!"".equals(strforx2))&(!"".equals(strforx3))&(!"".equals(strfory1))&(!"".equals(strfory2))&(!"".equals(strfory3)))
{
x1=Integer.parseInt(strforx1);
x2=Integer.parseInt(strforx2);
x3=Integer.parseInt(strforx3);
y1=Integer.parseInt(strfory1);
y2=Integer.parseInt(strfory2);
y3=Integer.parseInt(strfory3);
}
if ((!"".equals(strforx)) & (!"".equals(strfory)) & (!"".equals(strforwidth)) & (!"".equals(strforheight)))
{
x=Integer.parseInt(strforx);
y=Integer.parseInt(strfory);
width=Integer.parseInt(strforwidth);
height=Integer.parseInt(strforheight);
}
MyPanel p=new MyPanel();
//awtPanel a=new awtPanel();
this.setLayout(new BorderLayout());
this.setSize(600, 800);
//this.add(a);
//this.repaint();
this.add(p, BorderLayout.CENTER);
p.setBorder(BorderFactory.createLineBorder(Color.RED));
p.setBackground(Color.WHITE);
//a.setVisible(true);
p.repaint();
}//GEN-LAST:event_formWindowActivated
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
// TODO add your handling code here:
SettingsOfTriangle trianglewindowsettings=new SettingsOfTriangle();
trianglewindowsettings.setVisible(true);
}//GEN-LAST:event_jMenuItem2ActionPerformed
private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
// TODO add your handling code here:
SettingsOfEllipse ellipsewindowsettings=new SettingsOfEllipse();
ellipsewindowsettings.setVisible(true);
}//GEN-LAST:event_jMenuItem3ActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainWindow().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
// End of variables declaration//GEN-END:variables
}
Модуль панели на которой происходит рисование
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.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package my.draw;
import java.awt.Color;
import java.awt.Graphics;
/**
*
* @author Asus
*/
public class MyPanel extends javax.swing.JPanel {
/**
* Creates new form MyPanel
*/
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Integer dx, dy;
dx=(int)(MainWindow.len*Math.sin(180-(double)MainWindow.angle));
dy=(int)(MainWindow.len*Math.cos(180-(double)MainWindow.angle));
if ((!"".equals(MainWindow.strforlen)) & (!"".equals(MainWindow.strforangle)))
{
g.drawLine(250, 250, 250, 250+MainWindow.len);
//Integer dx, dy;
//dx=(int)(MainWindow.len*Math.sin(180-(double)MainWindow.angle));
//dy=(int)(MainWindow.len*Math.cos(180-(double)MainWindow.angle));
g.drawLine(250, 250+MainWindow.len, 250+dx, 250+MainWindow.len+dy);
g.drawLine(250+dx, 250+MainWindow.len+dy, 250+dx, 250+dy);
g.drawLine(250+dx, 250+dy, 250, 250);
}
if ((!"".equals(MainWindow.strforx1)) & (!"".equals(MainWindow.strforx2)) & (!"".equals(MainWindow.strforx3)) & (!"".equals(MainWindow.strfory1)) & (!"".equals(MainWindow.strfory2)) & (!"".equals(MainWindow.strfory3)))
{
g.drawLine(MainWindow.x1, MainWindow.y1, MainWindow.x2, MainWindow.y2);
g.drawLine(MainWindow.x2, MainWindow.y2, MainWindow.x3, MainWindow.y3);
g.drawLine(MainWindow.x3, MainWindow.y3, MainWindow.x1, MainWindow.y1);
}
if ((!"".equals(MainWindow.strforx)) & (!"".equals(MainWindow.strfory)) & (!"".equals(MainWindow.strforwidth)) & (!"".equals(MainWindow.strforheight)))
{
g.drawOval(MainWindow.x, MainWindow.y, MainWindow.width, MainWindow.height);
}
g.setColor(Color.green);
for (int i=1; i<1000; i++)
{
for (int j=1; j<=1000; j++)
{
if (MainWindow.InTriangle(MainWindow.x1, MainWindow.y1, MainWindow.x2, MainWindow.y2, MainWindow.x3, MainWindow.y3, i, j))
{
//g.setColor(Color.green);
g.drawLine(i, j, i, j);
}
if (/**MainWindow.InTriangle(MainWindow.x1, MainWindow.y1, MainWindow.x2, MainWindow.y2, MainWindow.x3, MainWindow.y3, i, j) &**/ MainWindow.InRhomb(250, 250, 250, 250+MainWindow.len, 250+dx, 250+MainWindow.len+dy, 250+dx, 250+dy, i, j))
{
//g.setColor(Color.green);
g.drawLine(i, j, i, j);
}
if (MainWindow.InTriangle(250, 250, 250, 250+MainWindow.len, 250+dx, 250+MainWindow.len+dy, i, j))
{
g.drawLine(i, j, i, j);
}
if (MainWindow.InTriangle(250, 250, 250+dx, 250+MainWindow.len+dy, 250+dx, 250+dy, i, j))
{
g.drawLine(i, j, i, j);
}
}
}
}
public MyPanel() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
}// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
}
Также есть модули для окон, в которых задаются параметры рисования фигур. Их приводить не буду, чтоб не перезагружать вопрос кодом (елси кому надо приведу) и скажу, что в них происходит по нажатию кнопки запись в переменные
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.
public static String strforlen;
public static String strforangle;
public static String strforx1;
public static String strforx2;
public static String strforx3;
public static String strfory1;
public static String strfory2;
public static String strfory3;
public static String strforx;
public static String strfory;
public static String strforwidth;
public static String strforheight;
главного окна введенных значений текстовых полей этих окон и затем дальнейшая конвертация их в соответствующие Integer переменные.
|