JAVA 2D GRAFİKSEL ÇİZİM ÖRNEKLERİ VE HAZIR KODLARI:



- Java ile yan yana kareler çizme:





import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;


class Surface extends JPanel {

    private void doDrawing(Graphics g) {

        Graphics2D g2d = (Graphics2D) g;
        
    for(int i=0; i<450; i+=50) {
        g2d.drawLine(30, 30+i, 430, 30+i);
        g2d.drawLine(30+i, 430, 30+i, 30);
        
    }  
      
    }

    @Override
    public void paintComponent(Graphics g) {

        super.paintComponent(g);
        doDrawing(g);
    }
}

public class SoruBir extends JFrame {

    public SoruBir() {

        initUI();
    }

    private void initUI() {

        add(new Surface());

        setTitle("Lines");
        setSize(600, 800);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                
                SoruBir ex = new SoruBir();
                ex.setVisible(true);
            }
        });
    }
}

--------------------------------------------------------------------------------------------------------------------------

- Java ile random bir şekilde renklendirilmiş yan yana kareler çizme:





import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.util.Random;

public class SoruIki extends JPanel {

  public void paint(Graphics g) {
   Random rnd= new Random();

   for (int i=0; i<350; i+=50) {
    for (int j=0; j<350; j+=50)
    g.drawRect(50+j,80+i,100,50);
    g.setColor(new Color(rnd.nextInt(255),rnd.nextInt(255),rnd.nextInt(255)));
         
   }
  }

  public static void main(String[] args) {
    Random rnd= new Random();
 JFrame frame = new JFrame();
    frame.getContentPane().add(new SoruIki());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600,800);
    frame.setVisible(true);
  }
}  
--------------------------------------------------------------------------------------------------------------------------
- Java ile içi dolu bir dikdörtgen çizme:







import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class DikdörtgenMethod extends JPanel {

  public void paint(Graphics g) {
 
   g.fillRect(50,50,100,50);
         
    
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new DikdörtgenMethod());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200,200);
    frame.setVisible(true);
  }
}  
--------------------------------------------------------------------------------------------------------------------------
-Java ile içi boş bir kare çizme:




import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class DrawRectKomutu extends JPanel {

  public void paint(Graphics g) {
 
   g.drawRect(50,50,50,50);
         
    
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new DrawRectKomutu());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200,200);
    frame.setVisible(true);
  }
} 
--------------------------------------------------------------------------------------------------------------------------
- Java ile iç içe kareler çizme:





import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;



class Surface extends JPanel {

    private void doDrawing(Graphics g) {

        Graphics2D g2d = (Graphics2D) g;
        
        for (int i=0; i<100;i+=10) {
         
        g2d.drawLine(30+i, 30+i, 200-i, 30+i);
        g2d.drawLine(200-i, 30+i, 200-i, 200-i);
        g2d.drawLine(30+i, 200-i, 200-i, 200-i);
        g2d.drawLine(30+i, 200-i, 30+i, 30+i);
        
        }
    }

    @Override
    public void paintComponent(Graphics g) {

        super.paintComponent(g);
        doDrawing(g);
    }
}

public class KareCizme extends JFrame {

    public KareCizme() {

        initUI();
    }

    private void initUI() {

        add(new Surface());

        setTitle("Lines");
        setSize(350, 250);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                
                KareCizme ex = new KareCizme();
                ex.setVisible(true);
            }
        });
    }
}
----------------------------------------------------------------------------------Java ile kenarları ve içi farklı renklerde kare çizme:





import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;



class Surface extends JPanel {

 int pos_x=160;
    int pos_y=150;
    
