Programming design.

Uploaded on:
Category: Medical / Health
Programming design Need a window – javax.swing.JFrame A few crucial strides to utilize (fundamental "pipes"): Set the size – width and stature in pixels Set a title (discretionary), and a nearby operation Make it noticeable See (p. 59)
Slide 1

Programming representation Need a window – javax.swing.JFrame Several crucial strides to utilize (vital “plumbing”): Set the size – width and tallness in pixels Set a title (discretionary), and a nearby operation Make it noticeable See (p. 59) Add javax.swing.JComponent s to window Draw shapes, hues, … on these parts That’s the long and short of it! With the exception of the careful work, obviously

Slide 2

Drawing rectangles for instance Define class that expands JComponent Or a subclass like JPanel for extra components Implement paintComponent system Use Graphics item went to this technique Actually a Graphics2D object since Java 1.2 Then let that question draw Rectangle articles See (p. 61) Add the segment to an edge for review e.g.,

Slide 3

java.awt.Graphics2D Is a subclass of java.awt.Graphics So thrown is permitted; and Graphics systems acquired If don’t cast, must utilize primitive drawing techniques: e.g., drawRect(int, int, int, int) , fillOval(int, int, int, int) , … i.e., not protest arranged – so bunches of work to utilize/reuse But Graphics2D can do a great deal more stuff e.g., draw(java.awt.Shape) draws any Shape , including Rectangle , Ellipse2D , Polygon , … fill(Shape) draws and fills Shape with current shading

Slide 4

Drawing more perplexing shapes Text illustration (p. 114-116) – Acts like a Car that can draw itself Car constructor sets x and y areas Includes draw(Graphics2D g2) system Lets Graphics2D item draw lines, circles, rectangles A class like just uses it: Car myCar = new Car(x, y); myCar.draw(g2);/passes reference to illustrations protest Still need a perspective window, as

Slide 5

C o l o r Current shading applies to content, lines, and fills: g2.setColor(Color.RED); g2.draw( … );/attracts … red g2.setColor(Color.BLUE); g2.fill(…);/fills … with blue Custom hues accessible: Can set by buoy values in extent 0.0F to 1.0F : Color gb = new Color(0.0F, 0.7F, 1.0F); g2.setColor(gb); Or by int values in reach 0 to 255 : Color bg = new Color(0, 255, 175);

Slide 6

Rendering content with Graphics2D Actually important to “draw” the content at a predetermined area on the Graphics article g.drawString(aString, x, y) – utilizes current rendering connection (e.g., shading), and current content traits (e.g., text style) Font: a face name, a style , and a point size Font f = new Font(“Serif”, Font.BOLD, 24); g2.setFont(f);/sets textual style for g2

Slide 7

Applets – an other methodology An approach to run a project – yet not an application No fundamental technique essential Need a subclass of Applet (or JApplet) So: class __ amplifies Applet (or augments JApplet ) Most web programs know how to make another applet, and how to utilize certain Applet routines So, applets must be installed in a html page And, to be helpful, they must incorporate no less than one of the strategies the program conjures (e.g., paint )

Slide 8

