Download presentation
Presentation is loading. Please wait.
Published byJeffrey Sims Modified over 9 years ago
1
CS12420 - Lecture 03 Drawing & Displaying Images Lynda Thomas ltt@aber.ac.uk
2
Drawing Use a panel as a canvas JPanel.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()
3
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()
4
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)
5
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..
6
Look in drawing folder at: SimpleGraphics classes
7
Screen Parameters int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height; int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
8
Panel Parameters int panelHeight = this.getHeight(); int panelWidth = this.getWidth();
9
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)
11
Multiple Images and Image manipulation see images-code/LotsOfImages classes
12
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)
13
In This Lecture Drawing Images
14
In The Next Lecture Mouse interaction More on Listeners
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.