    private void doDrawing(Graphics g) {

        Graphics2D g2d = (Graphics2D) g;
        Random rnd= new Random();
        
        BasicStroke bs = new BasicStroke(3, BasicStroke.CAP_BUTT,
                BasicStroke.JOIN_BEVEL);
        
        g2d.setPaint(Color.red);
        g2d.fillRect(pos_x, pos_y,60, 60);


        g2d.setColor(Color.yellow);
        g2d.setStroke(bs);
        g2d.drawLine(pos_x, pos_y,pos_x+60, pos_y);


        g2d.setColor(Color.green);
        g2d.setStroke(bs);
        g2d.drawLine(pos_x+60, pos_y, pos_x+60, pos_y+60);


        g2d.setColor(Color.blue);
        g2d.setStroke(bs);
        g2d.drawLine(pos_x, pos_y+60, pos_x+60, pos_y+60);


        g2d.setColor(Color.BLACK);
        g2d.drawLine(pos_x, pos_y, pos_x, pos_y+60);
    }

    @Override
    public void paintComponent(Graphics g) {

        super.paintComponent(g);
        doDrawing(g);
    }
}

public class KareCiz extends JFrame {

    public KareCiz() {

        initUI();
    }

    private void initUI() {

        add(new Surface());

        setTitle("Kare");
        setSize(400, 400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    

    public static void main(String[] args) {
     
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                
                KareCiz kc = new KareCiz();
                kc.setVisible(true);
            }
        });
    }
}
--------------------------------------------------------------------------------- -Java ile saydamlaşan kare animasyonu:




import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.util.Random;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;



class Surface extends JPanel implements ActionListener {

    private Timer timer;
    private float alpha = 1f;
    boolean kontrol = true;
    private final int DELAY = 100;
    int pos_x=160;
    int pos_y=150;
    public Surface() {
        initTimer();
    }
    private void initTimer() {

        timer = new Timer(DELAY, this);
        timer.start();
    }


    private void doDrawing(Graphics g) {

        Graphics2D g2d = (Graphics2D) g;
        Random rnd= new Random();
        //BufferedImage buffImg = new BufferedImage(220, 140,
             //   BufferedImage.TYPE_INT_ARGB);
        //Graphics2D gbi = buffImg.createGraphics();

        AlphaComposite ac = AlphaComposite.getInstance(
                AlphaComposite.SRC_OVER, alpha);

        BasicStroke bs = new BasicStroke(3, BasicStroke.CAP_BUTT,
                BasicStroke.JOIN_BEVEL);
        
        g2d.setPaint(Color.red);
        g2d.setComposite(ac);
        g2d.fillRect(pos_x, pos_y,60, 60);


        g2d.setColor(Color.yellow);
        g2d.setComposite(ac);
        g2d.setStroke(bs);
        g2d.drawLine(pos_x, pos_y,pos_x+60, pos_y);


        g2d.setColor(Color.green);
        g2d.setComposite(ac);
        g2d.setStroke(bs);
        g2d.drawLine(pos_x+60, pos_y, pos_x+60, pos_y+60);


        g2d.setColor(Color.blue);
        g2d.setComposite(ac);
        g2d.setStroke(bs);
        g2d.drawLine(pos_x, pos_y+60, pos_x+60, pos_y+60);


        g2d.setColor(Color.BLACK);
        g2d.setComposite(ac);
        g2d.drawLine(pos_x, pos_y, pos_x, pos_y+60);




        g2d.dispose();

    }

    @Override
    public void paintComponent(Graphics g) {

        super.paintComponent(g);
        doDrawing(g);
    }

    private void step() {

        if (alpha < 0.1) {
         kontrol = false;
        }
        if (alpha > 0.9){
         kontrol = true;
        }
        if (kontrol) {
            alpha -= 0.1f;
        } else {
            alpha += 0.1f;
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        step();
        repaint();
    }
}

public class KareSaydamlik extends JFrame {

    public KareSaydamlik() {

        initUI();
    }

    private void initUI() {

        add(new Surface());

        setTitle("KareSaydam");
        setSize(400, 400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }


    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {

             KareSaydamlik ks = new KareSaydamlik();
                ks.setVisible(true);
            }
        });
    }
}
---------------------------------------------------------------------------------
- Java ile yüklenilen fotoğrafı aşağı yukarı hareket ettirme animasyonu:





