1 / 14

Java – Primitve grafice 2D

Java – Primitve grafice 2D. Primitive grafice 2D frame panel punct, linie, deptunghi, elipsa, text Utilizarea primitivelor 2D in simulare. Frame si panel. primitivele grafice prezentate au la baza biblioteca Swing;

Download Presentation

Java – Primitve grafice 2D

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java – Primitve grafice 2D Primitive grafice 2D frame panel punct, linie, deptunghi, elipsa, text Utilizarea primitivelor 2D in simulare

  2. Frame si panel • primitivele grafice prezentate au la baza biblioteca Swing; • acestea se deseneza pe suprafata unei ferestre principale (necontinuta de alte ferestre) numite frame in Java; • clasa corespunzatoare acestei ferestre se numeste JFrame; ea are doar cateva metode pentru modificarea aspectului vizual, pozitiei si a marimii ferestrei principale; • frame-ul este proiectat pentru a fi container pentu alte componente, nu se deseneaza direct pe el; • desenarea directa se face pe o componenta numita panel (panou) care se adauga frame-ului • pentru a desena intr-un panel trebuie ca: • sa se defineasca o clasa ce extinde JPanel; • sa se redefineasca metoda paintComponent();

  3. Aplicatia 1 – primitive 2D Aplicatia ce urmeaza contine 3 clase: fr2D, Grafica2D si Test2D. fr2D este clasa frame, Grafica2D extinde clasa JPanel iar Test2D ruleaza aplicatia. import java.awt.Dimension; import java.awt.Rectangle; import javax.swing.JFrame; public class fr2D extends JFrame { private Grafica2D jPanel1 = new Grafica2D(); public fr2D() { try { jbInit(); } catch (Exception e) { e.printStackTrace(); } } private void jbInit() throws Exception { this.getContentPane().setLayout( null ); this.setSize(new Dimension(380, 300)); this.setTitle( "Primitive grafice 2D" ); jPanel1.setBounds(new Rectangle(0, 0, 315, 250)); jPanel1.setLayout(null); this.getContentPane().add(jPanel1, null); } }

  4. Aplicatia 1 /2 import java.awt.*; import java.awt.geom.*; import javax.swing.JPanel; public class Grafica2D extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; double leftX = 50; double topY = 50; double width = 200; double height = 150; //trasare dreptunghi Rectangle2D dreptunghi = new Rectangle2D.Double(leftX, topY, width, height); g2.draw(dreptunghi); //schimbare culoare //trasare elipsa g2.setPaint(Color.RED); Ellipse2D elipsa = new Ellipse2D.Double(); elipsa.setFrame(dreptunghi); //umplere elipsa g2.fill(elipsa); g2.draw(elipsa);

  5. Aplicatia 1/3 //desenare cerc double centruX = dreptunghi.getCenterX(); double centruY = dreptunghi.getCenterY(); double raza = 120; Ellipse2D cerc = new Ellipse2D.Double(); cerc.setFrameFromCenter(centruX, centruY, centruX+raza, centruY+raza); g2.draw(cerc); //desenare linie g2.draw(new Line2D.Double(leftX, topY, leftX+width,topY+height)); g2.setPaint(Color.BLUE); //desenare text g2.drawString("Salutare! ", (int)leftX, (int)topY-2); masina(g2); } void masina(Graphics2D g2 ) { Rectangle2D r; g2.draw(r = new Rectangle2D.Double(100,170,200,30)); g2.fill(r); g2.draw(r = new Rectangle2D.Double(150,100,150,100)); g2.fill(r); g2.setPaint(Color.ORANGE); g2.draw(r = new Rectangle2D.Double(170,110,40,55)); g2.fill(r);

  6. Aplicatia 1/4 g2.setPaint(Color.BLACK); Ellipse2D c; g2.draw(c = new Ellipse2D.Double(160,180,40,40)); g2.fill(c); g2.draw(c = new Ellipse2D.Double(240,170,50,50)); g2.fill(c); g2.setPaint(Color.WHITE); g2.draw(c = new Ellipse2D.Double(165,185,30,30)); g2.fill(c); } } • Se creează, cu mâna, clasa Grafica2D.java care moşteneşte JPanel. Partea de instrucţini grafice se scriu în metoda paintComponent(); aici se defineşte modul în care se desenează suprafaţa JPanel-ului. • 2. Din mediu, se creează frame-ul, fr2D.java. Codul clasei este creat automat de JDeveloper. Frame-ul v-a avea în centru un JPanel. Din Containers se creează un JPanel pe suprafaţa frame-ului. Iniţal, în locul lui Grafica2D apare JPanel, această modificarea lui JPanel în Grafica2D, trebuie făcută cu mâna. • 3. Se creează clasa de testare Test2D.java. Aceasta are doar metoda main din care se instanţiază frameul fr2D .

  7. Aplicatia 2 - Simulare Aplicatia ce urmeaza contine 3 clase: Bila , jpMinge si fMingeV1 . Prima oara se creeaza Bila, apoi jpMinge si la final fMingeV1. Aplicatia realizeaza simularea miscarii unui grup de bile (mingi), stocate intr-un tablou. Clasa Bila realizeaza stocarea starii fiecarie bile si detectia ciocnirii cu marginea suprafetei de desenare sau cu o alta minge, respectiv desenarea bilei. Clasa jpMinge extinde un JPanel, stocheaza toate bilele intr-un tablou si le afiseaza pe suprafata de desenare. Clasa fMingeV1 este un frame pe suprafata caruia se adauga jpMinge si care prin metoda miscaMinge() ruleaza simularea.

  8. Aplicatia 2 / 1 import java.awt.Color; import java.awt.Graphics; import java.awt.geom.Ellipse2D; import java.awt.geom.Rectangle2D; import java.util.Random; public class Bila { public double Px, Py; public double Vx, Vy; public Color culoare; private static final int XSIZE = 30; private static final int YSIZE = 30; public Bila(int w, int h) { this.Px = aleator(w); this.Py = aleator(h); this.Vx = 2 - aleator(2); //1 this.Vy = 2 - aleator(2); //1 this.culoare = new Color(aleator(255), aleator(255), aleator(255)); } public int aleator(int i) { Random rnd = new Random(); return rnd.nextInt(i); }

  9. Aplicatia 2 / 2 public void muta(Rectangle2D limite) { Px += Vx; Py += Vy; if (Px < limite.getMinX()) { Px = limite.getMinX(); Vx = -Vx; } if (Px + XSIZE >= limite.getMaxX()) { Px = limite.getMaxX() - XSIZE; Vx = -Vx; } if (Py < limite.getMinY()) { Py = limite.getMinY(); Vy = -Vy; } if (Py + YSIZE >= limite.getMaxY()) { Py = limite.getMaxY() - YSIZE; Vy = -Vy; } } public void ciocnireBila(Bila b1) { double dist, r1b1; dist = Math.sqrt((Px - b1.Px) * (Px - b1.Px) + (Py - b1.Py) * (Py - b1.Py)); r1b1 = XSIZE / 2. + b1.XSIZE / 2.;

  10. Aplicatia 2 / 3 if ((dist) <= r1b1) { Vx = -Vx; b1.Vx = -b1.Vx; Vy = -Vy; b1.Vy = -b1.Vy; } } public Ellipse2D desenare(Graphics g) { g.setColor(culoare); return new Ellipse2D.Double(Px, Py, XSIZE, YSIZE); } }

  11. Aplicatia 2 / 4 import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JPanel; public class jpMinge extends JPanel { public Bila bile[]; private int N; public jpMinge(int nrMingi) { N = nrMingi; bile = new Bila[N]; for (int i = 0; i < N; ++i) bile[i] = new Bila(400, 350); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; for (int i = 0; i < N; ++i) g2.fill(bile[i].desenare(g2)); } }

  12. Aplicatia 2 / 5 import java.awt.Color; import java.awt.Dimension; import java.awt.Rectangle; import javax.swing.BorderFactory; import javax.swing.JFrame; public class fMingeV1 extends JFrame { private final int NrBile = 5; private jpMinge jpM = new jpMinge(NrBile); public fMingeV1() { try { jbInit(); } catch (Exception e) { e.printStackTrace(); } } private void jbInit() throws Exception { this.getContentPane().setLayout(null); this.setSize(new Dimension(425, 435)); //da dimensiunil JPanel-ului jpM.setBounds(new Rectangle(0, 0, 420, 405)); //il pune in coltul din stinga sus jpM.setLocation(0, 0);

  13. //ii trage chenarul jpM.setBorder(BorderFactory.createLineBorder(Color.black, 2)); this.getContentPane().add(jpM, null); } public void miscaMinge() { try { //10000 de mutari for (int i = 1; i <= 10000; i++) { //rezolva ciocnirile intre bile for (int k1 = 0; k1 < NrBile; ++k1) for (int k2 = k1 + 1; k2 < NrBile; ++k2) jpM.bile[k1].ciocnireBila(jpM.bile[k2]); //rezolva ciocnirile cu peretii for (int j = 0; j < NrBile; ++j) jpM.bile[j].muta(jpM.getBounds()); jpM.repaint(); //timpul de asteptare in ms pina la o 9 mutare Thread.sleep(10); } } catch (InterruptedException e) { } } public static void main(String[] args) { fMingeV1 frame = new fMingeV1(); frame.setVisible(true); frame.miscaMinge(); } }

  14. Bibliografie • http://www.east.utcluj.ro/mb/mep/antal/downloads.html > Java: course, IDE (JDeveloper), JDK and JRE, JDeveloper labs. • http://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_50/jdtut_11r2_50.html > Getting Started With the JDeveloper IDE

More Related