CS 245 Lecture 7 – Custom Drawing
Agenda
- Design This
- inheriting from JPanel to do custom drawing
- Sketcher
- Program This
- recursion
Design This
You’re coding up a game. There are good characters. There are bad ones. How do you model them? You wish to not repeat code.
Program This
Write a method that counts how many times a given character appears in a String. Try not to use a loop.
Recipe for Custom Drawing
Extend JPanel and override paintComponent:
class CustomCanvas extends JPanel {
@Override
public void paintComponent(Graphics g) {
// insert drawing code here
}
}
Code
Person.java
package lecture0924;
public class Person {
protected int hitPoints;
}
GoodPerson.java
package lecture0924;
public class GoodPerson extends Person {
public GoodPerson() {
System.out.println(hitPoints);
}
}
Sketcher.java
package lecture0924;
import java.awt.BorderLayout;
import javax.swing.JFrame;
public class Sketcher {
public static void main(String[] args) {
JFrame frame = new JFrame("Sketcher");
frame.add(new SketcherWidget(), BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1200, 700);
frame.setVisible(true);
}
}
SketcherWidget.java
package lecture0924;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JPanel;
public class SketcherWidget extends JPanel implements MouseListener, MouseMotionListener {
private ArrayList<Point> points;
public SketcherWidget() {
points = new ArrayList<Point>();
Random generator = new Random();
for (int i = 0; i < 90 + 1; ++i) {
points.add(new Point(generator.nextInt(1200),
generator.nextInt(700)));
}
addMouseListener(this);
addMouseMotionListener(this);
}
@Override
public void paintComponent(Graphics g) {
// Draw a pink line.
g.setColor(Color.PINK);
g.drawLine(0, 0, 1200, 700);
// Draw some circles, recursively.
drawCircles(g, 500);
// Draw points.
g.setColor(Color.MAGENTA);
for (Point point : points) {
g.fillOval(point.x, point.y, 5, 5);
}
}
private void drawCircles(Graphics g, int radius) {
if (radius <= 0) {
// base case
// don't do anything, probably omit this case from the code
} else {
// general case: draw circle of given radius and inner ones
g.setColor(Color.RED);
g.fillOval(100, 150, radius, radius);
g.setColor(Color.WHITE);
g.fillOval(100, 150, radius - 5, radius - 5);
drawCircles(g, radius - 10);
}
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
points.add(e.getPoint());
repaint();
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseDragged(MouseEvent e) {
points.add(e.getPoint());
repaint();
}
@Override
public void mouseMoved(MouseEvent e) {
}
}
StringUtilities.java
package lecture0924;
public class StringUtilities {
public static int nOccurrences(String s) {
if (s.isEmpty()) {
return 0;
} else if (s.charAt(0) == 'F') {
return 1 + nOccurrences(s.substring(1));
} else {
return 0 + nOccurrences(s.substring(1));
}
}
public static void main(String[] args) {
System.out.println(nOccurrences("FooF"));
System.out.println(nOccurrences("FFFFF"));
System.out.println(nOccurrences(".........."));
}
}
Haiku
I miss being young
I drew things without machines
I thought I could then
I drew things without machines
I thought I could then