import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

class Surface extends JPanel implements ActionListener {
    private final int DELAY = 100;
    private Timer timer;
    int i=0;
    boolean kontrol = true;
    int pos_x=0;
    int pos_y=0;
    int hareket=0;
    boolean yyon=false;
    private Image picture;
    public Surface() {
        initTimer();
        loadImage();
        setSurfaceSize();
    }
    private void setSurfaceSize() {
        Dimension d = new Dimension();
        d.height = picture.getHeight(null);
        setPreferredSize(d);
    }
    private void initTimer() {
        timer = new Timer(DELAY, this);
        timer.start();
    }
    private void loadImage() {
        picture = new ImageIcon("C:\\Users\\delic\\Desktop\\Yiğidosoft\\ysoft.png").getImage();

    }
    private void doDrawing(Graphics g) {
        Graphics2D g2d = (Graphics2D) g.create();
        
        g2d.drawImage(picture, pos_x, pos_y,50,50, null);
        
        g2d.dispose();
    }
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        doDrawing(g);
    }
    
    private void hareket() {
              
     if (pos_y < 0 ) {
         kontrol = false;
         hareket++;
        }
        if (pos_y > 400){
         kontrol = true;
        }
        if (hareket==11) {
         timer.stop();
        }
        if (kontrol) {
         pos_y -= 10f;
        } else {
         pos_y += 10f;
        } 
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {       
     
     hareket();
     repaint();
     
    }

}

public class onPixelAnimasyon extends JFrame {

    public onPixelAnimasyon() {

        initUI();
    }

    private void initUI() {

        setTitle("Animation");
        add(new Surface());
        setSize(500, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
             onPixelAnimasyon ex = new onPixelAnimasyon();
                ex.setVisible(true);
            }
        });
    }
}
---------------------------------------------------------------------------------
- Java ile aşağıdan yukarı doğru siyah-beyaz yuvarlaklar çizdirme animasyonu:


import java.awt.Color;
import java.awt.BasicStroke;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;



class Surface extends JPanel implements ActionListener {
 private final int DELAY = 10;
 private Timer timer;
 int sayac = 1;
 int posx = 750;
 int posy = 750;
 int count = 1;
 boolean yyon = true;

    public Surface() {
        initTimer();
    }
    private void initTimer() {
        timer = new Timer(DELAY, this);
        timer.start();
    }
    private void doDrawing(Graphics g) {
        Graphics2D g2d = (Graphics2D) g.create();

        if (sayac % 2 == 1) {

   g2d.setPaint(Color.white);
   g2d.fillOval(posx, posy, 50, 50);
   g2d.setStroke(new BasicStroke(2));
   g2d.setPaint(Color.black);
   g2d.drawOval(posx, posy, 50, 50);
   
  } else {

   g2d.setPaint(Color.black);
   g2d.fillOval(posx, posy, 50, 50);
   g2d.setStroke(new BasicStroke(2));
   g2d.setPaint(Color.white);
   g2d.drawOval(posx, posy, 50, 50);
  }

  g2d.dispose();

  sayac++;
    }
    @Override
    public void paintComponent(Graphics g) {
       
     doDrawing(g);

  if (count < 5) {

   if (posx == 750 && posy == -50) {

    super.paintComponent(g);
    posx = 750;
    posy = 750;
    yyon = true;
    count++;
   }
  }
        
    }
    private void hareket() {
     if (posx > this.getHeight()-10) {

   yyon = false;
   posy -= 50;
   sayac++;
  }
  if (posx < 0) {

   yyon = true;
   posy -= 50;
   sayac++;
  }
  if (yyon == true) {
   posx += 50;
  }
  if (yyon == false) {
   posx -= 50;
  }
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        hareket();
        repaint();
    }
}

