Easy Graphics Programming Tutorial

Easy Graphics Programming Tutorial
Slide Note
Embed
Share

Learn how to create interactive graphics in Java using the Graphics class. Explore simple methods like drawLine, setColor, and drawRect to draw shapes and text on a JPanel. Follow a step-by-step guide to implementing paintComponent for custom drawing in a JFrame. Discover the power of Graphics class in creating visually appealing applications.

  • Java
  • Graphics Programming
  • JPanel
  • JFrame
  • Graphics Class

Uploaded on Feb 20, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent is invoked You cannot directly invoke paintComponent either, but it is called whenever a JPanel is made visible on a JFrame or whenever the JFrame is moved You can also invoke paintComponent by calling the panel s repaint( ) method as in panel.repaint( ); Graphics has a number of easy to use methods like drawLine and setColor To draw something, specify locations using x, y (int) coordinates 0,0 is the upper left hand corner rather than either the middle or the lower left hand corner, x and y increase as you move to the right and down respectively

  2. Creating a Graphics Program To use a Graphics object, create a JFrame, insert a JPanel and in your JPanel class implement the paintComponent method import java.awt.*; import javax.swing.*; public class GraphicsDrawer { public static final int X_SIZE=600,Y_SIZE=600; public static void main(String[] args) { JFrame frame=new JFrame(""); frame.setSize(X_SIZE,Y_SIZE); GraphicsPanel p=new GraphicsPanel(); frame.add(p); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public static class GraphicsPanel extends JPanel { public void paintComponent(Graphics g) { // Graphics code here } }

  3. java.awt.Graphics Graphics Methods +setColor(color: Color): void +setFont(font: Font): void +drawString(s: String, x: int, y: int): void +drawLine(x1: int, y1: int, x2: int, y2: int): void +drawRect(x: int, y: int, w: int, h: int): void Sets a new color for subsequent drawings. Sets a new font for subsequent drwings. Draws a string starting at point (x, y). Draws a line from (x1, y1) to (x2, y2). Draws a rectangle with specified upper-left corner point at (x, y) and width w and height h. Draws a filled rectangle with specified upper-left corner point at (x, y) and width w and height h. Draws a round-cornered rectangle with specified arc width aw and arc height ah. Draws a filled round-cornered rectangle with specified arc width aw and arc height ah. Draws a 3-D rectangle raised above the surface or sunk into the surface. Draws a filled 3-D rectangle raised above the surface or sunk into the surface. Draws an oval bounded by the rectangle specified by the parameters x, y, w, and h. Draws a filled oval bounded by the rectangle specified by the parameters x, y, w, and h. Draws an arc conceived as part of an oval bounded by the rectangle specified by the parameters x, y, w, and h. Draws a filled arc conceived as part of an oval bounded by the rectangle specified by the parameters x, y, w, and h. Draws a closed polygon defined by arrays of x and y coordinates. Each pair of (x[i], y[i]) coordinates is a point. Draws a filled polygon defined by arrays of x and y coordinates. Each pair of (x[i], y[i]) coordinates is a point. Draws a closed polygon defined by a Polygon object. Draws a filled polygon defined by a Polygon object. Draws a polyline defined by arrays of x and y coordinates. Each pair of (x[i], y[i]) coordinates is a point. +fillRect(x: int, y: int, w: int, h: int): void +drawRoundRect(x: int, y: int, w: int, h: int, aw: int, ah: int): void +fillRoundRect(x: int, y: int, w: int, h: int, aw: int, ah: int): void +draw3DRect(x: int, y: int, w: int, h: int, raised: boolean): void +fill3DRect(x: int, y: int, w: int, h: int, raised: boolean): void +drawOval(x: int, y: int, w: int, h: int): void When drawing anything, the color used is the color last set in setColor +fillOval(x: int, y: int, w: int, h: int): void The default color is black +drawArc(x: int, y: int, w: int, h: int, startAngle: int, arcAngle: int): void +fillArc(x: int, y: int, w: int, h: int, startAngle: int, arcAngle: int): void +drawPolygon(xPoints: int[], yPoints: int[], nPoints: int): void +fillPolygon(xPoints: int[], yPoints: int[], nPoints: int): void +drawPolygon(g: Polygon): void +fillPolygon(g: Polygon): void +drawPolyline(xPoints: int[], yPoints: int[], nPoints: int): void

  4. Drawing/Filling Rectangles/Ovals For a line, you specify the two endpoints But for a rectangle or oval, you specify the upper left hand corner and the width and height For an oval or rounded oval, the x, y coordinate is the upper left hand corner of the bounding box For a rounded oval, you also specify the amount of curvature at the corners as aw and ah (the width and height arcs of the corners) (x, y) ah/2 aw/2 h w (x, y) h w

  5. Other Shapes of Note Arcs require the x, y coordinate and width, height of the bounding box and then the starting arc s angle (0 meaning horizontal to the right) and arc s size as an angle (360 degrees is a full circle, 90 would be of a circle) g.setColor(Color.blue); g.fillArc(10,10,100,100,0,45); g.setColor(Color.red); g.fillArc(10,10,100,100,180,45); Polygons are drawn by specifying two arrays of x and y coordinates and the number of points to draw from those arrays int x[]={10,20,30,30,20,10}; int y[]={20,10,20,30,40,30}; int n=6; g.setColor(Color.blue); g.drawPolygon(x,y,n); Polylines are the same as polygons except that the first and last points are not connected

  6. Drawing Images An image is not the same as an Icon from chapter 12 (recall icons can be placed on JButtons or JLabels) We convert an Icon into an Image as follows ImageIcon icon = new ImageIcon(filename); Image anImage = icon.getImage( ); g.drawImage(anImage, x, y, width, height, Color.white, this); The drawImage method receives parameters of the image the x, y coordinate and its width and height if height/width are omitted the actual size of the image is used a color to use in the background if any pixels in the image are transparent (optional) an object that is used as an observer for the image (this object which will receive notifications as the image is constructed) use this Remember that the only image types are jpg, gif and png if the file is not found, you do not receive any error or exception notice

  7. Drawing Strings To output text graphically, use drawString g.drawString(string, x, y); You can set the font of the text using setFont In order to better format how the text appears, there is also the FontMetrics class We can use this to measure the width and height of text in the given font so that we can better determine the x,y coordinate to draw the text First, get the font type s metrics using getFontMetrics(font) and then you can pass this variable methods as shown below getLeading( ) getHeight() getAscent() getDescent( )

  8. Example Display Welcome to Java in the center of a JPanel Obtain width/height of the JPanel Use getWidth( ) and getHeight( ) assuming that we store these values in variables w and h Obtain the font metric using FontMetrics fm = g.getFontMetrics( ); note that getFontMetrics is of the font currently set for the Graphics object, we may have preceded this with g.setFont(new Font( )); Determine width of the String with sw=fm.stringWidth( Welcome to Java ); Determine height that the String will require using as=fm.getAscent( ); we do not supply the string here, the ascent is based solely on the font being used as part of the FontMetric g.drawString( Welcome to Java , w/2-sw2/2, h/2+as/2);

  9. Recursive Graphics Fractal images are generated recursively Since they are created by a mathematical equation, we can zoom in on any part and never lose focus (that is, the image never blurs) Rather than seeing a true fractal design, we will do a couple of simplistic recursive designs Recursive I Draw an I and then for each end of the line recursively draw Is 3-line design From our starting point, draw 3 lines (one up, one at each of 45 and -45 degrees from the starting point) and recursively do the same note that in both cases, we will eventually reach a point where the sizes used for the endpoints of our lines are equal, this will be our base case to stop recursing (no point in further recursion as we can t draw any smaller pixels)

  10. Drawing Recursive Is Given two points, x1, y1 and x2, y2, we want to draw a line between them and then recursively add 2 lines at the end points We need to alternate drawing horizontal and vertical lines we do this by setting x1=x2 or y1=y2 We also have a special case for our first output in that we will just draw a single top-down line We will use three conditions If c==0 then draw a single line and recurse with c=1 If x1==x2 && y1==y2 stop recursing Otherwise if c is even, draw a horizontal line and recurse twice with c+1 and the same y values and if c is odd, draw a vertical line and recurse twice with c+1 and the same x values

  11. public static class DrawPanel extends Jpanel public void paintComponent(Graphics g) { int x1=300, y1=200, x2=300, y2=500; create(g, x1,y1, x2, y2, 0, 300); } public void create(Graphics g, int x1, int y1, int x2, int y2, int c, int size) { if(c==0) { g.drawLine(x1,y1,x2,y2); create(g,x1-size/2,y1,x2+size/2,y1,c+1,size/2); create(g,x1-size/2,y2,x2+size/2,y2,c+1,size/2); } else if(x1!=x2||y1!=y2) { if(c%2==0) { g.drawLine(x1,y1,x2,y2); create(g,x1-size/2,y1,x1+size/2,y1,c+1,size/2); create(g,x2-size/2,y2,x2+size/2,y2,c+1,size/2); } else { g.drawLine(x1,y1,x2,y2); create(g,x1,y1-size/2,x1,y1+size/2,c+1,size/2); create(g,x2,y1-size/2,x2,y1+size/2,c+1,size/2); } } } } {

  12. public static class DrawPanel extends JPanel { public void paintComponent(Graphics g) { draw(g, 350,300,250,0); } public void draw(Graphics g, int x, int y, int size, int c) { if(c<8) { g.drawLine(x-size/4,y-size/4,x,y); g.drawLine(x,y-size/2,x,y); g.drawLine(x+size/4,y-size/4,x,y); draw(g,x-size/4,y-size/4,size/2,c+1); draw(g,x+size/4,y-size/4,size/2,c+1); draw(g,x,y-size/2,size/2,c+1); } } }

  13. Sierpinski Triangles public static class GP extends JPanel { public void paintComponent(Graphics g) draw(g,250,100,400,400,100,400,6); } public void draw(Graphics g, int x1, int y1, int x2, int y2, int x3, int y3, int n) { if(n>0){ g.drawLine(x1,y1,x2,y2); g.drawLine(x2,y2,x3,y3); g.drawLine(x3,y3,x1,y1); draw(g,x1,y1,(x1+x2)/2,(y1+y2)/2,(x1+x3)/2,(y1+y3)/2,n-1); draw(g,(x1+x2)/2,(y1+y2)/2,x2,y2,(x2+x3)/2,(y2+y3)/2,n-1); draw(g,(x1+x3)/2,(y1+y3)/2,(x2+x3)/2,(y2+y3)/2,x3,y3,n-1); } }

More Related Content