



















Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity
Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium
Prepara tus exámenes
Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity
Prepara tus exámenes con los documentos que comparten otros estudiantes como tú en Docsity
Encuentra los documentos específicos para los exámenes de tu universidad
Estudia con lecciones y exámenes resueltos basados en los programas académicos de las mejores universidades
Responde a preguntas de exámenes reales y pon a prueba tu preparación
Consigue puntos base para descargar
Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium
Comunidad
Pide ayuda a la comunidad y resuelve tus dudas de estudio
Ebooks gratuitos
Descarga nuestras guías gratuitas sobre técnicas de estudio, métodos para controlar la ansiedad y consejos para la tesis preparadas por los tutores de Docsity
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
1 / 27
Esta página no es visible en la vista previa
¡No te pierdas las partes importantes!




















Clases e Interfaces Más Usadas Classes and interfaces from the Java2D API that appear in package java.awt
java.awt.Paint interface java.awt.Shape interface java.awt.Stroke Classes from the Java2D API that appear in package java.awt.geom
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
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.
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