Grupo CUYS (Como Usted Ya Sabe) | WWW.CUYS.COM.AR Fac. Cs. Exactas (UNICEN) | Pág. 1 Visualización Computacional I Utilizando Eclipse Helios Versión: 3.6.0 y Jigloo, vamos a crear un proyecto gráfico: Creamos un nuevo proyecto, luego vamos a File→New→Other (Ctrl+N). Importante: no crear clase que contenga el método main por ahora. Vamos a GUI Forms→Swing→Jframe Grupo CUYS (Como Usted Ya Sabe) | WWW.CUYS.COM.AR Fac. Cs. Exactas (UNICEN) | Pág. 2 Acá si creamos el método main. Grupo CUYS (Como Usted Ya Sabe) | WWW.CUYS.COM.AR Fac. Cs. Exactas (UNICEN) | Pág. 3 Agregamos un JPanel Grupo CUYS (Como Usted Ya Sabe) | WWW.CUYS.COM.AR Fac. Cs. Exactas (UNICEN) | Pág. 4 En el código escribimos: public void paint(Graphics gr){ Graphics g = jPanel1.getGraphics(); jPanel1.setSize(500, 500); BufferedImage bf = new BufferedImage(jPanel1.getWidth(), jPanel1.getHeight(), BufferedImage.TYPE_INT_RGB); for(int i = 0; i < jPanel1.getHeight(); i++) for(int j = 0; j < jPanel1.getWidth(); j++){ Color c = new Color(255,0,0); bf.setRGB(i, j, c.getRGB()); } g.drawImage(bf, 0, 0, this); this.repaint(); } Sobrescribiendo la función paint, lo que hacemos es que el jFrame se pinte como nosotros queremos. Lo que obtenemos con esta función es lo siguiente: Grupo CUYS (Como Usted Ya Sabe) | WWW.CUYS.COM.AR Fac. Cs. Exactas (UNICEN) | Pág. 5 Para pintar un degradé lo que hacemos es cambiar el color a medida que pintamos creando una función: public Color getColor1(int i, int j, int iMax, int jMax, Color cInic, Color cFin){ int r = cInic.getRed() * (iMax-i)/iMax + cFin.getRed() * i/iMax; int g = cInic.getGreen() * (iMax-i)/iMax + cFin.getGreen() * i/iMax; int b = cInic.getBlue() * (iMax-i)/iMax + cFin.getBlue() * i/iMax; return new Color(r,g,b); } public void paint(Graphics gr){ Graphics g = jPanel1.getGraphics(); jPanel1.setSize(500, 500); BufferedImage bf = new BufferedImage(jPanel1.getWidth(), jPanel1.getHeight(), BufferedImage.TYPE_INT_RGB); Color rojo = new Color(255,0,0); Color azul = new Color(0,0,255); for(int i = 0; i < jPanel1.getHeight(); i++) for(int j = 0; j < jPanel1.getWidth(); j++){ Color c = getColor1(i,j,jPanel1.getHeight(),jPanel1.getWidth(),rojo,azul); bf.setRGB(i, j, c.getRGB()); } g.drawImage(bf, 0, 0, this); this.repaint(); } Grupo CUYS (Como Usted Ya Sabe) | WWW.CUYS.COM.AR Fac. Cs. Exactas (UNICEN) | Pág. 6 Y ahora cambiando la función podemos obtener diferentes degradés: public Color getColor2(int i, int j, int iMax, int jMax, Color cInic, Color cFin){ int r = cInic.getRed() * (jMax-j)/jMax + cFin.getRed() * j/jMax; int g = cInic.getGreen() * (jMax-j)/jMax + cFin.getGreen() * j/jMax; int b = cInic.getBlue() * (jMax-j)/jMax + cFin.getBlue() * j/jMax; return new Color(r,g,b); } Grupo CUYS (Como Usted Ya Sabe) | WWW.CUYS.COM.AR Fac. Cs. Exactas (UNICEN) | Pág. 7 public Color getColor3(int i, int j, int iMax, int jMax, Color cInic, Color cFin){ int ij = i + j; int ijMax = iMax + jMax; int r = cInic.getRed() * (ijMax-ij)/ijMax + cFin.getRed() * ij/ijMax; int g = cInic.getGreen() * (ijMax-ij)/ijMax + cFin.getGreen() * ij/ijMax; int b = cInic.getBlue() * (ijMax-ij)/ijMax + cFin.getBlue() * ij/ijMax; return new Color(r,g,b); }