Muallim-i Ali Posted July 3, 2007 Share Posted July 3, 2007 Yazan : Aybars UĞUR JAVA 2D ÖRNEK 1 : Dikdörtgenler ve Yazıları import java.awt.*;import java.awt.geom.*;public class ornek1 extends ApplicationFrame { public static void main(String[] args) { ornek1 f = new ornek1(); f.setTitle("Örnek 1.1"); f.setSize(350,200); f.center(); f.setVisible(true); } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; double x=30, y=80, w=50, h=75; Rectangle2D r1 = new Rectangle2D.Double(x,y,w,h); g2.draw(r1); Rectangle2D r2 = new Rectangle2D.Double(x+75,y,w,h); g2.fill(r2); Font font1 = new Font("Serif",Font.PLAIN,15); g2.setFont(font1); g2.drawString("Dikdörtgen",30,75); g2.drawString("Içi Dolu",105,58); g2.drawString("Dikdörtgen",105,75); g2.setColor(Color.blue); RoundRectangle2D rr1 = new RoundRectangle2D.Double(x+150,y,w,h,30,30); g2.draw(rr1); RoundRectangle2D rr2 = new RoundRectangle2D.Double(x+225,y,w,h,40,20); g2.fill(rr2); Font font2 = new Font("Serif",Font.PLAIN,10); g2.setFont(font2); g2.drawString("Yuvarlatilmis",(int)x+150,65); g2.drawString("Dikdörtgen",(int)x+150,75); g2.drawString("Içi Dolu",(int)x+225,55); g2.drawString("Yuvarlatilmis",(int)x+225,65); g2.drawString("Dikdörtgen",(int)x+225,75); } }public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; double x=30, y=80, w=50, h=75; Rectangle2D r1 = new Rectangle2D.Double(x,y,w,h); g2.draw(r1); Rectangle2D r2 = new Rectangle2D.Double(x+75,y,w,h); g2.fill(r2); Font font1 = new Font("Serif",Font.PLAIN,15); g2.setFont(font1); g2.drawString("Dikdörtgen",30,75); g2.drawString("Içi Dolu",105,58); g2.drawString("Dikdörtgen",105,75); g2.setColor(Color.blue); RoundRectangle2D rr1 = new RoundRectangle2D.Double(x+150,y,w,h,30,30); g2.draw(rr1); RoundRectangle2D rr2 = new RoundRectangle2D.Double(x+225,y,w,h,40,20); g2.fill(rr2); Font font2 = new Font("Serif",Font.PLAIN,10); g2.setFont(font2); g2.drawString("Yuvarlatilmis",(int)x+150,65); g2.drawString("Dikdörtgen",(int)x+150,75); g2.drawString("Içi Dolu",(int)x+225,55); g2.drawString("Yuvarlatilmis",(int)x+225,65); g2.drawString("Dikdörtgen",(int)x+225,75); } [/CODE][b]JAVA 2D ÖRNEK 2 : Elips, Yay ve Gradient (Renk Geçişi)[/b][CODE]public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; GradientPaint gp = new GradientPaint(75,75,Color.pink, 95,95,Color.blue,true); g2.setPaint(gp); double x=30, y=80, w=50, h=75; Ellipse2D e1 = new Ellipse2D.Double(x,y,w,h); g2.draw(e1); Ellipse2D e2 = new Ellipse2D.Double(x+75,y,w,h); g2.fill(e2); Font font1 = new Font("Serif",Font.PLAIN,30); g2.setFont(font1); g2.drawString("Elips ve Yay",50,75); Arc2D arc1 = new Arc2D.Double(x+150,y,w,h,30,120,Arc2D.PIE); g2.draw(arc1); Arc2D arc2 = new Arc2D.Double(x+225,y,w,h,-180,120,Arc2D.OPEN); g2.fill(arc2); }[/CODE][b]JAVA 2D ÖRNEK 3 : Eğriler[/b][CODE]public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; QuadCurve2D qc1 = new QuadCurve2D.Double(10,50,20,100,110,50); g2.draw(qc1); QuadCurve2D qc2 = new QuadCurve2D.Double(10,50,120,120,110,50); g2.draw(qc2); QuadCurve2D qc3 = new QuadCurve2D.Double(60,150,0,0,110,190); g2.fill(qc3); CubicCurve2D cc = new CubicCurve2D.Double(160,30,170,80,280,0,250,190); g2.setStroke(new BasicStroke(8)); g2.draw(cc); } [/CODE][b]JAVA 2D ÖRNEK 4 : Dönüşümler[/b][CODE]public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; AffineTransform at = new AffineTransform(); at.translate(100,100); g2.transform(at); QuadCurve2D qc1 = new QuadCurve2D.Double(0,0,20,15,0,50); QuadCurve2D qc2 = new QuadCurve2D.Double(0,0,-20,15,0,50); Color c1 = new Color(175,255,0); g2.setPaint(c1); for(int i=0; i<6; ++i) { at = AffineTransform.getRotateInstance(Math.PI/6); g2.transform(at); g2.fill(qc1); g2.fill(qc2); }; for(int i=0; i<4; ++i) { at = AffineTransform.getTranslateInstance(-40,0); g2.transform(at); at = AffineTransform.getScaleInstance(1.2,1.2); g2.transform(at); g2.fill(qc1); g2.fill(qc2); }; Font f = new Font("Serif",Font.PLAIN,20); g2.setFont(f); at.shear(.3,0); g2.transform(at); g2.drawString("Transformation",0,-20);}[/CODE]Şekil 4 : Dönüşümler (Transformations) : Döndürme (Rotation), Yer Değiştirme (Translation), Ölçeklendirme (Scaling) ve Shear[b]JAVA 2D ÖRNEK 5 : Resim görüntüleme, Alfa parametresi, yazılara ve şekillere doku kaplama[/b][CODE]public class ornek5_1 extends Component{ public static void main(String[] args) { ApplicationFrame f = new ApplicationFrame("Örnek 5"); f.setLayout(new BorderLayout()); Component c = new ornek5_1(); f.add(c, BorderLayout.CENTER); f.setSize(525,400); f.center(); f.setVisible(true); } private BufferedImage mimage; private Image image1,image2; public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; if(mimage==null) createOffscreenImage(); g2.drawImage(mimage,0,0,this); // Araba resmi image1 = Toolkit.getDefaultToolkit().getImage("cab.gif"); g2.drawImage(image1,0,0,null); Composite c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,.5f); g2.setComposite(c); // Alfa parametresi ile yarı saydam kaplan resmi image2 = Toolkit.getDefaultToolkit().getImage("BigTiger.gif"); g2.drawImage(image2,100,50,180,180,this); } private void createOffscreenImage() { Dimension d = getSize(); int w = d.width, h = d.height; // Dünya haritası resmi mimage = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB); Graphics2D g2 = mimage.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); try { String filename = "earth.jpg"; InputStream in = getClass().getResourceAsStream(filename); JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in); BufferedImage image = decoder.decodeAsBufferedImage(); in.close(); g2.drawImage(image,0,0,w,h,null); } catch (Exception e) { System.out.print(e); } // Elipsler g2.setStroke(new BasicStroke(2)); Color[] colors = { Color.red, Color.blue, Color.green }; for(int i=-32; i<40; i+=8) { g2.setPaint(colors[Math.abs(i)%3]); g2.drawOval(i,i,w-i*2,h-i*2); }; // Resim dokusu kaplanmış bir kare ve yazı Rectangle2D tr = new Rectangle2D.Double(50,10, 50,50); g2.setFont(new Font("Serif",Font.BOLD,94)); TexturePaint tp = new TexturePaint(mimage,tr); g2.setPaint(tp); g2.drawString("WORLD",50,250); g2.fill(tr); }}[/CODE]Şekil 5 : Resim görüntüleme (dünya haritası, kaplan ve araba), Alfa parametresi (yarı saydam kaplan resmi), yazılara (ortadaki world yazısı) ve şekillere (sol üst köşedeki küçük dikdörtgen dünya haritası) doku kaplama[b]JAVA 2D ÖRNEK 6 : Fare kullanarak bir çizgi ve elipsin hareket ettirilmesini sağlayan Java2D programı[/b][CODE]import java.awt.*;import java.awt.event.*;import java.awt.geom.*;public class ornek6 extends ApplicationFrame implements MouseListener, MouseMotionListener{ public static void main(String[] args) { new ornek6(); } protected Point2D[] noktalar; protected Point2D etkinnokta; public ornek6() { super("FARE v1.0"); setSize(250,250); center(); noktalar = new Point2D[4]; noktalar[0] = new Point2D.Double(50,50); noktalar[1] = new Point2D.Double(100,100); noktalar[2] = new Point2D.Double(50,150); noktalar[3] = new Point2D.Double(100,200); etkinnokta = null; // Fare İşlemlerinin Dinlenmesi addMouseListener(this); addMouseMotionListener(this); setVisible(true); } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; Line2D cizgi1 = new Line2D.Double(noktalar[0],noktalar[1]); g2.draw(cizgi1); Ellipse2D elips1 = new Ellipse2D.Double( noktalar[2].getX(), noktalar[2].getY(), noktalar[3].getX()-noktalar[2].getX(), noktalar[3].getY()-noktalar[2].getY()); g2.draw(elips1); for(int i=0; i>noktalar.length;i++) { if(noktalar[i]==etkinnokta) g2.setPaint(Color.red); else g2.setPaint(Color.blue); g2.fill(getControlPoint(noktalar[i])); } } protected Shape getControlPoint(Point2D p) { int kenar = 4; return new Rectangle2D.Double( p.getX()-kenar/2, p.getY()-kenar/2, kenar,kenar); } public void mouseClicked(MouseEvent me) {} public void mousePressed(MouseEvent me) { etkinnokta = null; for(int i=0; i<noktalar.length; ++i) { Shape s = getControlPoint(noktalar[i]); if(s.contains(me.getPoint())) { etkinnokta = noktalar[i]; break; } } repaint(); } public void mouseReleased(MouseEvent me) {} public void mouseMoved(MouseEvent me) {} public void mouseDragged(MouseEvent me) { if(etkinnokta!=null) { etkinnokta.setLocation(me.getPoint()); repaint(); } } public void mouseEntered(MouseEvent me) {} public void mouseExited(MouseEvent me) {}}[/CODE]Şekil 6 : Bir çizginin uç noktalarının yerlerinin ve bir elipsin boyutlarının değiştirilmesini sağlayan Java 2D programının Applet penceresi Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.