14
CS12420 - Lecture 03 Drawing & Displaying Images Lynda Thomas [email protected]

CS12420 - Lecture 03 Drawing Displaying Images Lynda Thomas

Embed Size (px)

DESCRIPTION

paintComponent method public void paintComponent(Graphics g) { super.paintComponent(g); g.drawLine(10,10,100,100); //add own commands here } Just like with actionPerformed – this parameter is supplied If you need to call it (see later) you call repaint()

Citation preview

Page 1: CS12420 - Lecture 03 Drawing  Displaying Images Lynda Thomas

CS12420 - Lecture 03Drawing & Displaying Images

Lynda Thomas

[email protected]

Page 2: CS12420 - Lecture 03 Drawing  Displaying Images Lynda Thomas

Drawing

• Use a panel as a canvasJPanel.paintComponent(Graphics g)

Override (but first call super) to add own commands like

g.drawLine(10,10,100,100);

• Never called directly, instead call repaint()

Page 3: CS12420 - Lecture 03 Drawing  Displaying Images Lynda Thomas

paintComponent method

public void paintComponent(Graphics g) {super.paintComponent(g);

g.drawLine(10,10,100,100);

//add own commands here}

Just like with actionPerformed – this parameter is supplied

If you need to call it (see later) you call repaint()

Page 4: CS12420 - Lecture 03 Drawing  Displaying Images Lynda Thomas

Other Graphics methods

drawLine(int xStart,int yStart,int xEnd,int yEnd)drawRect(int xLeft,int yTop,int width,int height)drawOval(int xLeft,int yTop,int width,int height)drawString(String text,int xLeft,int yBottom)fillRect(int xLeft,int yTop,int width,int height)fillOval(int xLeft,int yTop,int width,int height)setColor(Color col)drawPolygon(int[] xp,int[] yp,int n)

Page 5: CS12420 - Lecture 03 Drawing  Displaying Images Lynda Thomas

public void paintComponent(Graphics g) {super.paintComponent(g);

g.setColor(Color.black);g.drawLine(10,10,100,100);g.setColor(Color.red);g.drawLine(10,100,100,10);g.setColor(Color.green);g.drawOval(120,60,70,40);g.setColor(Color.black);g.fillOval(238,160,30,30);g.setColor(Color.cyan);g.fillOval(10,120,100,60);g.setColor(this.getBackground());g.fillOval(50,140,100,60);g.setColor(Color.blue);g.drawString("Swing is £$~%",100,200);

}

This method is called whenever you ask it to (with repaint()) or

whenever it ‘needs’For instance when the application starts or when it is uncovered or ..

Page 6: CS12420 - Lecture 03 Drawing  Displaying Images Lynda Thomas

Look in drawing folder at:SimpleGraphics classes

Page 7: CS12420 - Lecture 03 Drawing  Displaying Images Lynda Thomas

Screen Parameters

int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;

int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;

Page 8: CS12420 - Lecture 03 Drawing  Displaying Images Lynda Thomas

Panel Parameters

int panelHeight = this.getHeight();

int panelWidth = this.getWidth();

Page 9: CS12420 - Lecture 03 Drawing  Displaying Images Lynda Thomas

Images• This is barebones – more in second year - there is now a special

package called javax.imageio and other approaches like JavaFX• Swing also has ImageIcon to decorate buttons etc.

• See images-code/ImagePanel.java for (over) simple loading and displaying of a single image

• ImageFormats: Graphics Interchange Format (GIF), Joint Photographic Experts Group (JPG), Portable Network Graphics (PNG)

Page 10: CS12420 - Lecture 03 Drawing  Displaying Images Lynda Thomas
Page 11: CS12420 - Lecture 03 Drawing  Displaying Images Lynda Thomas

Multiple Images and Image manipulation

see images-code/LotsOfImages classes

Page 12: CS12420 - Lecture 03 Drawing  Displaying Images Lynda Thomas

These examples use a MediaTracker

• When you first load an Image all that happens is that Java notes where it is. Does not get image, so hence doesn’t know what size!!!

• It isn’t until you do something with it that anything really happens.• MediaTracker does the upload and waits for an ID – sort of fakes

the machine into thinking image is needed NOW.• You can also do all kinds of clever things with loading in groups, in

order etc.• Look at LyndaFrameDriver and see what happens without one (the

images are there but all the clever sizing and positioning fails)

Page 13: CS12420 - Lecture 03 Drawing  Displaying Images Lynda Thomas

In This Lecture

• Drawing• Images

Page 14: CS12420 - Lecture 03 Drawing  Displaying Images Lynda Thomas

In The Next Lecture

• Mouse interaction• More on Listeners