Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An overview of java's graphics capabilities, focusing on drawing 2d shapes, controlling colors, and fonts using the java 2d api. It covers creating custom shapes, filling them with colors and patterns, and controlling the java coordinate system and graphics context.
Typology: Papers
1 / 49
X a xis
Y a xis
(0, 0)
( x , (^) y )
x
y
public Color( int r , int g , int b )
public Color( float r , float g , float b )
public int getRed() // Color class
public int getGreen() // Color class
public int getBlue() // Color class
public Color getColor() // Graphics class
public void setColor( Color c ) // Graphics class
1 // ShowColors.java 2 // Demonstrating Colors. 3 4 // Java core packages 5 import java.awt.; 6 import java.awt.event.; 7 8 // Java extension packages 9 import javax.swing.*; 10 11 public class ShowColors extends JFrame { 12 13 // constructor sets window's title bar string and dimensions 14 public ShowColors() 15 { 16 super( "Using colors" ); 17 18 setSize( 400, 130 ); 19 setVisible( true ); 20 } 21
22 // draw rectangles and Strings in different colors 23 public void paint( Graphics g ) 24 { 25 // call superclass's paint method 26 super.paint( g ); 27 28 // set new drawing color using integers 29 g.setColor( new Color( 255, 0, 0 ) ); 30 g.fillRect( 25, 25, 100, 20 ); 31 g.drawString( "Current RGB: " + g.getColor(), 130, 40 ); 32 33 // set new drawing color using floats 34 g.setColor( new Color( 0.0f, 1.0f, 0.0f ) ); 35 g.fillRect( 25, 50, 100, 20 ); 36 g.drawString( "Current RGB: " + g.getColor(), 130, 65 ); 37 38 // set new drawing color using static Color objects 39 g.setColor( Color.blue ); 40 g.fillRect( 25, 75, 100, 20 ); 41 g.drawString( "Current RGB: " + g.getColor(), 130, 90 ); 42
43 // display individual RGB values 44 Color color = Color.magenta; 45 g.setColor( color ); 46 g.fillRect( 25, 100, 100, 20 ); 47 g.drawString( "RGB values: " + color.getRed() + ", " + 48 color.getGreen() + ", " + color.getBlue(), 130, 115 ); 49 } 50 51 // execute application 52 public static void main( String args[] ) 53 { 54 ShowColors application = new ShowColors(); 55 56 application.setDefaultCloseOperation( 57 JFrame.EXIT_ON_CLOSE ); 58 } 59 60 } // end class ShowColors
1 // ShowColors2.java 2 // Demonstrating JColorChooser. 3 4 // Java core packages 5 import java.awt.; 6 import java.awt.event.; 7 8 // Java extension packages 9 import javax.swing.*; 10 11 public class ShowColors2 extends JFrame { 12 private JButton changeColorButton; 13 private Color color = Color.lightGray; 14 private Container container; 15 16 // set up GUI 17 public ShowColors2() 18 { 19 super( "Using JColorChooser" ); 20
21 container = getContentPane(); 22 container.setLayout( new FlowLayout() ); 23 24 // set up changeColorButton and register its event handler 25 changeColorButton = new JButton( "Change Color" ); 26 27 changeColorButton.addActionListener( 28 29 // anonymous inner class 30 new ActionListener() { 31 32 // display JColorChooser when user clicks button 33 public void actionPerformed( ActionEvent event ) 34 { 35 color = JColorChooser.showDialog(
36 ShowColors2.this, "Choose a color", color ); 37 38 // set default color, if no color is returned 39 if ( color == null ) 40 color = Color.lightGray; 41
42 // change content pane's background color 43 container.setBackground( color ); 44 } 45 46 } // end anonymous inner class 47 48 ); // end call to addActionListener 49 50 container.add( changeColorButton ); 51 52 setSize( 400, 130 ); 53 setVisible( true ); 54 } 55 56 // execute application 57 public static void main( String args[] ) 58 { 59 ShowColors2 application = new ShowColors2(); 60 61 application.setDefaultCloseOperation( 62 JFrame.EXIT_ON_CLOSE );
63 }
64
65 } // end class ShowColors
public final static int PLAIN // Font class
public final static int BOLD // Font class
public final static int ITALIC // Font class
public Font( String name ,^ int style , int size )
public int getStyle() // Font class
public int getSize() // Font class
public String getName() // Font class
public String getFamily() // Font class
Me thod or c o ns tant De s c riptio n public boolean isPlain() // Font class
Tests a font for a plain font style. Returns (^) true if the font is plain. public boolean isBold() // Font class
Tests a font for a bold font style. Returns true if the font is bold. public boolean isItalic() // Font class
Tests a font for an italic font style. Returns true if the font is italic. public Font getFont() // Graphics class
Returns a Font object reference representing the current font. public void setFont( Font f ) // Graphics class
Sets the current font to the font, style and size specified by the Font object reference f.
1 // Fonts.java 2 // Using fonts 3 4 // Java core packages 5 import java.awt.; 6 import java.awt.event.; 7 8 // Java extension packages 9 import javax.swing.*; 10 11 public class Fonts extends JFrame { 12 13 // set window's title bar and dimensions 14 public Fonts() 15 { 16 super( "Using fonts" ); 17 18 setSize( 420, 125 ); 19 setVisible( true ); 20 } 21
22 // display Strings in different fonts and colors 23 public void paint( Graphics g ) 24 { 25 // call superclass's paint method 26 super.paint( g ); 27 28 // set current font to Serif (Times), bold, 12pt 29 // and draw a string 30 g.setFont( new Font( "Serif", Font.BOLD, 12 ) ); 31 g.drawString( "Serif 12 point bold.", 20, 50 ); 32 33 // set current font to Monospaced (Courier), 34 // italic, 24pt and draw a string 35 g.setFont( new Font( "Monospaced", Font.ITALIC, 24 ) ); 36 g.drawString( "Monospaced 24 point italic.", 20, 70 ); 37 38 // set current font to SansSerif (Helvetica), 39 // plain, 14pt and draw a string 40 g.setFont( new Font( "SansSerif", Font.PLAIN, 14 ) ); 41 g.drawString( "SansSerif 14 point plain.", 20, 90 ); 42
43 // set current font to Serif (times), bold/italic, 44 // 18pt and draw a string 45 g.setColor( Color.red ); 46 g.setFont( 47 new Font( "Serif", Font.BOLD + Font.ITALIC, 18 ) ); 48 g.drawString( g.getFont().getName() + " " + 49 g.getFont().getSize() + 50 " point bold italic.", 20, 110 ); 51 } 52 53 // execute application 54 public static void main( String args[] ) 55 { 56 Fonts application = new Fonts(); 57 58 application.setDefaultCloseOperation( 59 JFrame.EXIT_ON_CLOSE ); 60 } 61 62 } // end class Fonts
leading
ascent
baseline descent
height
Method Description public void drawLine( int x1, int y1, int x2, int y2 )
Draws a line between the point ( x1 , y1 ) and the point ( x2 , y2 ).
public void drawRect( int x, int y, int width, int height )
Draws a rectangle of the specified (^) width and (^) height. The top- left corner of the rectangle has the coordinates ( x , y ). public void fillRect( int x, int y, int width, int height )
Draws a solid rectangle with the specified width and height. The top-left corner of the rectangle has the coordinate ( x , (^) y ). public void clearRect( int x, int y, int width, int height )
Draws a solid rectangle with the specified width and height in the current background color. The top-left corner of the rectangle has the coordinate ( x , y ). public void drawRoundRect( int x, int y, int width, int height, int arcWidth, int arcHeight )
Draws a rectangle with rounded corners in the current color with the specified (^) width and (^) height. The (^) arcWidth and (^) arcHeight determine the rounding of the corners (see Fig. 11.15).
public void fillRoundRect( int x, int y, int width, int height, int arcWidth, int arcHeight )
Draws a solid rectangle with rounded corners in the current color with the specified (^) width and (^) height. The (^) arcWidth and arcHeight determine the rounding of the corners (see Fig. 11.15).
Method Description
Draws a filledthree-dimensional rectangle in the current color withthe specified width and height. The top-left corner of the rectangle hasthe coordinates ( x , (^) y ). The rectangle appears raised whenbis true andis loweredwhenbis false.
Drawsanoval inthe current color withthe specified width and height. The bounding rectangle’s top-left corner isat the coordinates ( x , (^) y ). Theoval touchesall four sidesof thebounding rectangle at thecenter of eachside (see Fig. 11.16).
Draws a filledoval inthe current color withthe specified width and height. The bounding rectangle’s top-left corner is at the coordinates ( x, y). The oval touches all four sides of the bounding rectangle at thecenter of eachside (see Fig. 11.16).
1 // LinesRectsOvals.java 2 // Drawing lines, rectangles and ovals 3 4 // Java core packages 5 import java.awt.; 6 import java.awt.event.; 7 8 // Java extension packages 9 import javax.swing.*; 10 11 public class LinesRectsOvals extends JFrame { 12 13 // set window's title bar String and dimensions 14 public LinesRectsOvals() 15 { 16 super( "Drawing lines, rectangles and ovals" ); 17 18 setSize( 400, 165 ); 19 setVisible( true ); 20 } 21