Advertisement
codisinmyvines

sssd

Nov 19th, 2021
910
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.58 KB | None | 0 0
  1. package hw10;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.geom.Ellipse2D;
  8. import java.awt.geom.Line2D;
  9. import java.awt.geom.Rectangle2D;
  10.  
  11. class PaintTask1Frame extends JFrame implements ActionListener {
  12.     private JButton button1, button2;
  13.     private JPanel picture;
  14.  
  15.     public PaintTask1Frame() {
  16.         setSize(800, 600);
  17.         setTitle("Picture 1");
  18.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  19.         setVisible(true);
  20.  
  21.         picture = new JPanel();
  22.         picture.setLayout(new CardLayout());
  23.         picture.add(new PaintTask1DrawComponent1());
  24.         picture.add(new PainTask1DrawComponent2());
  25.  
  26.         JPanel buttons = new JPanel();
  27.         button1 = new JButton("День");
  28.         button2 = new JButton("Ночь");
  29.         button1.addActionListener(this);
  30.         button2.addActionListener(this);
  31.         buttons.setLayout(new FlowLayout(FlowLayout.CENTER));
  32.         buttons.add(button1);
  33.         buttons.add(button2);
  34.         getContentPane().add("South", buttons);
  35.         getContentPane().add("Center", picture);
  36.     }
  37.  
  38.     //попробовать через setVisible, сделать просто две компоненты для рисования, а не один picture
  39.     @Override
  40.     public void actionPerformed(ActionEvent e) {
  41.         if (e.getSource() == button1) {
  42.             ((CardLayout) picture.getLayout()).first(picture);
  43.         }
  44.         if (e.getSource() == button2) {
  45.             ((CardLayout) picture.getLayout()).last(picture);
  46.         }
  47.     }
  48. }
  49.  
  50. class PaintTask1DrawComponent1 extends JComponent {
  51.  
  52.     public void paint(Graphics g) {
  53.         Graphics2D g2 = (Graphics2D) g;
  54.         //все что за окном
  55.         Rectangle2D horizon = new Rectangle2D.Double(200, 80, 400, 200);
  56.         g2.setPaint(new Color(98, 238, 209));
  57.         g2.fill(horizon);
  58.         g2.draw(horizon);
  59.         Rectangle2D grass = new Rectangle2D.Double(200, 280, 400, 200);
  60.         g2.setPaint(new Color(61, 155, 41));
  61.         g2.fill(grass);
  62.         g2.draw(grass);
  63.         //солнце
  64.         Ellipse2D sun = new Ellipse2D.Double(480, 120, 80, 80);
  65.         g2.setPaint(new Color(255, 255, 0));
  66.         g2.fill(sun);
  67.         g2.draw(sun);
  68.         Line2D l1 = new Line2D.Double(520, 100, 520, 220);
  69.         g2.draw(l1);
  70.         Line2D l2 = new Line2D.Double(460, 160, 580, 160);
  71.         g2.draw(l2);
  72.         Line2D l3 = new Line2D.Double(570, 105, 480, 220);
  73.         g2.draw(l3);
  74.         Line2D l4 = new Line2D.Double(470, 115, 570, 210);
  75.         g2.draw(l4);
  76.         //окно
  77.         g2.setPaint(new Color(162, 94, 59));
  78.         Rectangle2D podokonnik = new Rectangle2D.Double(160, 480, 480, 20);
  79.         g2.fill(podokonnik);
  80.         g2.draw(podokonnik);
  81.         Rectangle2D p1 = new Rectangle2D.Double(190, 70, 10, 410);
  82.         g2.fill(p1);
  83.         g2.draw(p1);
  84.         Rectangle2D p2 = new Rectangle2D.Double(190, 70, 420, 10);
  85.         g2.fill(p2);
  86.         g2.draw(p2);
  87.         Rectangle2D p3 = new Rectangle2D.Double(600, 70, 10, 410);
  88.         g2.fill(p3);
  89.         g2.draw(p3);
  90.         Rectangle2D p4 = new Rectangle2D.Double(395, 70, 10, 410);
  91.         g2.fill(p4);
  92.         g2.draw(p4);
  93.         //горшок
  94.         g2.setPaint(new Color(255, 159, 17));
  95.         Rectangle2D gorshok = new Rectangle2D.Double(260, 400, 60, 80);
  96.         g2.fill(gorshok);
  97.         g2.draw(gorshok);
  98.         //цветок
  99.         g2.setPaint(new Color(17, 255, 0));
  100.         Line2D stvol = new Line2D.Double(290, 360, 290, 400);
  101.         g2.draw(stvol);
  102.         g2.setPaint(new Color(255, 38, 38));
  103.         Polygon pol1 = new Polygon(new int[]{275, 275, 300}, new int[]{340, 360, 360}, 3);
  104.         g2.fill(pol1);
  105.         g2.draw(pol1);
  106.         Polygon pol2 = new Polygon(new int[]{305, 305, 280}, new int[]{340, 360, 360}, 3);
  107.         g2.fill(pol2);
  108.         g2.draw(pol2);
  109.         g2.setPaint(new Color(17, 255, 0));
  110.         Polygon pol3 = new Polygon(new int[]{290, 300, 300}, new int[]{380, 370, 380}, 3);
  111.         g2.fill(pol3);
  112.         g2.draw(pol3);
  113.     }
  114. }
  115.  
  116. class PainTask1DrawComponent2 extends JComponent {
  117.     public void paint(Graphics g) {
  118.         Graphics2D g2 = (Graphics2D) g;
  119.         //все что за окном
  120.         Rectangle2D horizon = new Rectangle2D.Double(200, 80, 400, 200);
  121.         g2.setPaint(new Color(40, 43, 96));
  122.         g2.fill(horizon);
  123.         g2.draw(horizon);
  124.         Rectangle2D grass = new Rectangle2D.Double(200, 280, 400, 200);
  125.         g2.setPaint(new Color(34, 91, 24));
  126.         g2.fill(grass);
  127.         g2.draw(grass);
  128.         //луна
  129.         Ellipse2D moon = new Ellipse2D.Double(480, 120, 80, 80);
  130.         g2.setPaint(new Color(248, 238, 238));
  131.         g2.fill(moon);
  132.         g2.draw(moon);
  133.         Ellipse2D addElMoon = new Ellipse2D.Double(455, 120, 80, 80);
  134.         g2.setPaint(new Color(40, 43, 96));
  135.         g2.fill(addElMoon);
  136.         g2.draw(addElMoon);
  137.         //окно
  138.         g2.setPaint(new Color(162, 94, 59));
  139.         Rectangle2D podokonnik = new Rectangle2D.Double(160, 480, 480, 20);
  140.         g2.fill(podokonnik);
  141.         g2.draw(podokonnik);
  142.         Rectangle2D p1 = new Rectangle2D.Double(190, 70, 10, 410);
  143.         g2.fill(p1);
  144.         g2.draw(p1);
  145.         Rectangle2D p2 = new Rectangle2D.Double(190, 70, 420, 10);
  146.         g2.fill(p2);
  147.         g2.draw(p2);
  148.         Rectangle2D p3 = new Rectangle2D.Double(600, 70, 10, 410);
  149.         g2.fill(p3);
  150.         g2.draw(p3);
  151.         Rectangle2D p4 = new Rectangle2D.Double(395, 70, 10, 410);
  152.         g2.fill(p4);
  153.         g2.draw(p4);
  154.         //горшок
  155.         g2.setPaint(new Color(255, 159, 17));
  156.         Rectangle2D gorshok = new Rectangle2D.Double(260, 400, 60, 80);
  157.         g2.fill(gorshok);
  158.         g2.draw(gorshok);
  159.         //цветок
  160.         g2.setPaint(new Color(17, 255, 0));
  161.         Line2D stvol = new Line2D.Double(290, 360, 290, 400);
  162.         g2.draw(stvol);
  163.         g2.setPaint(new Color(255, 38, 38));
  164.         Polygon pol1 = new Polygon(new int[]{275, 275, 300}, new int[]{340, 360, 360}, 3);
  165.         g2.fill(pol1);
  166.         g2.draw(pol1);
  167.         Polygon pol2 = new Polygon(new int[]{305, 305, 280}, new int[]{340, 360, 360}, 3);
  168.         g2.fill(pol2);
  169.         g2.draw(pol2);
  170.         g2.setPaint(new Color(17, 255, 0));
  171.         Polygon pol3 = new Polygon(new int[]{290, 300, 300}, new int[]{380, 370, 380}, 3);
  172.         g2.fill(pol3);
  173.         g2.draw(pol3);
  174.     }
  175. }
  176.  
  177. public class PaintTask1v2 {
  178.     public static void main(String[] args) {
  179.         PaintTask1Frame f = new PaintTask1Frame();
  180.     }
  181. }
  182.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement