Advertisement
lashrone1

lab2

Nov 14th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.43 KB | None | 0 0
  1.  
  2.  
  3. import java.awt.*;
  4. import javax.imageio.ImageIO;
  5. import javax.swing.*;
  6. import java.awt.event.*;
  7. import java.io.File;
  8. import java.io.IOException;
  9.  
  10. public class Main {
  11.  
  12. static boolean red=false;
  13. static boolean green=false;
  14. static boolean blue=false;
  15.  
  16. static String bMor = "Добрый утро";
  17. static String bDay = "Добрый день";
  18. static String bEve = "Добрый вечер";
  19. static String bNig = "Доброй ночи";
  20. static String path = "C:\\Users\\Nikita Tishenko\\Downloads\\thousand_words_01.jpg";
  21. static String path2 = "C:\\Users\\Nikita Tishenko\\Downloads\\the_starry_night-wallpaper-1920x1080.jpg";
  22. static String path3 = "C:\\\\Users\\\\Nikita Tishenko\\\\Downloads\\photo5330332131672565989.jpg";
  23. static String path4 = "C:\\Users\\Nikita Tishenko\\Downloads\\photo5373259492277463689 (1).jpg";
  24.  
  25. static JFrame frame = new JFrame();
  26. static JPanel lpane = new JPanel();
  27. static JPanel panelSouth = new JPanel();
  28. static ImPanel im_panel;
  29. static JLabel label = new JLabel("Label", SwingConstants.CENTER);
  30. static JPanel checkboxe;
  31. static JPanel radio;
  32. static ButtonGroup Bgroup;
  33.  
  34. public static void main(String[] args) {
  35.  
  36. EventQueue.invokeLater(new Runnable() {
  37. public void run() {
  38.  
  39. frame.setSize(800,600 );
  40. frame.setBounds(200,200,800,600);
  41. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  42. frame.setResizable(false);
  43.  
  44. lpane.setLayout(new BorderLayout());
  45. label.setForeground(Color.WHITE);
  46. lpane.add(label, BorderLayout.NORTH);
  47.  
  48. im_panel = new ImPanel(path);
  49. lpane.add(im_panel,BorderLayout.CENTER);
  50.  
  51. panelSouth.setLayout(new FlowLayout());
  52. addB(bMor,"C:\\Users\\Nikita Tishenko\\Downloads\\thousand_words_01.jpg");
  53. addB(bDay,"C:\\Users\\Nikita Tishenko\\Downloads\\the_starry_night-wallpaper-1920x1080.jpg");
  54. addB(bEve,"C:\\\\Users\\\\Nikita Tishenko\\\\Downloads\\photo5330332131672565989.jpg");
  55. addB(bNig,"C:\\Users\\Nikita Tishenko\\Downloads\\photo5373259492277463689 (1).jpg");
  56. panelSouth.setOpaque(false);
  57. lpane.add(panelSouth,BorderLayout.SOUTH);
  58.  
  59. checkboxe= new JPanel(new GridLayout(3,1,100,100));
  60. addChB("RED");
  61. addChB("GREEN");
  62. addChB("BLUE");
  63. checkboxe.setOpaque(false);
  64. lpane.add(checkboxe, BorderLayout.EAST);
  65.  
  66. Bgroup = new ButtonGroup();
  67. radio = new JPanel(new GridLayout(3,1,100,100));
  68. addRB("WHITE");
  69. addRB("BLACK");
  70. radio.setOpaque(false);
  71. lpane.add(radio, BorderLayout.WEST);
  72.  
  73. frame.setTitle("PASHALoCHKA");
  74. lpane.setBackground(Color.BLACK);
  75. frame.add(lpane);
  76. frame.setVisible(true);
  77.  
  78. }
  79. });
  80. }
  81.  
  82. private static void addRB(String name){
  83.  
  84. JRadioButton radioButton = new JRadioButton(name);
  85. radioButton.setOpaque(false);
  86.  
  87. radioButton.addActionListener(new ActionListener() {
  88.  
  89. @Override
  90. public void actionPerformed(ActionEvent e) {
  91. if(label.getForeground()==Color.BLACK&&name=="WHITE")label.setForeground(Color.WHITE);
  92. else if(label.getForeground()==Color.WHITE&&name=="BLACK")label.setForeground(Color.BLACK);
  93. }
  94. });
  95. Bgroup.add(radioButton);
  96. radio.add(radioButton);
  97. }
  98.  
  99. private static void addChB(String name) {
  100.  
  101. JCheckBox box = new JCheckBox(name);
  102. box.setOpaque(false);
  103. box.addItemListener(new ItemListener() {
  104.  
  105. @Override
  106. public void itemStateChanged(ItemEvent e) {
  107.  
  108. if(e.getStateChange()==ItemEvent.SELECTED) {
  109. if(name=="RED")red=true;
  110. if(name=="GREEN")green=true;
  111. if(name=="BLUE")blue=true;
  112. }
  113.  
  114. else {
  115. if(name=="RED")red=false;
  116. if(name=="GREEN")green=false;
  117. if(name=="BLUE")blue=false;
  118. }
  119.  
  120. if(!red&&!green&&!blue)
  121. lpane.setBackground(Color.BLACK);
  122. if(red&&!green&&!blue)
  123. lpane.setBackground(Color.RED);
  124. if(!red&&green&&!blue)
  125. lpane.setBackground(Color.GREEN);
  126. if(!red&&!green&&blue)
  127. lpane.setBackground(Color.BLUE);
  128. if(red&&green&&!blue)
  129. lpane.setBackground(Color.YELLOW);
  130. if(red&&!green&&blue)
  131. lpane.setBackground(Color.MAGENTA);
  132. if(!red&&green&&blue)
  133. lpane.setBackground(Color.CYAN);
  134. if(red&&green&&blue)
  135. lpane.setBackground(Color.WHITE);
  136. lpane.repaint();
  137. }
  138. });
  139. checkboxe.add(box);
  140. }
  141.  
  142. private static void addB(String name, String path) {
  143. JButton button = new JButton(name);
  144. button.addActionListener(new ActionListener() {
  145. public void actionPerformed(ActionEvent e) {
  146. if(path==im_panel.getPath())return;
  147. label.setText(name);
  148. lpane.remove(im_panel);
  149. im_panel = new ImPanel(path);
  150. lpane.add(im_panel);
  151. lpane.repaint();
  152. }
  153.  
  154. });
  155. panelSouth.add(button);
  156. }
  157. }
  158.  
  159. class ImPanel extends JPanel {
  160. private static final long serialVersionUID = 1L;
  161. private String path;
  162. private Image im;
  163.  
  164. public ImPanel(String path) {
  165. super();
  166. try {
  167. this.path = path;
  168. im = ImageIO.read(new File(this.path));
  169. } catch (IOException e) {
  170. }
  171. }
  172.  
  173. public String getPath() {
  174. return path;
  175. }
  176.  
  177. public void paintComponent(Graphics g) {
  178. super.paintComponent(g);
  179. if (im == null)
  180. g.drawString("Error", 0, 0);
  181. else
  182. g.drawImage(im, 0, 0, Toolkit.getDefaultToolkit().getScreenSize().width / 2, Toolkit.getDefaultToolkit().getScreenSize().height / 2, null);
  183. }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement