Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.01 KB | None | 0 0
  1. abstract class A {
  2.     public abstract void show();
  3. }
  4. ------------------------------------------------------------------
  5. public
  6.     class B extends A{
  7.  
  8.     public void show(){
  9.         System.out.println("Hej tu klasa - B\t"+getClass());
  10.     };
  11. }
  12. ------------------------------------------------------------------
  13. import java.awt.Graphics;
  14.  
  15. import figury.Kwadrat;
  16.  
  17. public class Main {
  18.    
  19.     public static void main(String[] args){
  20.         A a = null;
  21.         a = new B();
  22.         a.show();
  23.        
  24.         a = new A(){
  25.             public void show(){
  26.                 System.out.println("Hej tu klasa anonimowa\t"+getClass());
  27.             }
  28.         };
  29.         a.show();
  30.         new MyFrame();
  31.     }
  32. }
  33. ------------------------------------------------------------------
  34. import java.awt.*;
  35. import java.awt.event.*;
  36. import figury.*;
  37.  
  38. public class MyFrame
  39.     extends Frame{
  40.    
  41.     Figura fig;
  42.    
  43.     public MyFrame(){
  44.        
  45.         addWindowListener(
  46.             new WindowAdapter(){
  47.                 public void windowClosing(WindowEvent e){
  48.                     System.exit(0);
  49.                 }
  50.             }
  51.         );
  52.         addMouseListener(
  53.             new MouseAdapter(){
  54.                 public void mouseClicked(MouseEvent e){
  55.                     System.out.println(e.getX()+" "+e.getY());
  56.                     switch((int)(Math.random()*3)){
  57.                     case 0:
  58.                         fig = new Kwadrat(e.getX(),e.getY());
  59.                     break;
  60.                     case 1:
  61.                         fig = new Kolo(e.getX(),e.getY());
  62.                     break;
  63.                     case 2:
  64.                         fig = new Trojkat(e.getX(),e.getY());
  65.                     break;
  66.                     }
  67.                     repaint();
  68.                 }
  69.             }
  70.                
  71.         );
  72.         setSize(800,400);
  73.         setVisible(true);
  74.     }
  75.    
  76.     public void paint(Graphics g){
  77.         if(fig !=null)
  78.         fig.rysuj(g);
  79.     }
  80.    
  81. }
  82. ------------------------------------------------------------------
  83. package figury;
  84. import java.awt.Graphics;
  85.  
  86. public class Kwadrat
  87.     extends Figura{
  88.    
  89.     public Kwadrat(int x, int y) {
  90.         super(x, y);
  91.     }
  92.  
  93.     public void rysuj(Graphics g) {
  94.         g.drawRect(x, y, 50, 50);
  95.     }
  96.    
  97. }
  98. ------------------------------------------------------------------
  99. package figury;
  100. import java.awt.Graphics;
  101.  
  102. public class Trojkat
  103.     extends Figura{
  104.    
  105.     int punktyX[];
  106.     int punktyY[];
  107.     int promien;
  108.    
  109.     public Trojkat(int x, int y) {
  110.         super(x, y);
  111.        
  112.         promien = 25;
  113.        
  114.         double step =(Math.PI*2)/3;
  115.        
  116.         punktyX=new int[]{
  117.             (int)(Math.sin(step*0)*promien) +x,
  118.             (int)(Math.sin(step*1)*promien) +x,
  119.             (int)(Math.sin(step*2)*promien) +x
  120.         };
  121.         punktyY=new int[]{
  122.                 (int)(Math.cos(step*0)*promien) +y,
  123.                 (int)(Math.cos(step*1)*promien) +y,
  124.                 (int)(Math.cos(step*2)*promien) +y
  125.             };
  126.        
  127.     }
  128.  
  129.     public void rysuj(Graphics g) {
  130.         g.drawPolygon(punktyX,punktyY,3);
  131.     }
  132.    
  133. }
  134. ------------------------------------------------------------------
  135. package figury;
  136. import java.awt.*;
  137.  
  138. public abstract
  139.     class Figura {
  140.     protected int x,y;
  141.     public Figura(int x,int y){
  142.         this.x=x;
  143.         this.y=y;
  144.     }
  145.     public abstract void rysuj(Graphics g);
  146. }
  147. ------------------------------------------------------------------
  148. package figury;
  149. import java.awt.Graphics;
  150.  
  151. public class Kolo
  152.     extends Figura{
  153.    
  154.     public Kolo(int x, int y) {
  155.         super(x, y);
  156.     }
  157.  
  158.     public void rysuj(Graphics g) {
  159.         g.drawOval(x, y, 50, 50);
  160.     }
  161.    
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement