Docsity
Docsity

Prepara tus exámenes
Prepara tus exámenes

Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity


Consigue puntos base para descargar
Consigue puntos base para descargar

Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium


Orientación Universidad
Orientación Universidad


Graficos y Java 2D - Apuntes - Programación - Informática - Parte 1, Apuntes de Informática

Apuntes del curso universitario de Informatica sobre los Graficos y los Java 2D - Caracteristicas de los Gráficos en Java: –Dibujar en 2D –Control de colores –Control de fuentes

Tipo: Apuntes

2012/2013

Subido el 07/05/2013

Mauro_88
Mauro_88 🇻🇪

4.5

(213)

619 documentos

1 / 27

Toggle sidebar

Esta página no es visible en la vista previa

¡No te pierdas las partes importantes!

bg1
1
Introducción
Caracteristicas de los Gráficos en Java:
Dibujar en 2D
Control de colores
Control de fuentes
Java 2D API
Capacidades gráficas más extensas
Dibujando en 2D complejos
Rellenar las formas con colores y patrones
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Vista previa parcial del texto

¡Descarga Graficos y Java 2D - Apuntes - Programación - Informática - Parte 1 y más Apuntes en PDF de Informática solo en Docsity!

Introducción

• Caracteristicas de los Gráficos en Java:

– Dibujar en 2D

– Control de colores

– Control de fuentes

• Java 2D API

– Capacidades gráficas más extensas

• Dibujando en 2D complejos

• Rellenar las formas con colores y patrones

Clases e Interfaces Más Usadas Classes and interfaces from the Java2D API that appear in package java.awt

Object

Color

Component

Font

FontMetrics

Graphics

Polygon

Graphics2D interface

java.awt.Paint interface java.awt.Shape interface java.awt.Stroke Classes from the Java2D API that appear in package java.awt.geom

GradientPaint

BasicStroke

TexturePaint

RectangularShape

GeneralPath

Line2D

RoundRectangle2D

Arc2D

Ellipse2D

Rectangle2D

Entorno Gráfico

• Contexto del entorno gráfico

– Se puede dibujar sobre la aplicacion.

– El objeto Graphics controla todo el entorno

• Controles para dibujar

– La Clase Graphics es abstracta

• No se puede instanciar

• Ayuda a la portabilidad de Java (Windows, Linux, Mac,…)

– En la Clase Component nace el método paint , que

recibe un objeto Graphics

public void paint( Graphics g )

– Llamado por el método repaint

Color Control

• Class Color

– Define los métodos y constantes para manipular los colores

– El color es creado por los componetes rojo, verde y azul

• RGB values

Color methods and color-related Graphics

methods

Method Description Color constructors and methods public Color( int r, int g, int b ) Creates a color based on red, green and blue components expressed as integers from 0 to 255. public Color( float r, float g, float b ) Creates a color based on red, green and blue components expressed as floating-point values from 0.0 to 1.0. public int getRed() Returns a value between 0 and 255 representing the red content. public int getGreen() Returns a value between 0 and 255 representing the green content. public int getBlue() Returns a value between 0 and 255 representing the blue content. Graphics methods for manipulating Color s public Color getColor() Returns a Color object representing the current color for the graphics context. public void setColor( Color c ) Sets the current color for drawing with the graphics context.

Láminas ShowColors.java Line 18 Line 24 Line 25 Line 26 1 // Fig. 12.5: ShowColors.java 2 // Demonstrating Colors. 3 import java.awt.; 4 import javax.swing.; 5 6 public class ShowColors extends JFrame { 7 8 // constructor sets window's title bar string and dimensions 9 public ShowColors() 10 { 11 super( "Using colors" ); 12 13 setSize( 400, 130 ); 14 setVisible( true ); 15 } 16 17 // draw rectangles and Strings in different colors 18 public void paint( Graphics g ) 19 { 20 // call superclass's paint method 21 super.paint( g ); 22 23 // set new drawing color using integers 24 g.setColor( new Color( 255, 0, 0 ) ); 25 g.fillRect( 25, 25, 100, 20 ); 26 g.drawString( "Current RGB: " + g.getColor(), 130, 40 ); 27 Paint window when application begins execution Method setColor sets color’s RGB value Method fillRect creates filled rectangle at specified coordinates using current RGB value Method drawString draws colored text at specified coordinates

Láminas ShowColors2.jav a 1 // Fig. 12.6: ShowColors2.java 2 // Choosing colors with JColorChooser. 3 import java.awt.; 4 import java.awt.event.; 5 import javax.swing.*; 6 7 public class ShowColors2 extends JFrame { 8 private JButton changeColorButton; 9 private Color color = Color.LIGHT_GRAY; 10 private Container container; 11 12 // set up GUI 13 public ShowColors2() 14 { 15 super( "Using JColorChooser" ); 16 17 container = getContentPane(); 18 container.setLayout( new FlowLayout() ); 19 20 // set up changeColorButton and register its event handler 21 changeColorButton = new JButton( "Change Color" ); 22 changeColorButton.addActionListener( 23

Láminas ShowColors2.jav a Line 29 Line 29 24 new ActionListener() { // anonymous inner class 25 26 // display JColorChooser when user clicks button 27 public void actionPerformed( ActionEvent event ) 28 { 29 color = JColorChooser.showDialog( 30 ShowColors2.this, "Choose a color", color ); 31 32 // set default color, if no color is returned 33 if ( color == null ) 34 color = Color.LIGHT_GRAY; 35 36 // change content pane's background color 37 container.setBackground( color ); 38 } 39 40 } // end anonymous inner class 41 42 ); // end call to addActionListener 43 44 container.add( changeColorButton ); 45 46 setSize( 400, 130 ); 47 setVisible( true ); 48 49 } // end ShowColor2 constructor 50 static method showDialog displays the color chooser dialog JColorChooser allows user to choose from among several colors

Láminas ShowColors2.jav a

HSB and RGB tabs of the JColorChooser

dialog

Font-related methods and constants

Method or constant Description Font constants, constructors and methods for drawing polygons public final static int PLAIN A constant representing a plain font style. public final static int BOLD A constant representing a bold font style. public final static int ITALIC A constant representing an italic font style. public Font( String name, int style, int size ) Creates a Font object with the specified font, style and size. public int getStyle() Returns an integer value indicating the current font style. public int getSize() Returns an integer value indicating the current font size. public String getName() Returns the current font name as a string. public String getFamily() Returns the font’s family name as a string. public boolean isPlain() Tests a font for a plain font style. Returns true if the font is plain. public boolean isBold() Tests a font for a bold font style. Returns true if the font is bold. public boolean isItalic() Tests a font for an italic font style. Returns true if the font is italic.

Font-related methods and constants

Method or constant Description Graphics methods for manipulating Font s public Font getFont() Returns a Font object reference representing the current font. public void setFont( Font f ) Sets the current font to the font, style and size specified by the Font object reference f.

Láminas Fonts.java Line 32 Line 37

27 // set font to Monospaced (Courier), italic, 24pt and draw a string 28 g.setFont( new Font( "Monospaced", Font.ITALIC, 24 ) ); 29 g.drawString( "Monospaced 24 point italic.", 20, 70 ); 30 31 // set font to SansSerif (Helvetica), plain, 14pt and draw a string 32 g.setFont( new Font( "SansSerif", Font.PLAIN, 14 ) ); 33 g.drawString( "SansSerif 14 point plain.", 20, 90 ); 34 35 // set font to Serif (Times), bold/italic, 18pt and draw a string 36 g.setColor( Color.RED ); 37 g.setFont( new Font( "Serif", Font.BOLD + Font.ITALIC, 18 ) ); 38 g.drawString( g.getFont().getName() + " " + g.getFont().getSize() + 39 " point bold italic.", 20, 110 ); 40 41 } // end method paint 42 43 // execute application 44 public static void main( String args[] ) 45 { 46 Fonts application = new Fonts(); 47 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 48 } 49 50 } // end class Fonts Set font to Serif 18-point bold italic Set font to SansSerif 14-point plain

Font Control

• Metricas de las fuentes

– Height

– Descent (amount character dips below baseline)

– Ascent (amount character rises above baseline)

– Leading (difference between descent and ascent)