public class soru1 extends JFrame {

    public soru1() {

        initUI();
    }

    private void initUI() {

        setTitle("Quiz");

        add(new Surface());
        setSize(818, 800);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
             soru1 cl = new soru1();
                cl.setVisible(true);
            }
        });
    }
}
---------------------------------------------------------------------------------- Java ile düşey hareket kullanarak top sektirme animasyonu:





import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

class Surface extends JPanel implements ActionListener {
    private Timer timer;
    int DELAY = 60;
    int posy=0;
    int posx =350;
    int t = 0;
    int v = 0;
    boolean asagi = true;
    int radius=50;    
    int hiz=9;
    int artis = 9;
    
    public Surface() {
        timer = new Timer(DELAY, this);
        timer.start();
    }
    private void doDrawing(Graphics g) {
     Graphics2D g2d = (Graphics2D)g.create();
        g2d.setColor(Color.red);
        g2d.fillOval(posx,posy,radius,radius);
    }
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        doDrawing(g);
    }
    @Override
    public void actionPerformed(ActionEvent e) {    
        hareket();
        repaint();
    }
    private void hareket() {
     if (asagi) {
            t = 50;
            v = (int) 9.8*t;
            posy += hiz/2;
            hiz += artis;
        } else {            
            t = 12;
            v = (int) 9.8*t;
            hiz -= artis;
            posy -= hiz/2;            
        }        
        if (posy > 750) {
            DELAY = 0;
            asagi = false;
        }
         if (posy <= 0) {
            DELAY = 50;
            asagi = true;
        }
    }
}

public class quiztwo extends JFrame {

    public quiztwo() {

        initUI();
    }

    private void initUI() {

        setTitle("Animation");
        add(new Surface());
        setSize(800, 800);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
             quiztwo ex = new quiztwo();
                ex.setVisible(true);
            }
        });
    }
}
---------------------------------------------------------------------------------
-Java ile girilen harfleri zikzak bir şekilde aşağı doğru hareket ettiren animasyon:




import java.awt.Font;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

class Surface extends JPanel implements ActionListener{
 Timer timer;
 boolean stop = false;
 char harf;
 int posx=350;
 int posy=10;
 int i=0;
 String isim = "YIGIDOSOFT";
 String isim2 = ""; 
 
 public Surface() {
  initTimer();
 }
 
 private void initTimer() {
  timer = new Timer(100, this);
  timer.start();
 }
 
 private void doDrawing(Graphics g) {
  
  Graphics2D g2d = (Graphics2D)g.create();
  
   Font font = new Font("Arial Black", 1, 100);
   g2d.setFont(font);
   g2d.setColor(Color.red);
   harf = isim.charAt(i);
   if(!stop) {
    g2d.drawString(String.valueOf(harf), posx, posy);
   }
   //g2d.drawString(isim2, 10, 750);

 }
 private void hareket() {
  
  posy +=10;
  posx =  (int) (200* Math.sin(Math.toDegrees(posy)))+getWidth()/2;
  
  if(posy>getHeight()) {
   //isim2 += String.valueOf(a);
   if(i<4) {
    i++;    
   }else {
    i=0;
    /*stop = true;
    timer.stop();*/
   }   
   posy=0;
  }
 }
 @Override
 public void actionPerformed(ActionEvent arg0) {
  hareket();
  repaint();
  
 }
 
 
 @Override
 public void paintComponent(Graphics g) {
  super.paintComponent(g);
  doDrawing(g);
 }
 
}
 

public class twotwo extends JFrame {

    public twotwo() {

        initUI();
    }

    private void initUI() {

        setTitle("Animation");
        add(new Surface());
        setSize(800, 800);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
             twotwo ex = new twotwo();
                ex.setVisible(true);
            }
        });
    }
}
--------------------------------------------------------------------------------------------------------------------------



Yorum Gönder

Daha yeni Daha eski