Вы находитесь на странице: 1из 13

| 

|
  



|  
    

To begin drawing in Java, we must first understand Java's  


 

ë default, the upper-left corner of a GUI component (e.g., a window)


has the coordinates (0, 0). A coordinate pair is composed of an x-
coordinate (the horizontal coordinate) and a -coordinate (the vertical
coordinate).
      !"
This section presents Graphics methods
for drawing lines, rectangles and ovals.
The methods and their parameters are
summarized in next slides. For each
drawing method that requires a width
and height parameter, the width and
height must be nonnegative values.
             !
1. public void drawLine( int x1, int 1, int xm, int m ) :
Draws a line between the point (x1, 1) and the point (xm, m).
m. public void    ( int x , int , int width, int height)
Draws a rectangle of the specified width and height. The top-left corner
of the rectangle has the coordinates (x, ) , rectangle is not filled with
this color.
3. public void #  ( int x, int , int width, int height )
Draws a filled rectangle with the specified width and height. The top
left corner of the rectangle has the coordinate (x, ). The rectangle is
filled with the Graphics object's color.
$ public void  %  ( int x, int , int width, int height, boolean b)
Draws a three-dimensional rectangle in the current color with the specified
width and height. The top-left corner of the rectangle has the coordinates
(x, ). The rectangle appears raised when b is true and lowered when b is
false. Onl the outline of the shape is drawn.
public void #%  ( int x, int , int width, int height, boolean b )
Draws a filled three-dimensional rectangle in the current color with the
specified width and height. The top-left corner of the rectangle has the
coordinates (x, ). The rectangle appears raised when b is true and lowered
when b is false.

&public void   !( int x, int , int width, int height )


Draws an oval in the current color with the specified width and height. The
bounding rectangle's top-left corner is at the coordinates (v, ). The oval
touches all four sides of the bounding rectangle at the center of each side.
Onl the outline of the shape is drawn.

' public void # !( int x, int , int width, int height )
Draws a filled oval in the current color with the specified width and height.
The bounding rectangle's top-left corner is at the coordinates (x, ). The
oval touches all four sides of the bounding rectangle at the center of each
side
·· 
  ·· 3  3" 
··         
        4 
        3"  !
    ··  
  
  
     
# 5   67 $ !
! ··
"  ··   8  8 
  
#   $ 4  8  9  4 # :  
! : $
 #  $ 8 8; #
··   %  
4 <*+=;>= ;5 $
&' 
#  ()*+ $
 
9 
 #$
 #  , $
&' 
#  ()*+ $

 # -. /0. /10. /0 $ 8 

#
 $
22 ·· 

  8 
   !  |   (   )
8 5?# @00. A 0 $
    #    *|   ··  8  ?
  !+    !      8 B #   $
#             ··
" 8  2
,       
    *-. ·· 
 2
# !  ! /    
··  A
 ,
·· 3  3",
··    A C / ,
    
    
   4 
    
   3", !
   
··  
  
 , 
  !
   
# 5 
  
#    $
 67 $ !
!
··   8  8 
 #  $
4  8  9  4 #
··   %  

:   : $
&' 
#  ()*+ $
8 8; #
 #  & D $
4 <*+=;>= ;5 $

 ,# -. @0. E0. -- $

 ,
9 
 ,#$
8,# 00. @0. E0. -- $

&' 
#  ()*+ $
·· /F ,
8 

#
$
 #  G ;( $
·· 

  8 

 /,# -. 00. E0. --.   $
8 5?# @00. A 0 $
8/,# 00. 00. E0. --. 8 $
··  8  ?
22
8 B #   $
··
" 8  2
·· 
 2
··  /
 ;
·· 3  3";
··    ;
    
    
   4 
    
   3"; !
   
··  
  
 ; 
  !
   
# 5   67 $
  
#    $
!
!
··   8  8 
 #  $
4  8  9  4 #
··   %  

:   : $
&' 
#  ()*+ $
8 8; #
g.setColor( Color.MAGENTA );
4 <*+=;>= ;5 $
g.drawOval( 195, 100, 90, 55 );

 ; ; 9 
 ;#$
g.fillOval( m90, 100, 90, 55 );
; &' 
#  ()*+ $
22
8 

