Download presentation
Presentation is loading. Please wait.
Published byLuis Bowes Modified over 10 years ago
1
Chapter 15 Graphics
2
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, Graphical Coordinate Systems
3
+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 java.awt.Graphics
4
import javax.swing.*; import java.awt.Graphics; public class TestPaintComponent extends JFrame { public TestPaintComponent() { add(new NewPanel()); } public static void 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 { protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawLine(0, 0, 50, 50); g.drawString("Banner", 0, 40); } } A Simple Example
5
import javax.swing.*; import java.awt.Color; import java.awt.Graphics; public class RandGraphicsDemo extends JFrame { public static int w = 500; public static int h = 500; public RandGraphicsDemo() { add(new aPanel()); } public static void 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 { protected void 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)); } } } Random Graphics Example
6
class aPanel extends JPanel { public static int x = 250; public static int y = 250; public static int vx = 3; public static int vy = -2; public static int size = 20; public static int side_border = 15; public static int title_bar = 40; protected void 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 500-size-title_bar) vy = -vy; if(y 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. Animation Techniques
7
import java.awt.*; import javax.swing.*; public class CardShow extends JFrame { public CardShow() { add(new ImagePanel()); } public static void 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 { public void 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); } } } } Graphics Card Deck Demo
8
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); } } public void 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; } } public class 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; } public Card dealCard() { Card the_card; if(topcard<52) { the_card = card[topcard]; topcard += 1; } else the_card = null; return the_card; } } Deck Class
9
import java.awt.*; public class 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"); } } Card Class
10
Example Output
11
import java.awt.*; import javax.swing.*; public class ImageDisplay extends JFrame { public ImageDisplay() { add(new ImagePanel()); } public static void 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); } class ImagePanel extends JPanel { private ImageIcon imageIcon = new ImageIcon("flag.gif"); private Image img = imageIcon.getImage(); @Override protected void paintComponent(Graphics g) { super.paintComponent(g); if(img != null) g.drawImage(img,0,0,getWidth(), getHeight(), this); } } } Image Display Demo
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.