1 / 11

Chapter 15 Graphics

Chapter 15 Graphics. Graphical Coordinate Systems.

Download Presentation

Chapter 15 Graphics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 15 Graphics

  2. Graphical Coordinate Systems To paint, you need to specify where to paint. Each component has its own coordinate system with the origin (0, 0) at the upper-left corner. The x-coordinate increases to the right, and the y-coordinate increases downward. Note that the Java coordinate system differs from the conventional coordinate system,

  3. java.awt.Graphics +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 +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 +fillOval(x: int, y: int, w: int, h: int): void +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. A Simple Example import javax.swing.*;import java.awt.Graphics;publicclass TestPaintComponent extends JFrame {public TestPaintComponent() { add(new NewPanel());}publicstaticvoid main(String[] args) { TestPaintComponent frame = new TestPaintComponent(); frame.setTitle("TestPaintComponent"); frame.setSize(200, 100);frame.setLocationRelativeTo(null); // Center the frame frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }}class NewPanel extends JPanel {protectedvoid paintComponent(Graphics g) {super.paintComponent(g); g.drawLine(0, 0, 50, 50); g.drawString("Banner", 0, 40); }}

  5. Random Graphics Example import javax.swing.*;import java.awt.Color;import java.awt.Graphics;publicclass RandGraphicsDemo extends JFrame {publicstaticint w = 500;publicstaticint h = 500;public RandGraphicsDemo() { add(new aPanel()); }publicstaticvoid main(String[] args) { RandGraphicsDemo frame = new RandGraphicsDemo(); frame.setTitle("Random Graphics"); frame.setSize(w,h); frame.setLocationRelativeTo(null); // Center the frame frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }}class aPanel extends JPanel {protectedvoid paintComponent(Graphics g) { Color col;super.paintComponent(g); setBackground(Color.BLUE);for(int i=0;i<1000;i++) { col = new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256)); g.setColor(col); g.drawRect((int)(Math.random()*450),(int)(Math.random()*450),(int)(Math.random()*50),(int)(Math.random()*50)); }} }

  6. Animation Techniques class aPanel extends JPanel {publicstaticint x = 250;publicstaticint y = 250;publicstaticint vx = 3;publicstaticint vy = -2;publicstaticint size = 20;publicstaticint side_border = 15;publicstaticint title_bar = 40;protectedvoid paintComponent(Graphics g) {super.paintComponent(g); setBackground(Color.BLUE); g.setColor(Color.RED); g.fillOval(x,y,size,size); x = x + vx; y = y + vy;if(x>500-size-side_border) vx = -vx;if(x<0) vx = -vx;if(y>500-size-title_bar) vy = -vy;if(y<0) vy = -vy; delay(5); repaint(); }privatevoid delay(long ms) {long curtime = System.currentTimeMillis();while(curtime + ms > System.currentTimeMillis());}} Through the use of the repaint( ) method and a means of controlling the speed of processing, we can simulate motion of graphical objects.

  7. Graphics Card Deck Demo import java.awt.*;import javax.swing.*;publicclass CardShow extends JFrame {public CardShow() { add(new ImagePanel()); }publicstaticvoid main(String[] args) { JFrame frame = new CardShow(); frame.setTitle("Image Display Demo"); frame.setSize(1000,445); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }class ImagePanel extends JPanel {publicvoid paintComponent(Graphics g) { Card a_card; Deck a_deck = new Deck(); a_deck.shuffle();super.paintComponent(g);for(int i=0;i<52;i++){ a_card = a_deck.dealCard(); g.drawImage(a_card.img,75*(i % 13)+5,100*(i/13) + 5,this); } }} }

  8. publicclass Deck{ String suit = ""; String rank = "";int suitnum, ranknum, value; Card[] card = new Card[52];int topcard; Deck() { topcard = 0;for(int i = 0;i<card.length;i++) { suitnum = i/13; ranknum = i%13 + 1; value = 10;if(ranknum<=9) value = ranknum;switch (suitnum) {case 0: suit = "Club";break;case 1: suit = "Spade";break;case 2: suit = "Heart";break;case 3: suit = "Diamond";break;} if(ranknum==1) rank = "Ace";if(ranknum>1 & ranknum<10) rank = Character.toString((char)(ranknum + 48));if(ranknum == 10) rank = "10";if(ranknum == 11) rank = "Jack";if(ranknum == 12) rank = "Queen";if(ranknum == 13) rank = "King"; card[i] = new Card(suit,rank,value); }}publicvoid shuffle() { Card tmpcard; topcard = 0;for(int i=0;i<1000;i++) {int k,m; k = (int)(Math.random()*52.0); m = (int)(Math.random()*52.0); tmpcard = card[k]; card[k] = card[m]; card[m] = tmpcard;} } publicCard dealCard() { Card the_card;if(topcard<52) { the_card = card[topcard]; topcard += 1; }else the_card = null;return the_card; }} Deck Class

  9. Card Class import java.awt.*;publicclass Card{ String suit; String rank;int value; Image img; Card(String s, String r, int v) { String sname = ""; String rname = ""; suit = s; rank = r; value = v;if(suit=="Club") sname = "C";if(suit=="Spade") sname = "S";if(suit=="Heart") sname = "H";if(suit=="Diamond") sname = "D"; rname = r;if(rank == "Ace") rname = "A";if(rank == "10") rname = "T";if(rank == "Jack") rname = "J";if(rank == "Queen") rname = "Q";if(rank == "King") rname = "K"; img = Toolkit.getDefaultToolkit().getImage("cards/" + sname + rname + ".jpg");} }

  10. Example Output

  11. Image Display Demo import java.awt.*;import javax.swing.*;publicclass ImageDisplay extends JFrame{public ImageDisplay() { add(new ImagePanel()); }publicstaticvoid main(String[] args) { JFrame frame = new ImageDisplay(); frame.setTitle("Image Display Demo"); frame.setSize(400,300); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true);}classImagePanel extends JPanel {private ImageIcon imageIcon = new ImageIcon("flag.gif");private Image img = imageIcon.getImage(); @Overrideprotectedvoid paintComponent(Graphics g) {super.paintComponent(g);if(img != null) g.drawImage(img,0,0,getWidth(), getHeight(), this); }} }

More Related