Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- abstract class A {
- public abstract void show();
- }
- ------------------------------------------------------------------
- public
- class B extends A{
- public void show(){
- System.out.println("Hej tu klasa - B\t"+getClass());
- };
- }
- ------------------------------------------------------------------
- import java.awt.Graphics;
- import figury.Kwadrat;
- public class Main {
- public static void main(String[] args){
- A a = null;
- a = new B();
- a.show();
- a = new A(){
- public void show(){
- System.out.println("Hej tu klasa anonimowa\t"+getClass());
- }
- };
- a.show();
- new MyFrame();
- }
- }
- ------------------------------------------------------------------
- import java.awt.*;
- import java.awt.event.*;
- import figury.*;
- public class MyFrame
- extends Frame{
- Figura fig;
- public MyFrame(){
- addWindowListener(
- new WindowAdapter(){
- public void windowClosing(WindowEvent e){
- System.exit(0);
- }
- }
- );
- addMouseListener(
- new MouseAdapter(){
- public void mouseClicked(MouseEvent e){
- System.out.println(e.getX()+" "+e.getY());
- switch((int)(Math.random()*3)){
- case 0:
- fig = new Kwadrat(e.getX(),e.getY());
- break;
- case 1:
- fig = new Kolo(e.getX(),e.getY());
- break;
- case 2:
- fig = new Trojkat(e.getX(),e.getY());
- break;
- }
- repaint();
- }
- }
- );
- setSize(800,400);
- setVisible(true);
- }
- public void paint(Graphics g){
- if(fig !=null)
- fig.rysuj(g);
- }
- }
- ------------------------------------------------------------------
- package figury;
- import java.awt.Graphics;
- public class Kwadrat
- extends Figura{
- public Kwadrat(int x, int y) {
- super(x, y);
- }
- public void rysuj(Graphics g) {
- g.drawRect(x, y, 50, 50);
- }
- }
- ------------------------------------------------------------------
- package figury;
- import java.awt.Graphics;
- public class Trojkat
- extends Figura{
- int punktyX[];
- int punktyY[];
- int promien;
- public Trojkat(int x, int y) {
- super(x, y);
- promien = 25;
- double step =(Math.PI*2)/3;
- punktyX=new int[]{
- (int)(Math.sin(step*0)*promien) +x,
- (int)(Math.sin(step*1)*promien) +x,
- (int)(Math.sin(step*2)*promien) +x
- };
- punktyY=new int[]{
- (int)(Math.cos(step*0)*promien) +y,
- (int)(Math.cos(step*1)*promien) +y,
- (int)(Math.cos(step*2)*promien) +y
- };
- }
- public void rysuj(Graphics g) {
- g.drawPolygon(punktyX,punktyY,3);
- }
- }
- ------------------------------------------------------------------
- package figury;
- import java.awt.*;
- public abstract
- class Figura {
- protected int x,y;
- public Figura(int x,int y){
- this.x=x;
- this.y=y;
- }
- public abstract void rysuj(Graphics g);
- }
- ------------------------------------------------------------------
- package figury;
- import java.awt.Graphics;
- public class Kolo
- extends Figura{
- public Kolo(int x, int y) {
- super(x, y);
- }
- public void rysuj(Graphics g) {
- g.drawOval(x, y, 50, 50);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement