package circulos;
import javax.swing.*;
import java.awt.*;
public class circulo extends java.applet.Applet {
int mouseX,mouseY;
boolean mouseClicked=false;
public void init(){
setBackground(Color.BLACK);
}
public boolean mouseDown(Event e,int x,int y){
mouseX=x;
mouseY=y;
mouseClicked=true;
repaint();
return true;
}
public void paint(Graphics g){
g.setColor(Color.WHITE);
if(mouseClicked){
g.fillOval(mouseX-5, mouseY-5, 10, 10);
mouseClicked=false;
}
}
public void update(Graphics g){
paint(g);
}
}
lunes, 23 de noviembre de 2009
bola de colores
import java.awt.*;
import javax.swing.*;
import java.awt.Event;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class demo extends JComponent{
private final static int ANCHO = 512;
private final static int ALTO = 384;
private final static int DIAMETRO = 20;
Color[] colores={Color.BLACK,Color.BLUE,Color.CYAN,Color.GRAY,Color.GREEN,Color.RED,Color.YELLOW,Color.PINK};
private float x, y;
private float vx, vy;
public demo() {
setPreferredSize(new Dimension(ANCHO, ALTO));
x = 50;
y = 70;
vx = 300;
vy = 400;
}
private void fisica(float dt) {
x += vx * dt;
y += vy * dt;
if (vx < 0 && x <= 0 || vx > 0 && x + DIAMETRO >= ANCHO)
vx = -vx;
if (vy < 0 && y < 0 || vy > 0 && y + DIAMETRO >= ALTO)
vy = -vy;
}
public void paint(Graphics g) {
g.setColor(colores[(int)(Math.random()*colores.length)]);
g.fillRect(0, 0, ANCHO, ALTO);
g.setColor(colores[(int)(Math.random()*colores.length)]);
g.fillRect(Math.round(x), Math.round(y), DIAMETRO, DIAMETRO);
}
private void dibuja() throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
paintImmediately(0, 0, ANCHO, ALTO);
}
});
}
public void cicloPrincipalJuego() throws Exception {
long tiempoViejo = System.nanoTime();
while (true) {
long tiempoNuevo = System.nanoTime();
float dt = (tiempoNuevo - tiempoViejo) / 1000000000f;
tiempoViejo = tiempoNuevo;
fisica(dt);
dibuja();
}
}
}
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
public class principal {
public static void main(String[] args) throws Exception {
JFrame jf = new JFrame("Demo1");
jf.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
jf.setResizable(false);
demo demo1 = new demo();
jf.getContentPane().add(demo1);
jf.pack();
jf.setVisible(true);
demo1.cicloPrincipalJuego();
}
}
import javax.swing.*;
import java.awt.Event;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class demo extends JComponent{
private final static int ANCHO = 512;
private final static int ALTO = 384;
private final static int DIAMETRO = 20;
Color[] colores={Color.BLACK,Color.BLUE,Color.CYAN,Color.GRAY,Color.GREEN,Color.RED,Color.YELLOW,Color.PINK};
private float x, y;
private float vx, vy;
public demo() {
setPreferredSize(new Dimension(ANCHO, ALTO));
x = 50;
y = 70;
vx = 300;
vy = 400;
}
private void fisica(float dt) {
x += vx * dt;
y += vy * dt;
if (vx < 0 && x <= 0 || vx > 0 && x + DIAMETRO >= ANCHO)
vx = -vx;
if (vy < 0 && y < 0 || vy > 0 && y + DIAMETRO >= ALTO)
vy = -vy;
}
public void paint(Graphics g) {
g.setColor(colores[(int)(Math.random()*colores.length)]);
g.fillRect(0, 0, ANCHO, ALTO);
g.setColor(colores[(int)(Math.random()*colores.length)]);
g.fillRect(Math.round(x), Math.round(y), DIAMETRO, DIAMETRO);
}
private void dibuja() throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
paintImmediately(0, 0, ANCHO, ALTO);
}
});
}
public void cicloPrincipalJuego() throws Exception {
long tiempoViejo = System.nanoTime();
while (true) {
long tiempoNuevo = System.nanoTime();
float dt = (tiempoNuevo - tiempoViejo) / 1000000000f;
tiempoViejo = tiempoNuevo;
fisica(dt);
dibuja();
}
}
}
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
public class principal {
public static void main(String[] args) throws Exception {
JFrame jf = new JFrame("Demo1");
jf.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
jf.setResizable(false);
demo demo1 = new demo();
jf.getContentPane().add(demo1);
jf.pack();
jf.setVisible(true);
demo1.cicloPrincipalJuego();
}
}
domingo, 1 de noviembre de 2009
public class mail1(MECHA)
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.MimeMessage;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message;
import javax.mail.internet.InternetAddress;
public class mail1 {
public mail1()throws Throwable{
Properties prop = new Properties();
//caracteristicas de la conexion
prop.setProperty("mail.smtp.host", "smtp.gmail.com");
//establecer el smtp
prop.setProperty("mail.smtp.starttls.enable","true");
//conexion sobre tunel ttls
prop.setProperty("mail.smtp.port", "587");
//puerto de salida
prop.setProperty("mail.smtp.user", "warning.mexa@gmail.com");
//nombre de la cuenta
prop.setProperty("mail.smtp.auth", "true");
//decimos que el servidor necesita una autentificacion
Session sesion= Session.getDefaultInstance(prop);
//creamos una sesion con los datos
MimeMessage mensaje = new MimeMessage(sesion);
//crear mensaje
mensaje.setFrom(new InternetAddress("warning.mexa@gmail.com"));
//emisor del correo
mensaje.addRecipient(Message.RecipientType.TO, new InternetAddress("argentos_ryu@hotmail.co.jp"));
//destinatario
mensaje.setSubject("prueba1");
mensaje.setText("prueba de mail 1");
//asunto y texto
Transport t= sesion.getTransport("smtp");
//pedimos los datos de la sesion
t.connect("warning.mexa@gmail.com","tuapapunchi");
//conectamos
t.sendMessage(mensaje, mensaje.getAllRecipients());
//mandamos el mensaje
t.close();
//cerramos la sesion;
}
}
import javax.mail.*;
import javax.mail.internet.MimeMessage;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message;
import javax.mail.internet.InternetAddress;
public class mail1 {
public mail1()throws Throwable{
Properties prop = new Properties();
//caracteristicas de la conexion
prop.setProperty("mail.smtp.host", "smtp.gmail.com");
//establecer el smtp
prop.setProperty("mail.smtp.starttls.enable","true");
//conexion sobre tunel ttls
prop.setProperty("mail.smtp.port", "587");
//puerto de salida
prop.setProperty("mail.smtp.user", "warning.mexa@gmail.com");
//nombre de la cuenta
prop.setProperty("mail.smtp.auth", "true");
//decimos que el servidor necesita una autentificacion
Session sesion= Session.getDefaultInstance(prop);
//creamos una sesion con los datos
MimeMessage mensaje = new MimeMessage(sesion);
//crear mensaje
mensaje.setFrom(new InternetAddress("warning.mexa@gmail.com"));
//emisor del correo
mensaje.addRecipient(Message.RecipientType.TO, new InternetAddress("argentos_ryu@hotmail.co.jp"));
//destinatario
mensaje.setSubject("prueba1");
mensaje.setText("prueba de mail 1");
//asunto y texto
Transport t= sesion.getTransport("smtp");
//pedimos los datos de la sesion
t.connect("warning.mexa@gmail.com","tuapapunchi");
//conectamos
t.sendMessage(mensaje, mensaje.getAllRecipients());
//mandamos el mensaje
t.close();
//cerramos la sesion;
}
}
jueves, 29 de octubre de 2009
getSistemaOperativo(): String - v2
public static String getSistemaOperativo(){
return System.getProperty("os.name");
}
return System.getProperty("os.name");
}
getSistemaOperativo()
import java.util.Properties;
import java.util.Vector;
public class busquedas {
public String getSistemaOperativo(){
String res="";
Properties p= System.getProperties();
Vector v= new Vector();
String s=p.toString();
String[] ss=s.split("=");
for(String elem:ss)
v.add(elem);
s=v.toString();
ss=s.split(" ");
v.clear();
for(String elem:ss)
v.add(elem);
for(int i=0;i
if(v.get(i).equals("Windows"))
res="Windows";
if(v.get(i).equals("Linux"))
res="Linux";
if(v.get(i).equals("Ubuntu"))
res="Ubuntu";
if(v.get(i).equals("Mac"))
res="Mac";
}
return res;
}
}
//queda probarlo en sistemas operativos distintos a windows
import java.util.Vector;
public class busquedas {
public String getSistemaOperativo(){
String res="";
Properties p= System.getProperties();
Vector
String s=p.toString();
String[] ss=s.split("=");
for(String elem:ss)
v.add(elem);
s=v.toString();
ss=s.split(" ");
v.clear();
for(String elem:ss)
v.add(elem);
for(int i=0;i
if(v.get(i).equals("Windows"))
res="Windows";
if(v.get(i).equals("Linux"))
res="Linux";
if(v.get(i).equals("Ubuntu"))
res="Ubuntu";
if(v.get(i).equals("Mac"))
res="Mac";
}
return res;
}
}
//queda probarlo en sistemas operativos distintos a windows
martes, 27 de octubre de 2009
exec(String cmd): void
//ejecuta el comando pasado como cmd en la consola del sistema
public static void exec(String cmd) {
try {
Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
System.out.println("Failed");
}
}
public static void exec(String cmd) {
try {
Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
System.out.println("Failed");
}
}
equals(Object o): boolean
//sirve para cualquier clase con el metodo hashcode(): int definido
//no probado
public boolean equals(Object o){
boolean res=false;
res=this.hashCode()==this.getClass().cast(o).hashCode();
return res;
}
//no probado
public boolean equals(Object o){
boolean res=false;
res=this.hashCode()==this.getClass().cast(o).hashCode();
return res;
}
lunes, 26 de octubre de 2009
creaInteger(String num): Integer
public static Integer creaInteger(String num){
/* Devuelve el Integer que se le pase como String
* Pensado para numeros positivos con signo (ej.: +25)
* PROBADO
*/
if(num.startsWith("+"))
num=num.replace('+','0');
return new Integer(num);
}
/* Devuelve el Integer que se le pase como String
* Pensado para numeros positivos con signo (ej.: +25)
* PROBADO
*/
if(num.startsWith("+"))
num=num.replace('+','0');
return new Integer(num);
}
Imagen fondo (clase extends JPanel)
package pruebasconimagenes;
import javax.swing.*;
import java.awt.*;
import java.applet.*;
public class PnlFondo extends javax.swing.JPanel{
public PnlFondo(){
//initComponents();
this.setSize(100,100);
Icon imgBoton = new ImageIcon(getClass().getResource("/pruebasconimagenes/bombay.jpg"));
}
public void paintComponent(Graphics g){
Dimension tamaño= getSize();
ImageIcon imagenFondo= new ImageIcon(getClass().getResource("/pruebasconimagenes/bombay.jpg"));
g.drawImage(imagenFondo.getImage(),0,0,imagenFondo.getImage().getWidth(this),imagenFondo.getImage().getHeight(this),null);
setOpaque(false);
super.paintComponent(g);
}
}
package pruebasconimagenes;
import java.applet.*;
import java.awt.*;
public class FrmPrincipal extends javax.swing.JFrame {
public FrmPrincipal(){
PnlFondo pnlFondo= new PnlFondo();
this.add(pnlFondo, BorderLayout.CENTER);
this.pack();
}
}
package pruebasconimagenes;
public class claseprincipal {
public static void main(String[]args){
FrmPrincipal uno=new FrmPrincipal();
uno.setVisible(true);
}
}
import javax.swing.*;
import java.awt.*;
import java.applet.*;
public class PnlFondo extends javax.swing.JPanel{
public PnlFondo(){
//initComponents();
this.setSize(100,100);
Icon imgBoton = new ImageIcon(getClass().getResource("/pruebasconimagenes/bombay.jpg"));
}
public void paintComponent(Graphics g){
Dimension tamaño= getSize();
ImageIcon imagenFondo= new ImageIcon(getClass().getResource("/pruebasconimagenes/bombay.jpg"));
g.drawImage(imagenFondo.getImage(),0,0,imagenFondo.getImage().getWidth(this),imagenFondo.getImage().getHeight(this),null);
setOpaque(false);
super.paintComponent(g);
}
}
package pruebasconimagenes;
import java.applet.*;
import java.awt.*;
public class FrmPrincipal extends javax.swing.JFrame {
public FrmPrincipal(){
PnlFondo pnlFondo= new PnlFondo();
this.add(pnlFondo, BorderLayout.CENTER);
this.pack();
}
}
package pruebasconimagenes;
public class claseprincipal {
public static void main(String[]args){
FrmPrincipal uno=new FrmPrincipal();
uno.setVisible(true);
}
}
TrabajarFicheros (clase)
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Vector;
public class TrabajarFicheros {
public static Vector ficherosCarpeta(String ruta){
Vector res=new Vector();
String[] aux=new File(ruta).list();
for(String elem:aux)
res.add(elem);
return res;
}
public static void creaCarpeta(String ruta,String nombre){
File dir = new File(ruta+nombre);
dir.mkdir();
}
public static void creaFichero(String ruta, String contenido)throws Throwable {
FileWriter fichero = null;
PrintWriter pw = null;
try {
fichero = new FileWriter(ruta);
pw = new PrintWriter(fichero);
pw.println(contenido);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != fichero)
fichero.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
public static Vector leeFichero(String ruta) {
File archivo = null;
FileReader fr = null;
BufferedReader br = null;
Vector res=new Vector();
try {
archivo = new File(ruta);
fr = new FileReader(archivo);
br = new BufferedReader(fr);
String linea;
while ((linea = br.readLine()) != null){
res.add(linea);
System.out.println(linea);
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
if (null != fr) {
fr.close();
}
}
catch (Exception e2) {
e2.printStackTrace();
}
}
return res;
}
public static void borraFichero(String ruta) {
File fichero = new File(ruta);
fichero.delete();
}
public static void editaFichero(String ruta, String contenido) {
FileWriter fichero = null;
PrintWriter pw = null;
try {
fichero = new FileWriter(ruta);
pw = new PrintWriter(fichero);
pw.println(contenido);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != fichero)
fichero.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Vector;
public class TrabajarFicheros {
public static Vector
Vector
String[] aux=new File(ruta).list();
for(String elem:aux)
res.add(elem);
return res;
}
public static void creaCarpeta(String ruta,String nombre){
File dir = new File(ruta+nombre);
dir.mkdir();
}
public static void creaFichero(String ruta, String contenido)throws Throwable {
FileWriter fichero = null;
PrintWriter pw = null;
try {
fichero = new FileWriter(ruta);
pw = new PrintWriter(fichero);
pw.println(contenido);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != fichero)
fichero.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
public static Vector
File archivo = null;
FileReader fr = null;
BufferedReader br = null;
Vector
try {
archivo = new File(ruta);
fr = new FileReader(archivo);
br = new BufferedReader(fr);
String linea;
while ((linea = br.readLine()) != null){
res.add(linea);
System.out.println(linea);
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
if (null != fr) {
fr.close();
}
}
catch (Exception e2) {
e2.printStackTrace();
}
}
return res;
}
public static void borraFichero(String ruta) {
File fichero = new File(ruta);
fichero.delete();
}
public static void editaFichero(String ruta, String contenido) {
FileWriter fichero = null;
PrintWriter pw = null;
try {
fichero = new FileWriter(ruta);
pw = new PrintWriter(fichero);
pw.println(contenido);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != fichero)
fichero.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
Etiquetas:
ficheros
Suscribirse a:
Entradas (Atom)