Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Painting and Events in Java Packages Class framework Windows, Painting, Events Java Platform (JDK) 1 • The Java Platform includes extensive crossplatform libraries to do everything from threading to database queries to user-interfaces. Java Class Library 2 • Classes are grouped into packages (i.e. namespaces) to avoid name collisions. • To assign your source code to a package, use the package keyword at the top of source files. • Typically, package = subdirectory – e.g. “graphics” package is in subdirectory of the same name – alt. it can be included in a JAR file. • Use the import keyword to include a class from a different package in your code. – This is how you include bundled Java libraries. 3 Common Classes/Packages Java Class Hierarchy 4 Package Classes (Examples) Description java.awt Color, Graphics, Graphics2D, event. Contains all of the classes for creating user interfaces and for painting graphics and images. javax.swing JFrame, JButton, JList, JToolbar Provides a set of "lightweight" (allJava language) components that works the same on all platforms. java.io File, FileReader, FileWriter, InputStream Provides for system input and output through data streams, serialization and the file system. java.lang Boolean, Integer, String, System, Thread, Math Provides classes that are fundamental to the design of the Java programming language. java.util ArrayList, HashMap, Observable Contains the collections framework, legacy collection classes, event model,… Java 8 API Specification: https://docs.oracle.com/javase/8/docs/api/overview-summary.html • Implicit class hierarchy – All classes in Java are derived from the Object class in java.lang defines and implements common class behavior • e.g. clone(), toString(), finalize() methods – Classes you write inherit this basic behavior. 5 Getting Started Creating a window Painting Events 6 Open a Window package guis_1.v1; import javax.swing.*; public class GUIs1v1 { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() {JFrame is a container for components. Paint on components (coming). JFrame frame = new JFrame("Window Title"); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); frame.setSize(400, 500); frame.setVisible(true); } } } }); Open a Window 7 package guis_1.v1; import javax.swing.*; public class GUIs1v1 { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { JFrame frame = new JFrame("Window Title"); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); frame.setSize(400, 500); frame.setVisible(true); } } } }); invokeLater ensures that the program can’t start accepting events from the user before it’s ready to start processing them. Swing Component Hierarchy 8 • java.awt.Window is the base for all containers. • javax.swing.Jcomponent is the root for all widgets. 9 Adding a Component package guis_1.v2; import javax.swing.*; public class GUIs1v2 { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { JFrame frame = new JFrame("Window Title"); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); frame.setSize(400, 500); } } } }); frame.add(new ColouredX()); frame.setVisible(true); class ColouredX extends JComponent { } Painting the ColouredX 10 class ColouredX extends JComponent { private BasicStroke stroke = new BasicStroke(30.0f); public void paintComponent(Graphics g) { Graphics vs. Graphics2D g2 = (Graphics2D) g; Graphics2D int w = this.getWidth(); int h = this.getHeight(); paintComponent is called automatically. You never g2.setStroke(this.stroke); call it yourself.* g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(Color.RED); g2.drawLine(0, 0, w, h); g2.setColor(Color.BLUE); g2.drawLine(0, h, w, 0); } } *Except, maybe, for pedagogical reasons in part 1 of assignment 1. 11 Responding to Events 12 Responding to Events (Painful) class ColouredX extends JComponent { private BasicStroke stroke = new BasicStroke(30.0f); private LinkedList<Point> clicks = List of clicked points. new LinkedList<Point>(); public ColouredX() { this.addMouseListener( new ColouredXMouseListener(this)); } public void addClick(Point p) { this.clicks.add(p); this.repaint(); } Add a clicked point to the list. repaint()… public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; this.paintX(g2); g2.setColor(Color.BLACK); for (Point p : this.clicks) { g2.drawString("click", p.x, p.y); } Paint the clicked points. } public void paintX(Graphics2D g) { ... } } Responding to Events (Painful) 13 class ColouredXMouseListener implements MouseListener { private ColouredX cx; public ColouredXMouseListener(ColouredX cx) { this.cx = cx; } @Override public void mouseClicked(MouseEvent e) { this.cx.addClick(e.getPoint()); } @Override public void mousePressed(MouseEvent e) { } @Override public void mouseReleased(MouseEvent e) { } } @Override Must implement public void mouseEntered(MouseEvent e)all{five } methods named in the MouseListener @Override interface in spite of only needing one. public void mouseExited(MouseEvent e) { } Responding to Events (Less Painful) 14 class ColouredXMouseListener extends MouseAdapter { private ColouredX cx; public ColouredXMouseListener(ColouredX cx) { this.cx = cx; } @Override public void mouseClicked(MouseEvent e) { this.cx.addClick(e.getPoint()); } } ColouredX class remains the same. 15 Every listener interface with more than one method has a corresponding adapter – an abstract class with an empty method for each method in the interface. Just override the relevant methods. Responding to Events (Better) class ColouredX extends JComponent { ... private LinkedList<Point> clicks = public ColouredX() { this.addMouseListener( new ColouredXMouseListener()); } new LinkedList<Point>(); An inner class has access to private members in the enclosing class. class ColouredXMouseListener extends MouseAdapter { @Override public void mouseClicked(MouseEvent e) { ColouredX.this.clicks.add(e.getPoint()); repaint(); } Changes to ColouredXMouseListener: } 1. Ref to ColouredX is gone. 2. Constructor is gone; no need to pass argument. public void paintComponent(Graphics g) { ... Changes to ColouredX: } 1. addClick is gone; content moved to mouseClicked } Responding to Events (Ideomatic) 16 class ColouredX extends JComponent { ... private LinkedList<Point> clicks = new LinkedList<Point>(); public ColouredX() { this.addMouseListener( new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { clicks.add(e.getPoint()); repaint(); } } Anonymous inner class: Java’s ); equivalent to passing a function. } public void paintComponent(Graphics g) { ... } } 17 Animation in Java 18 Animation using a Timer class ColouredX ... private Point private final private Timer extends JComponent { ballPos = new Point(100, 0); int FPS = 40; FPS = Frames Per Second timer; public ColouredX() { this.addMouseListener(...); javax.swing.Timer this.timer = new Timer(1000/FPS, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { ballPos.y += 2; repaint(); } }); this.timer.start(); } 19 } public void paintComponent(Graphics g) { Paint the ball at ... its new location. g2.setColor(Color.ORANGE); g2.fillOval(this.ballPos.x, this.ballPos.y, 30, 30); }