Guest User

Untitled

a guest
Oct 21st, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.52 KB | None | 0 0
  1. /*
  2.  
  3. Задание №4
  4.  
  5. Напишите программу, выводящую окошко с 6 кнопками: ВЫШЕ, НИЖЕ, ЛЕВЕЕ, ПРАВЕЕ,
  6. СВЕТЛЕЕ, ТЕМНЕЕ.
  7. В окошке нарисован отрезок, проходящий под наклоном примерно через центр окошка.
  8. Нажимая на кнопки, двигаем отрезок вверх-вниз-влево-вправо, либо делаем его светлее/темнее.
  9. Также в окошке выводится надпись о том, какая кнопка была нажата в последний раз.
  10. Продвинутые программисты могут добавить кнопки «Вращение по часовой стрелке» и
  11. «Вращение против часовой стрелки».
  12.  
  13. */
  14.  
  15.  
  16.  
  17.  
  18. package com.company;
  19.  
  20. import javax.swing.*;
  21. import java.awt.*;
  22. import java.awt.event.*;
  23. import java.awt.geom.Line2D;
  24.  
  25. public class Main
  26. { public static void main(String[] args)
  27. {
  28. new MyFrame();
  29. }
  30. }
  31.  
  32.  
  33. class MyFrame extends JFrame
  34. {
  35. public MyFrame()
  36. {
  37. super("This Frame");
  38. setDefaultCloseOperation(EXIT_ON_CLOSE);
  39.  
  40. MyPanel butPanel = new MyPanel();
  41. Container contBut = getContentPane();
  42. contBut.add(butPanel);
  43.  
  44. setLocation(500,350);
  45. setSize(700,300);
  46. setVisible(true);
  47. }
  48. }
  49.  
  50.  
  51. class MyPanel extends JPanel implements ActionListener
  52. {
  53.  
  54. JButton but1,but2,but3,but4,but5,but6;
  55. double x1 = 50,y1=50,x2=100,y2=250;
  56. int rc=255,gc=100,bc=100;
  57.  
  58.  
  59. public MyPanel() {
  60. but1 = new JButton("ВЫШЕ");
  61. but2 = new JButton("НИЖЕ");
  62. but3 = new JButton("ЛЕВЕЕ");
  63. but4 = new JButton("ПРАВЕЕ");
  64. but5 = new JButton("СВЕТЛЕЕ");
  65. but6 = new JButton("ТЕМНЕЕ");
  66.  
  67.  
  68. add(but1);
  69. add(but2);
  70. add(but3);
  71. add(but4);
  72. add(but5);
  73. add(but6);
  74.  
  75.  
  76. but6.addActionListener(this);
  77. but5.addActionListener(this);
  78. but4.addActionListener(this);
  79. but3.addActionListener(this);
  80. but2.addActionListener(this);
  81. but1.addActionListener(this);
  82.  
  83. }
  84.  
  85. public void paintComponent(Graphics graf) {
  86. super.paintComponent(graf);
  87. Graphics2D g = (Graphics2D) graf;
  88.  
  89. BasicStroke pen = new BasicStroke(6);
  90. g.setStroke(pen);
  91.  
  92. try
  93. {
  94. Color c = new Color(rc, gc, bc);
  95. Line2D myLine = new Line2D.Double(x1, y1, x2, y2);
  96. g.setColor(c);
  97. g.draw(myLine);
  98. }
  99.  
  100. catch(IllegalArgumentException ex)
  101. {
  102.  
  103. if ((bc>255)||(gc>255))
  104. {
  105. g.drawString("DO DARKER", 300, 150);
  106. }
  107.  
  108. if ((bc<0)||(gc<0))
  109. {
  110. g.drawString("DO LIGHTER",300,150);
  111. }
  112. }
  113.  
  114. }
  115.  
  116.  
  117. public void actionPerformed(ActionEvent event) throws IllegalArgumentException
  118. {
  119. Object source = event.getSource();
  120. if (source == but4)
  121. {
  122. x1=x1+50;
  123. x2=x2+50;
  124.  
  125. }
  126.  
  127. if (source == but3)
  128. {
  129. x1=x1-50;
  130. x2=x2-50;
  131. }
  132.  
  133. if (source == but2)
  134. {
  135. y1=y1+10;
  136. y2=y2+10;
  137. }
  138.  
  139. if (source == but1)
  140. {
  141. y1=y1-10;
  142. y2=y2-10;
  143. }
  144.  
  145.  
  146. if (source==but5)
  147. {
  148. if ((bc>255)||(gc>255)) throw new IllegalArgumentException("do light");
  149.  
  150. gc=gc+10;
  151. bc=bc+10;
  152. }
  153.  
  154. if (source==but6)
  155. {
  156. if ((bc<0)||(gc<0)) throw new IllegalArgumentException("do dark");
  157. gc=gc-10;
  158. bc=bc-10;
  159. }
  160.  
  161. repaint();
  162. }
  163.  
  164. }
Add Comment
Please, Sign In to add comment