“Running” an Applet The applet is begun by the web program when the website page( (html record) is gone to The html document (remains for h yper t ext m arkup l anguage) — must have an applet tag in it: <applet code=AppletClassName. class width=### height=###> </applet> <!- - needs an end tag as well - > The program meets expectations in a sure request: Creates another applet object – incorporates a window, a Graphics item, bunches of class Applet techniques Invokes (1) init – once, (2) begin – first & return visits, (3) paint – first & each need to paint (additionally stop , devastate )

Slide 9

Implementing a “simple” applet import javax.swing.JApplet;/obligatory Also ordinarily Graphics and Graphics2D and others Declare a class that broadens JApplet: open class RectangleApplet expands JApplet Implement paint strategy (at any rate) Same strategies as paintComponent for parts Create a html document to stack the applet in a web program or the appletviewer (gave JDK) See (p. 63) and related html documents (p. 64)

Slide 10

Images are not drawings You don’t draw them, you demonstrat to them But Graphics technique is called drawImage in any case Image is a conceptual class Generally, make occurrence by stacking from record or URL Can likewise make/alter by classes in java.awt.image Applets know how to get pictures ( ImageApplet ) Applications use Toolkit to get ( demo ) Toolkit.getDefaultToolkit().getImage("javacup.gif");

Slide 11

Events Signals from the outside Usually client starts: Mouse snap, catch push, menu-select, enter content, … Not generally: Each occasion has a sort – a class in java.awt.event So every occasion is an item Created continually (by Java window chief)

Slide 12

Event sources Technically: can be any outer sign Including from an alternate project or framework e.g., a “client” applet reaching a web “server” In a GUI, center is on client signals Java Component s produce the occasions Each part produces diverse occasion sorts, e.g.: Panels (inc. Applets)  MouseEvent s Textfields and catches  ActionEvent s Windows  WindowEvent s Keyboard  KeyEvent s Components illuminate a rundown of Listener s about occasions

Slide 13

Listeners Objects that parts tell about occasions Must have strategies a segment can conjure i.e., actualizes one of the Listener interfaces Must be add ed to a component’s audience list Different audience members apply to distinctive messages ActionListener – actionPerformed e.g., ClickListener (with ) MouseListener – mouseClicked , mouseEntered , … e.g., MouseSpy demo (from 1 st version of course reading) Note: additionally can broaden Adapter class rather than execute audience members specifically – spares busywork ☺

Slide 14

Inner class audience members Can get to private occurrence variables of external class Stores certain reference to external class question So can frequently “handle” occasions all the more effectively e.g., Notice they can get to last nearby variables excessively Another case: Must be last so no equivocalness about worth For instance, no open door for variable to go out of degree while item exists

Slide 15

Laying out GUI segments Depends on design director Defaults: JFrame: BorderLayout JPanel: FlowLayout Others: GridLayout GridBagLayout Can set new, and even make custom

Slide 16

Choosing a format chief e.g., a matrix format for number cruncher catches: board. setLayout ( new GridLayout(4,3) ); panel.add(button7); panel.add(button8); panel.add(button9); panel.add(button4); ... e.g.,

Slide 17

Text parts JLabel – not for client data, simply show JTextField – for one line of content new JTextField(), or (int sections) getText(), setText(String), setFont(Font), setEditable(boolean), … (basically acquired) ActionEvent on <enter> JTextArea – for different lines of content new JTextArea(), or (int lines, int segments) Same routines acquired from JTextComponent Generates no occasions so for the most part use with a catch

Slide 18

Choices Choice items produce ActionEvents Handlers for JCheckBox and JRadioButton : use boolean system isSelected() Note: put radio catches in a ButtonGroup – so only one can be chosen at once For same reason – ought to outwardly aggregate them in a board with a fringe, as EtchedBorder For JComboBox : use getSelectedItem() Note: returns Object – more often than not cast to String e.g., (see, pp. 794-798)

Slide 19

Menus Steps to actualize swing menus: 1. Add a menu bar to a JFrame JMenuBar bar = new JMenuBar(); setJMenuBar(bar); 2. Add menus to the menu bar JMenu fileMenu = new JMenu(“File”); bar.add(fileMenu); 3. Add menu things to the menus, & audience members to things JMenuItem openItem = new JMenuItem(“Open”); fileMenu.add(openItem); openItem.addActionListener(listener); e.g., (see, pp. 803-7)

Slide 20

Sliders, and all the more swing ing Note - great content segment 18.4, pp. 808-814: ought to peruse while skimming API on web About how to “discover” swing elements/utilization Focuses on JSlider – creates ChangeEvent s so addChangeListener(listener) , where audience actualizes stateChanged strategy Requires javax.swing.event for “change” occasions e.g., (see, pp. 812-814) Explore swing as required, or even only for the sake of entertainment Buy a book, or take a gander at API classes beginning with “ J ” Or simply run the SwingSet demo from the JDK

Slide 21

Maybe all the more Maybe less

Slide 22

Multithreading For concurrent procedures e.g., a website page stacks a few pictures freely e.g., separate diversion characters move voluntarily JVM plans “time slices” for every string Idea is to appear like all event on the double Use or broaden class Thread or execute interface Runnable Implement run() – yet summon begin() to utilize it Must handle or toss InterruptedException too e.g., GreetingThread

Slide 23

Synchronizing strings Necessary to “lock” shared articles Other strings must hold up until lock is uprooted Idea is to keep objects in legitimate state e.g., not care for this BankAccount strings test An answer for this situation is to synchr

View more...