#; $
·· 

  8 
8 5?# @00. A 0 $
··  8  ?
8 B #   $
··
" 8  2
·· 
 2
!
   
··  @
 ,
,
·· 3  3",
,
    
    
    
   4 
   
   3",
, !
  
 ,, 
  !
··  
  
#    $
   
# 5   67 $
!
!
 #  $
··   8  8 
&' 
#  ()*+ $
4  8  9  4 #
g.setColor( Color.CYAN );
:   : $
g.fillRoundRect( 195, 40, 90, 55, 50, 50 );
8 8; #
g.drawRoundRect( m90, 40, 90, 55, m0, m0 );
4 <*+=;>= ;5 $
22

 ,, ,;,9 
 ,,#$
,;,&' 
#  ()*+ $
8 

#,;,$
·· 

  8 
8 5?# @00. A 0 $
··  8  ?
8 B #   $
··
" 8  2
u     #  
     ·· 
 2
Drawing Arcs:
An   is drawn as a portion of an oval. Arc angles are measured in degrees Arcs
 (i.e., move along a curve) from a     b the number of degrees
specified b their    . The starting angle indicates in degrees where the arc
begins. The arc angle specifies the total number of degrees through which the arc
sweeps.

Graphics methods for drawing arcs.


public void  u ( int x, int , int width, int height, int startAngle, int arcAngle )
Draws an arc relative to the bounding rectangle's top-left x and coordinates with the
specified width and height. The arc segment is drawn starting at startAngle and sweeps arcAngle
degrees.
public void #u ( int x, int , int width, int height, int startAngle, int arcAngle )
Draws a filled arc (i.e., a sector) relative to the bounding rectangle's top-left x and
coordinates with the specified width and height. The arc segment is drawn starting at startAngle
and sweeps arcAngle degrees.
·· Example 5 : M Arcs.java
·· Drawing arcs. ·· start at 0 and sweep -m 0 degrees
import java.awt.Color; g.setColor( Color.RED );
import java.awt.Graphics; g.drawRect( 185, 35, 80, 80 );
import javax.swing.JPanel;
public class M Arcs extends JPanel g.setColor( Color.ëLACK );
{ g.drawArc( 185, 35, 80, 80, 0, -m 0 );
·· draw rectangles and arcs
public void paintComponent( Graphics g ) ·· start at 0 and sweep 360 degrees
{ g.fillArc( 15, 1m0, 80, 40, 0, 360 );
super.paintComponent( g );
·· start at 0 and sweep 360 degrees ·· start at m 0 and sweep -90 degrees
g.setColor( Color.RED ); g.fillArc( 100, 1m0, 80, 40, m 0, -90 );
g.drawRect( 15, 35, 80, 80 );
g.setColor( Color.ëLACK ); ·· start at 0 and sweep -m 0 degrees
g.drawArc( 15, 35, 80, 80, 0, 360 ); g.fillArc( 185, 1m0, 80, 40, 0, - m 0 );
·· start at 0 and sweep 110 degrees } ·· end method paintComponent
g.setColor( Color.RED ); } ·· end class ArcsJPanel
g.drawRect( 100, 35, 80, 80 );
g.setColor( Color.ëLACK );
g.drawArc( 100, 35, 80, 80, 0, 110 );
·· Creating JFrame to displa arcs.
·· DrawArcs.java - Drawing arcs.
import javax.swing.JFrame;

public class DrawArcs


{
·· execute application
public static void main( String args[] )
{
·· create frame for ArcsJPanel
JFrame frame = new JFrame( "Drawing Arcs" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

M Arcs arcs = new M Arcs(); ·· create ArcsJPanel


frame.add( arcs ); ·· add arcsJPanel to frame
frame.setSize( 300, m10 ); ·· set frame size
frame.setVisible( true ); ·· displa frame
} ·· end main
} ·· end class DrawArcs
Î 

1. Write an application that draws an 8-b -8 grid. Use Graphics


method drawLine.
m. Write an application that draws a 10-b -10 grid. Use the
Graphics method drawRect.
3. Write an application that draws a series of eight concentric
circles. The circles should be separated b 10 pixels. Use
Graphics method drawArc.
4. Write a program that draws a die on the screen. As shown in the
figure.

Похожие интересы