Toolkit miPantalla = Toolkit.getDefaultToolkit();
Dimension dimensionPantalla = miPantalla.getScreenSize();
int ancho = dimensionPantalla.width;
int altura = dimensionPantalla.height;
this.setSize(ancho / 3, altura / 3);
this.setLocation(ancho / 4, altura / 4);
PARA ESCOGER UN ARCHIVO DE LA PC
//creamos una instancia de jfilechooser
JFileChooser fc = new JFileChooser();
//escribimos el nombre del titulo
fc.setDialogTitle("Elige un fichero");
//indicamos que solo se puedan elegir ficheros
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
///creamos un filtro para jfilechoose
FileNameExtensionFilter filtro = new FileNameExtensionFilter("*.txt", "txt");
fc.setFileFilter(filtro);
int eleccion = fc.showSaveDialog(this);
if (eleccion == JFileChooser.APPROVE_OPTION) {
texto.setText(fc.getSelectedFile().getPath());
}
PARA CREAR CUADRADOS DENTRO DEL JPANEL AUTOMATICAMENTE
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package graficos;
import java.awt.*;
import javax.swing.*;
/**
*
* @author USUARIO
*/
public class Graficos {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
marcoColor marco = new marcoColor();
marco.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
marco.setVisible(true);
}
}
class marcoColor extends JFrame {
public marcoColor() {
setTitle("Prueba de colores");
Toolkit miPantalla = Toolkit.getDefaultToolkit();
Dimension dimension = miPantalla.getScreenSize();
double ancho = dimension.getWidth();
double altura = dimension.getHeight();
System.out.println("altura " + altura + " ancho " + ancho);
setSize(800, 500);
setLocation(200, 200);
laminaConColor lamina = new laminaConColor();
panelMenu panelMenu = new panelMenu();
add(lamina);
add(panelMenu);
panelMenu.setBackground(Color.gray);
lamina.setFont(new Font("Courier", Font.ITALIC, 20));
}
}
class laminaConColor extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Rectangle2D rectangulo = new Rectangle2D.Double(200, 200, 200, 200);
g2.draw(rectangulo);
g2.draw(new Line2D.Double(200, 200, 400, 400));
Ellipse2D eclipse = new Ellipse2D.Double();
eclipse.setFrame(rectangulo);
g2.draw(eclipse);
//dibujar encima del cuadrado+
double altura = rectangulo.getCenterY();
double ancho = rectangulo.getCenterX();
int radio = 200;
Ellipse2D eclipseGrande = new Ellipse2D.Double();
eclipseGrande.setFrameFromCenter(ancho, altura, ancho + radio, altura + radio);
g2.draw(eclipseGrande);
}
}
class panelMenu extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
dibujarMenuAbove(g2);
// Font miFuente = new Font("Courier", Font.BOLD, 15);
//g2.setFont(miFuente);
g2.setColor(Color.blue);
g2.drawString("hola como estas", 300, 200);
// g2.setFont(new Font("Arial", Font.ITALIC, 15));
//g2.setColor(new Color(12, 114, 21).brighter());
g2.drawString("hola que tal tu dia ", 400, 500);
dibujarPanelPadre(g2);
dibujarDentroPadre(g2);
}
void dibujarMenuAbove(Graphics2D g2) {
Rectangle2D rectangulo;
double x = 30;
double y = 30;
double ancho = 207.6666;
double altura = 40;
for (int i = 0; i < 6; i++) {
rectangulo = new Rectangle2D.Double(x, y, ancho, altura);
g2.setPaint(Color.BLUE);
g2.draw(rectangulo);
g2.setPaint(Color.WHITE);
g2.fill(rectangulo);
x += ancho + 10;
}
}
void dibujarPanelPadre(Graphics2D g2) {
Rectangle2D rectanble = new Rectangle2D.Double(30, 100, 237.7, 768);
g2.draw(rectanble);
}
void dibujarDentroPadre(Graphics2D g2) {
Rectangle2D rectangulo;
double x = 98.8;
double y = 130;
double ancho = 100;
double altura = 107.6;
for (int i = 0; i < 6; i++) {
rectangulo = new Rectangle2D.Double(x, y, ancho, altura);
g2.setPaint(Color.RED);
g2.fill(rectangulo);
y += altura + 20;
}
}
}
PARA SABER LAS LETRAS QUE TIENE EL SISTEMA OPERATIVO
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package graficos;
import java.awt.GraphicsEnvironment;
/**
*
* @author USUARIO
*/
public class paraSaberLetrasEnLaPcQueTienes {
public static void main(String[] args) {
String[] letrasSistema = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
for (String nombre : letrasSistema) {
System.out.println(nombre);
}
}
}
CREANDO UN RELOJ
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package reloj;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.Timer;
/**
*
* @author USUARIO
*/
public class Reloj {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Reloj1 hora = new Reloj1();
hora.enMarcha(3000, true);
JOptionPane.showMessageDialog(null, "Pulse aceptar para finalizar");
System.exit(0);
}
}
class Reloj1 {
public void enMarcha(int intervalo, final boolean sonido) {
class DameHora implements ActionListener {
@Override
public void actionPerformed(ActionEvent ae) {
Date hora = new Date();
System.out.println("La hora es :" + hora);
if (sonido) {
Toolkit.getDefaultToolkit().beep();
}
}
}
ActionListener oyente = new DameHora();
Timer tiempo = new Timer(1000, oyente);
tiempo.start();
}
}






0 comentarios:
Publicar un comentario