Lecture 13: Dynamic Jlist and Drawing on JPanel - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Lecture 13: Dynamic Jlist and Drawing on JPanel

Description:

Clear the list using. model.clear() Lecture 13: Dynamic Jlist and Drawing ... g.drawString('Double click clears screen.',10,50); class Down extends MouseAdapter ... – PowerPoint PPT presentation

Number of Views:221
Avg rating:3.0/5.0
Slides: 17
Provided by: csUi
Category:

less

Transcript and Presenter's Notes

Title: Lecture 13: Dynamic Jlist and Drawing on JPanel


1
Lecture 13 Dynamic Jlist and Drawing on JPanel
  • Dynamic JLists
  • To have a JList change during the execution of a
    program, we must deal with its underlying model,
    which contains the actual data.

2
Lecture 13 Dynamic Jlist and Drawing on JPanel
  • Example
  • Create a model for a list
  • DefaultListModel model new DefaultListModel()
  • Create the list
  • JList list new JList(model)
  • Add items to the list by adding them to the model
  • model.addElement("one")
  • model.addElement("two")
  • model.add(1, "oneandhalf")

3
Lecture 13 Dynamic Jlist and Drawing on JPanel
  • Remove items from the list by removing them from
    the model
  • model.remove(0)
  • model.removeElement("two")
  • Size of list returned by
  • model.getSize()
  • Clear the list using
  • model.clear()

4
Lecture 13 Dynamic Jlist and Drawing on JPanel
  • Drawing on a Panel
  • An application that allows the user to draw on a
    panel.
  • Application creates a panel on a frame for
    drawing.
  • Uses Point class to save points.

5
Lecture 13 Dynamic Jlist and Drawing on JPanel
  • Variations of mouseDown
  • Click
  • Shift-click
  • Double-click
  • Uses of mouseDrag
  • Draw a line (draw)
  • Draw a filled oval (erase)

6
Lecture 13 Dynamic Jlist and Drawing on JPanel
  • Point Class

7
public class java.awt.Point extends
java.lang.Object // Fields public int
x public int y // Constructors public
Point(int x, int y) // Methods public boolean
equals(Object obj) public void move(int x, int
y) public String toString() public void
translate(int dx, int dy)
8
Lecture 13 Dynamic Jlist and Drawing on JPanel
  • Note
  • The drawing color can be set by two different
    methods
  • setColor(Color c) in the class Graphics
  • setForeground(Color c) in the class Component
  • Doodle
  • Add a DrawPanel, an inner class, to a frame.
  • Capture mouse events to allow drawing on the
    panel.

9
import java.awt. import java.awt.event. import
javax.swing. public class Doodle extends
JFrame Point lineStart new Point(0,0)
int circleSize 16 JPanel panel
Doodle() setTitle("Doodle")
addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent e)
System.exit(0) )
10
panel new DrawPanel()
panel.setBackground(Color.cyan)
panel.addMouseListener(new Down())
panel.addMouseMotionListener(new Drag())
getContentPane().add(panel) class
DrawPanel extends JPanel public void
paintComponent(Graphics g)
super.paintComponent(g)
g.setColor(Color.blue)
g.drawString("Left button draws.",10,20)
g.drawString("Right button or shift-left
button erases.",10,35)
g.drawString("Double click clears
screen.",10,50)
11
class Down extends MouseAdapter
public void mousePressed(MouseEvent e)
int x e.getX(), y e.getY()
if (e.isMetaDown() e.isShiftDown())
setForeground(panel.getBackground())
else setForeground(Color.blue)
if (e.getClickCount() 2) // double click
panel.repaint() else
lineStart.move(x,y)
12
class Drag extends MouseMotionAdapter
public void mouseDragged(MouseEvent e)
int x e.getX(), y e.getY()
Graphics g getGraphics() if
(e.isMetaDown() e.isShiftDown())
g.fillOval(x-circleSize/2, y-circleSize/2,

circleSize, circleSize) else
g.drawLine(lineStart.x, lineStart.y, x, y)
lineStart.move(x,y)
13
public static void main(String args)
JFrame jf new Doodle()
jf.setSize(600,500) jf.setVisible(true)

14
(No Transcript)
15
Lecture 13 JTextArea
  • JTextArea
  • Constructors
  • JTextArea(int rows, int cols)
  • JTextArea(String text, int rows, int cols)

16
Lecture 13 JTextArea
  • Usage
  • JTextArea ta new JTextArea("Message", 5, 20)
  • ta.setEditable(false) // default is true
  • ta.select(3,7)
  • ta.selectAll()
  • String s ta.getSelectedText()
  • ta.setText("Replace all text")
Write a Comment
User Comments (0)
About PowerShow.com