Guest User

Untitled

a guest
Apr 20th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.03 KB | None | 0 0
  1. import java.awt.Button;
  2. import java.awt.Canvas;
  3. import java.awt.Color;
  4. import java.awt.Dimension;
  5. import java.awt.FlowLayout;
  6. import java.awt.Frame;
  7. import java.awt.Graphics;
  8. import java.awt.Panel;
  9. import java.awt.Rectangle;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.awt.event.WindowEvent;
  13. import java.awt.event.WindowListener;
  14.  
  15.  
  16. public class Zad1 extends Frame{
  17.  
  18.  
  19.     /**
  20.      *
  21.      */
  22.     private static final long serialVersionUID = 1L;
  23.     /**
  24.      *
  25.      */
  26.    
  27.    
  28.     Canvas canvas = null;
  29.     boolean selected = false;
  30.     Frame frame = null;
  31.     Panel panel = new Panel();
  32.     public Zad1() {
  33.        
  34.         setLayout(new FlowLayout());
  35.         setTitle("Zadanie 1");
  36.         setPreferredSize(new Dimension(400,400));
  37.     frame = this;  
  38.     canvas = getCanvas(75,50);
  39.     canvas.setPreferredSize(new Dimension(390, 300));
  40.     add(canvas);
  41.     add(getButton());
  42.     pack();
  43.     setLocationRelativeTo(null);
  44.    
  45.     addWindowListener(new WindowListener() {
  46.         public void windowClosing(WindowEvent e) {
  47.             // TODO Auto-generated method stub
  48.             System.exit(0);
  49.        
  50.     }
  51.  
  52.         @Override
  53.         public void windowActivated(WindowEvent e) {
  54.             // TODO Auto-generated method stub
  55.            
  56.         }
  57.  
  58.         @Override
  59.         public void windowClosed(WindowEvent e) {
  60.             // TODO Auto-generated method stub
  61.            
  62.         }
  63.  
  64.         @Override
  65.         public void windowDeactivated(WindowEvent e) {
  66.             // TODO Auto-generated method stub
  67.            
  68.         }
  69.  
  70.         @Override
  71.         public void windowDeiconified(WindowEvent e) {
  72.             // TODO Auto-generated method stub
  73.            
  74.         }
  75.  
  76.         @Override
  77.         public void windowIconified(WindowEvent e) {
  78.             // TODO Auto-generated method stub
  79.            
  80.         }
  81.  
  82.         @Override
  83.         public void windowOpened(WindowEvent e) {
  84.             // TODO Auto-generated method stub
  85.            
  86.         }
  87.     });
  88.     }
  89.     /**
  90.     * Metoda obslugująca rysowanie
  91.     * x - pozycja x
  92.     * y - pozycja y
  93.     *
  94.     */
  95.     public Canvas getCanvas(final int x, final int y)
  96.     {
  97.     Canvas canvas = new Canvas()
  98.     {
  99.     private static final long serialVersionUID = 1L;
  100.     public void paint(Graphics g)
  101.     {
  102.     //rysowanie lini
  103.     Dimension d = getSize();
  104.     g.drawLine(0, 0, d.width, d.height);
  105.     g.drawLine(d.width, 0, 0, d.height);
  106.     //dodatki
  107.     if(selected)
  108.     {
  109.     setBackground(new Color(50, 100, 150));
  110.     g.drawString("Test Środowiska GUI", x, y); //75, 50
  111.     g.setColor(Color.blue);
  112.     g.fillOval(x+35, y+20, 50, 50);
  113.     g.setColor(Color.red);
  114.     g.drawRoundRect(x-10, y-20, 140, 30, 20, 20);
  115.     }
  116.     else
  117.     setBackground(new Color(255, 255, 255));
  118.     }
  119.     };
  120.     return canvas;
  121.     }
  122.     /**
  123.     * Przycisk rysowania/czyszczenia
  124.     *
  125.     */
  126.     public Button getButton()
  127.     {
  128.     final Button button = new Button("Rysuj");
  129.     button.addActionListener(new ActionListener()
  130.     {
  131.     @Override
  132.     public void actionPerformed(ActionEvent e) {
  133.     if(selected==false)
  134.     {
  135.     selected=true;
  136.     button.setLabel("Czyść");
  137.     }
  138.     else
  139.     {
  140.     selected = false;
  141.     button.setLabel(" Rysuj ");
  142.     }
  143.     //odswiezenie
  144.     Rectangle rect = frame.getBounds();
  145.     canvas.repaint(rect.x, rect.y, rect.width, rect.height);
  146.     }
  147.     });
  148.     return button;
  149.     }
  150.  
  151.     public static void main(String[] args) {
  152.     new Zad1().setVisible(true);
  153.  
  154.     }
  155.  
  156. }
Add Comment
Please, Sign In to add comment