SlideShare une entreprise Scribd logo
1  sur  17
Bar Graph Design
Left-side/Right-side

mechanical
processing

creative,
abstract
reasoning
Why Is He Doing This?
The Problem:


Two frames (Views) and a data structure (Model).



Observer Pattern:
Observer Pattern Times Four:


JTextField: ActionListener receives event after you hit <return>.



The ActionListener updates the Data (Model) object.

JTextField
ActionListener
addActionListener
actionPerformed

anonymous
object
Observer Pattern Times Four:


Data: Is observed by the BarGraph (ChangeListener) for any
changes to the model.

Data
ChangeListener

attach
stateChanged

BarGraph
Observer Pattern Times Four:




BarGraph: MouseListener receives event when you click on the
bar graph.
mousePressed() moves the bar and updates Data object.
BarGraph
MouseListener
addMouseListener

mousePressed

Mouse
Adapter
Observer Pattern Times Four:


Data: Is observed by TextFrame (ChangeListener) for any
changes to data.

Data
ChangeListener

attach
stateChanged

TextFrame
Class Diagram:
Sequence Diagram


Enter new number
Sequence Diagram:


Click on BarGraph:
Data.java

public class Data {
public Data(ArrayList<Double> d)
{
data = d;
listeners = new ArrayList<ChangeListener>();
}
public ArrayList<Double> getData()
{
return (ArrayList<Double>) (data.clone());
}
public void attach(ChangeListener c)
{
listeners.add(c);
}
}
Data.java

public class Data {
public void update(int loc, double val)
{
if (val > MAX)
val = MAX;
if (val < 0)
val = 0;
data.set(loc, new Double(val));
// for each listener, let the listener know a change has occurred
for (ChangeListener l : listeners)
{
l.stateChanged(new ChangeEvent(this));
}
}
public Double get(int i) {
return data.get(i);
}
private ArrayList<Double> data;
private ArrayList<ChangeListener> listeners;
public static final double MAX = 100.0;
}
TextFrame:
public class TextFrame extends JFrame implements ChangeListener {
public TextFrame (Data md) {
d = md;
ActionListener l = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JTextField c = (JTextField) e.getSource();
int ndx = findBox(c);
if (ndx >= 0)
d.update(ndx,Double.parseDouble(c.getText().trim()));
}
};
JPanel tPanel = new JPanel();
tPanel.setLayout(new BoxLayout(tPanel,BoxLayout.PAGE_AXIS));

...
}

}

tf = new JTextField[5];
for (int i = 0; i < 5; i++) {
tf[i] = new JTextField(10);
tf[i].addActionListener(l);
tPanel.add(tf[i]);
}
add(tPanel, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
TextFrame:
@Override
public void stateChanged(ChangeEvent e) {
// how I notify the text frame if the bar graph changes
for (int i = 0; i < 5; i++){
double d1 = d.get(i).doubleValue();
String s = tf[i].getText().trim();
double d2;
try {
d2 = Double.parseDouble(s);
} catch(NumberFormatException nfe ) {
d2 = 0.0;
}
if (Math.abs(d1 - d2) > 0.01)
tf[i].setText(new Double(d1).toString());
}
}
public int findBox(JTextField t) {
int i = 0;
while (i < 5) {
if (t == tf[i])
return i;
i++;
}
return -1;
}

}

private Data d;
private JtextField[ ] tf;
BarGraph:
public class BarGraph extends JFrame implements ChangeListener {
public BarGraph(Data md) {
d = md;
setLocation(0,300);
Icon barIcon = new Icon() {
public int getIconWidth() { return WIDTH; }
public int getIconHeight() { return HEIGHT; }

}

public void paintIcon(Component c, Graphics g , int x, int y) {
Graphics2D g2 = (Graphics2D) g;
Color cl = g2.getColor();
for (int i = 0; i < 5; i++) {
Rectangle2D.Double rectangle =
new Rectangle2D.Double(0,
(HEIGHT/6)*i,
WIDTH*(d.get(i).doubleValue()/Data.MAX),
(HEIGHT/6));
g2.setPaint(Color.cyan);
g2.fill(rectangle);
g2.setColor(Color.black);
g2.draw(rectangle);
}
Rectangle2D.Double rectangle =
new Rectangle2D.Double(0,375,WIDTH,(HEIGHT/6));
g2.setPaint(cl); g2.fill(rectangle);g2.setColor(cl);g2.draw(rectangle);
}
}; // end of barIcon panel
...
BarGraph:
public class BarGraph extends JFrame implements ChangeListener {
public BarGraph(Data md) {
...
add(new JLabel(barIcon));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MouseListener m = new MouseAdapter() {
public void mousePressed(MouseEvent e){
Point2D p = e.getPoint();
double dy = p.getY();
int barToMove = (int)(dy / BARHEIGHT);
if (barToMove < 0 || barToMove > 4)
return;
double dx = p.getX();
double newV = (dx/WIDTH)* Data.MAX;
d.update(barToMove, newV);

}
};
addMouseListener(m);
pack();
setVisible(true);
} // end constructor
} // end class

Contenu connexe

Tendances

Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayimtiazalijoono
 
The Ring programming language version 1.5.3 book - Part 188 of 194
The Ring programming language version 1.5.3 book - Part 188 of 194The Ring programming language version 1.5.3 book - Part 188 of 194
The Ring programming language version 1.5.3 book - Part 188 of 194Mahmoud Samir Fayed
 
OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1vikram mahendra
 
The Ring programming language version 1.5.4 book - Part 179 of 185
The Ring programming language version 1.5.4 book - Part 179 of 185The Ring programming language version 1.5.4 book - Part 179 of 185
The Ring programming language version 1.5.4 book - Part 179 of 185Mahmoud Samir Fayed
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional arrayRajendran
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to ArraysTareq Hasan
 
INTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHONINTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHONvikram mahendra
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : ArraysGagan Deep
 
Multidimensional array in C
Multidimensional array in CMultidimensional array in C
Multidimensional array in CSmit Parikh
 

Tendances (20)

DATA TYPE IN PYTHON
DATA TYPE IN PYTHONDATA TYPE IN PYTHON
DATA TYPE IN PYTHON
 
2D Array
2D Array 2D Array
2D Array
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
 
The Ring programming language version 1.5.3 book - Part 188 of 194
The Ring programming language version 1.5.3 book - Part 188 of 194The Ring programming language version 1.5.3 book - Part 188 of 194
The Ring programming language version 1.5.3 book - Part 188 of 194
 
Queue
QueueQueue
Queue
 
OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1
 
The Ring programming language version 1.5.4 book - Part 179 of 185
The Ring programming language version 1.5.4 book - Part 179 of 185The Ring programming language version 1.5.4 book - Part 179 of 185
The Ring programming language version 1.5.4 book - Part 179 of 185
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 
Vector3
Vector3Vector3
Vector3
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
INTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHONINTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHON
 
C programming , array 2020
C programming , array 2020C programming , array 2020
C programming , array 2020
 
6.array
6.array6.array
6.array
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
 
Multidimensional array in C
Multidimensional array in CMultidimensional array in C
Multidimensional array in C
 
C programming slide c05
C programming slide c05C programming slide c05
C programming slide c05
 
Arrays
ArraysArrays
Arrays
 
Arrays in C
Arrays in CArrays in C
Arrays in C
 
Arrays
ArraysArrays
Arrays
 

Similaire à Bar graph

Oleksandr Tolstykh
Oleksandr TolstykhOleksandr Tolstykh
Oleksandr TolstykhCodeFest
 
Reactive clean architecture
Reactive clean architectureReactive clean architecture
Reactive clean architectureViktor Nyblom
 
Opensource gis development - part 3
Opensource gis development - part 3Opensource gis development - part 3
Opensource gis development - part 3Andrea Antonello
 
Keep getting a null pointer exception for some odd reasonim creati.pdf
Keep getting a null pointer exception for some odd reasonim creati.pdfKeep getting a null pointer exception for some odd reasonim creati.pdf
Keep getting a null pointer exception for some odd reasonim creati.pdfAroraRajinder1
 
How to become an Android dev starting from iOS (and vice versa)
How to become an Android dev starting from iOS (and vice versa)How to become an Android dev starting from iOS (and vice versa)
How to become an Android dev starting from iOS (and vice versa)Giuseppe Filograno
 
Chat application in java using swing and socket programming.
Chat application in java using swing and socket programming.Chat application in java using swing and socket programming.
Chat application in java using swing and socket programming.Kuldeep Jain
 
Use the following data set that compares age to average years lef.docx
Use the following data set that compares age to average years lef.docxUse the following data set that compares age to average years lef.docx
Use the following data set that compares age to average years lef.docxdickonsondorris
 
Flink Forward San Francisco 2018: Seth Wiesman - "Testing Stateful Streaming ...
Flink Forward San Francisco 2018: Seth Wiesman - "Testing Stateful Streaming ...Flink Forward San Francisco 2018: Seth Wiesman - "Testing Stateful Streaming ...
Flink Forward San Francisco 2018: Seth Wiesman - "Testing Stateful Streaming ...Flink Forward
 
Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#Mark Needham
 
Mixing Functional and Object Oriented Approaches to Programming in C#
Mixing Functional and Object Oriented Approaches to Programming in C#Mixing Functional and Object Oriented Approaches to Programming in C#
Mixing Functional and Object Oriented Approaches to Programming in C#Skills Matter
 
Creating a Facebook Clone - Part XXIX - Transcript.pdf
Creating a Facebook Clone - Part XXIX - Transcript.pdfCreating a Facebook Clone - Part XXIX - Transcript.pdf
Creating a Facebook Clone - Part XXIX - Transcript.pdfShaiAlmog1
 
Working effectively with legacy code
Working effectively with legacy codeWorking effectively with legacy code
Working effectively with legacy codeShriKant Vashishtha
 
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdfCreat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdfaromanets
 
Wicket KT part 2
Wicket KT part 2Wicket KT part 2
Wicket KT part 2stuq
 
React new features and intro to Hooks
React new features and intro to HooksReact new features and intro to Hooks
React new features and intro to HooksSoluto
 
JBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java ProgrammersJBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java ProgrammersTikal Knowledge
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Mario Fusco
 
Functional GUIs with F#
Functional GUIs with F#Functional GUIs with F#
Functional GUIs with F#Frank Krueger
 

Similaire à Bar graph (20)

Oleksandr Tolstykh
Oleksandr TolstykhOleksandr Tolstykh
Oleksandr Tolstykh
 
Reactive clean architecture
Reactive clean architectureReactive clean architecture
Reactive clean architecture
 
Opensource gis development - part 3
Opensource gis development - part 3Opensource gis development - part 3
Opensource gis development - part 3
 
Keep getting a null pointer exception for some odd reasonim creati.pdf
Keep getting a null pointer exception for some odd reasonim creati.pdfKeep getting a null pointer exception for some odd reasonim creati.pdf
Keep getting a null pointer exception for some odd reasonim creati.pdf
 
T
TT
T
 
How to become an Android dev starting from iOS (and vice versa)
How to become an Android dev starting from iOS (and vice versa)How to become an Android dev starting from iOS (and vice versa)
How to become an Android dev starting from iOS (and vice versa)
 
Chat application in java using swing and socket programming.
Chat application in java using swing and socket programming.Chat application in java using swing and socket programming.
Chat application in java using swing and socket programming.
 
resume_Alexey_Zaytsev
resume_Alexey_Zaytsevresume_Alexey_Zaytsev
resume_Alexey_Zaytsev
 
Use the following data set that compares age to average years lef.docx
Use the following data set that compares age to average years lef.docxUse the following data set that compares age to average years lef.docx
Use the following data set that compares age to average years lef.docx
 
Flink Forward San Francisco 2018: Seth Wiesman - "Testing Stateful Streaming ...
Flink Forward San Francisco 2018: Seth Wiesman - "Testing Stateful Streaming ...Flink Forward San Francisco 2018: Seth Wiesman - "Testing Stateful Streaming ...
Flink Forward San Francisco 2018: Seth Wiesman - "Testing Stateful Streaming ...
 
Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#
 
Mixing Functional and Object Oriented Approaches to Programming in C#
Mixing Functional and Object Oriented Approaches to Programming in C#Mixing Functional and Object Oriented Approaches to Programming in C#
Mixing Functional and Object Oriented Approaches to Programming in C#
 
Creating a Facebook Clone - Part XXIX - Transcript.pdf
Creating a Facebook Clone - Part XXIX - Transcript.pdfCreating a Facebook Clone - Part XXIX - Transcript.pdf
Creating a Facebook Clone - Part XXIX - Transcript.pdf
 
Working effectively with legacy code
Working effectively with legacy codeWorking effectively with legacy code
Working effectively with legacy code
 
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdfCreat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
 
Wicket KT part 2
Wicket KT part 2Wicket KT part 2
Wicket KT part 2
 
React new features and intro to Hooks
React new features and intro to HooksReact new features and intro to Hooks
React new features and intro to Hooks
 
JBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java ProgrammersJBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java Programmers
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
 
Functional GUIs with F#
Functional GUIs with F#Functional GUIs with F#
Functional GUIs with F#
 

Dernier

Top profile Call Girls In Palghar [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In Palghar [ 7014168258 ] Call Me For Genuine Models W...Top profile Call Girls In Palghar [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In Palghar [ 7014168258 ] Call Me For Genuine Models W...gajnagarg
 
Abortion Pill for sale in Riyadh ((+918761049707) Get Cytotec in Dammam
Abortion Pill for sale in Riyadh ((+918761049707) Get Cytotec in DammamAbortion Pill for sale in Riyadh ((+918761049707) Get Cytotec in Dammam
Abortion Pill for sale in Riyadh ((+918761049707) Get Cytotec in Dammamahmedjiabur940
 
一比一定(购)新西兰林肯大学毕业证(Lincoln毕业证)成绩单学位证
一比一定(购)新西兰林肯大学毕业证(Lincoln毕业证)成绩单学位证一比一定(购)新西兰林肯大学毕业证(Lincoln毕业证)成绩单学位证
一比一定(购)新西兰林肯大学毕业证(Lincoln毕业证)成绩单学位证wpkuukw
 
Hilti's Latest Battery - Hire Depot.pptx
Hilti's Latest Battery - Hire Depot.pptxHilti's Latest Battery - Hire Depot.pptx
Hilti's Latest Battery - Hire Depot.pptxhiredepot6
 
CRISIS COMMUNICATION presentation=-Rishabh(11195)-group ppt (4).pptx
CRISIS COMMUNICATION presentation=-Rishabh(11195)-group ppt (4).pptxCRISIS COMMUNICATION presentation=-Rishabh(11195)-group ppt (4).pptx
CRISIS COMMUNICATION presentation=-Rishabh(11195)-group ppt (4).pptxRishabh332761
 
一比一原版(USYD毕业证书)澳洲悉尼大学毕业证如何办理
一比一原版(USYD毕业证书)澳洲悉尼大学毕业证如何办理一比一原版(USYD毕业证书)澳洲悉尼大学毕业证如何办理
一比一原版(USYD毕业证书)澳洲悉尼大学毕业证如何办理uodye
 
怎样办理维多利亚大学毕业证(UVic毕业证书)成绩单留信认证
怎样办理维多利亚大学毕业证(UVic毕业证书)成绩单留信认证怎样办理维多利亚大学毕业证(UVic毕业证书)成绩单留信认证
怎样办理维多利亚大学毕业证(UVic毕业证书)成绩单留信认证tufbav
 
在线制作(ANU毕业证书)澳大利亚国立大学毕业证成绩单原版一比一
在线制作(ANU毕业证书)澳大利亚国立大学毕业证成绩单原版一比一在线制作(ANU毕业证书)澳大利亚国立大学毕业证成绩单原版一比一
在线制作(ANU毕业证书)澳大利亚国立大学毕业证成绩单原版一比一ougvy
 
怎样办理阿德莱德大学毕业证(Adelaide毕业证书)成绩单留信认证
怎样办理阿德莱德大学毕业证(Adelaide毕业证书)成绩单留信认证怎样办理阿德莱德大学毕业证(Adelaide毕业证书)成绩单留信认证
怎样办理阿德莱德大学毕业证(Adelaide毕业证书)成绩单留信认证ehyxf
 
Top profile Call Girls In Udgir [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Udgir [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Udgir [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Udgir [ 7014168258 ] Call Me For Genuine Models We ...gajnagarg
 
一比一原版(Otago毕业证书)奥塔哥理工学院毕业证成绩单学位证靠谱定制
一比一原版(Otago毕业证书)奥塔哥理工学院毕业证成绩单学位证靠谱定制一比一原版(Otago毕业证书)奥塔哥理工学院毕业证成绩单学位证靠谱定制
一比一原版(Otago毕业证书)奥塔哥理工学院毕业证成绩单学位证靠谱定制uodye
 
怎样办理斯威本科技大学毕业证(SUT毕业证书)成绩单留信认证
怎样办理斯威本科技大学毕业证(SUT毕业证书)成绩单留信认证怎样办理斯威本科技大学毕业证(SUT毕业证书)成绩单留信认证
怎样办理斯威本科技大学毕业证(SUT毕业证书)成绩单留信认证tufbav
 
Vashi Affordable Call Girls ,07506202331,Vasai Virar Charming Call Girl
Vashi Affordable Call Girls ,07506202331,Vasai Virar Charming Call GirlVashi Affordable Call Girls ,07506202331,Vasai Virar Charming Call Girl
Vashi Affordable Call Girls ,07506202331,Vasai Virar Charming Call GirlPriya Reddy
 
Mass storage systems presentation operating systems
Mass storage systems presentation operating systemsMass storage systems presentation operating systems
Mass storage systems presentation operating systemsnight1ng4ale
 
Call Girls Amethi 9332606886 HOT & SEXY Models beautiful and charming call g...
Call Girls Amethi  9332606886 HOT & SEXY Models beautiful and charming call g...Call Girls Amethi  9332606886 HOT & SEXY Models beautiful and charming call g...
Call Girls Amethi 9332606886 HOT & SEXY Models beautiful and charming call g...Sareena Khatun
 
🌹Patia⬅️ Vip Call Girls Bhubaneswar 📱9777949614 Book Well Trand Call Girls In...
🌹Patia⬅️ Vip Call Girls Bhubaneswar 📱9777949614 Book Well Trand Call Girls In...🌹Patia⬅️ Vip Call Girls Bhubaneswar 📱9777949614 Book Well Trand Call Girls In...
🌹Patia⬅️ Vip Call Girls Bhubaneswar 📱9777949614 Book Well Trand Call Girls In...Call Girls Mumbai
 

Dernier (20)

Top profile Call Girls In Palghar [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In Palghar [ 7014168258 ] Call Me For Genuine Models W...Top profile Call Girls In Palghar [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In Palghar [ 7014168258 ] Call Me For Genuine Models W...
 
In Riyadh Saudi Arabia |+966572737505 | Buy Cytotec| Get Abortion pills
In Riyadh Saudi Arabia |+966572737505 | Buy Cytotec| Get Abortion pillsIn Riyadh Saudi Arabia |+966572737505 | Buy Cytotec| Get Abortion pills
In Riyadh Saudi Arabia |+966572737505 | Buy Cytotec| Get Abortion pills
 
Abortion Pill for sale in Riyadh ((+918761049707) Get Cytotec in Dammam
Abortion Pill for sale in Riyadh ((+918761049707) Get Cytotec in DammamAbortion Pill for sale in Riyadh ((+918761049707) Get Cytotec in Dammam
Abortion Pill for sale in Riyadh ((+918761049707) Get Cytotec in Dammam
 
一比一定(购)新西兰林肯大学毕业证(Lincoln毕业证)成绩单学位证
一比一定(购)新西兰林肯大学毕业证(Lincoln毕业证)成绩单学位证一比一定(购)新西兰林肯大学毕业证(Lincoln毕业证)成绩单学位证
一比一定(购)新西兰林肯大学毕业证(Lincoln毕业证)成绩单学位证
 
Hilti's Latest Battery - Hire Depot.pptx
Hilti's Latest Battery - Hire Depot.pptxHilti's Latest Battery - Hire Depot.pptx
Hilti's Latest Battery - Hire Depot.pptx
 
CRISIS COMMUNICATION presentation=-Rishabh(11195)-group ppt (4).pptx
CRISIS COMMUNICATION presentation=-Rishabh(11195)-group ppt (4).pptxCRISIS COMMUNICATION presentation=-Rishabh(11195)-group ppt (4).pptx
CRISIS COMMUNICATION presentation=-Rishabh(11195)-group ppt (4).pptx
 
一比一原版(USYD毕业证书)澳洲悉尼大学毕业证如何办理
一比一原版(USYD毕业证书)澳洲悉尼大学毕业证如何办理一比一原版(USYD毕业证书)澳洲悉尼大学毕业证如何办理
一比一原版(USYD毕业证书)澳洲悉尼大学毕业证如何办理
 
怎样办理维多利亚大学毕业证(UVic毕业证书)成绩单留信认证
怎样办理维多利亚大学毕业证(UVic毕业证书)成绩单留信认证怎样办理维多利亚大学毕业证(UVic毕业证书)成绩单留信认证
怎样办理维多利亚大学毕业证(UVic毕业证书)成绩单留信认证
 
在线制作(ANU毕业证书)澳大利亚国立大学毕业证成绩单原版一比一
在线制作(ANU毕业证书)澳大利亚国立大学毕业证成绩单原版一比一在线制作(ANU毕业证书)澳大利亚国立大学毕业证成绩单原版一比一
在线制作(ANU毕业证书)澳大利亚国立大学毕业证成绩单原版一比一
 
怎样办理阿德莱德大学毕业证(Adelaide毕业证书)成绩单留信认证
怎样办理阿德莱德大学毕业证(Adelaide毕业证书)成绩单留信认证怎样办理阿德莱德大学毕业证(Adelaide毕业证书)成绩单留信认证
怎样办理阿德莱德大学毕业证(Adelaide毕业证书)成绩单留信认证
 
Abortion pills in Dammam +966572737505 Buy Cytotec
Abortion pills in Dammam +966572737505 Buy CytotecAbortion pills in Dammam +966572737505 Buy Cytotec
Abortion pills in Dammam +966572737505 Buy Cytotec
 
Top profile Call Girls In Udgir [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Udgir [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Udgir [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Udgir [ 7014168258 ] Call Me For Genuine Models We ...
 
一比一原版(Otago毕业证书)奥塔哥理工学院毕业证成绩单学位证靠谱定制
一比一原版(Otago毕业证书)奥塔哥理工学院毕业证成绩单学位证靠谱定制一比一原版(Otago毕业证书)奥塔哥理工学院毕业证成绩单学位证靠谱定制
一比一原版(Otago毕业证书)奥塔哥理工学院毕业证成绩单学位证靠谱定制
 
怎样办理斯威本科技大学毕业证(SUT毕业证书)成绩单留信认证
怎样办理斯威本科技大学毕业证(SUT毕业证书)成绩单留信认证怎样办理斯威本科技大学毕业证(SUT毕业证书)成绩单留信认证
怎样办理斯威本科技大学毕业证(SUT毕业证书)成绩单留信认证
 
Buy Abortion pills in Riyadh |+966572737505 | Get Cytotec
Buy Abortion pills in Riyadh |+966572737505 | Get CytotecBuy Abortion pills in Riyadh |+966572737505 | Get Cytotec
Buy Abortion pills in Riyadh |+966572737505 | Get Cytotec
 
Critical Commentary Social Work Ethics.pptx
Critical Commentary Social Work Ethics.pptxCritical Commentary Social Work Ethics.pptx
Critical Commentary Social Work Ethics.pptx
 
Vashi Affordable Call Girls ,07506202331,Vasai Virar Charming Call Girl
Vashi Affordable Call Girls ,07506202331,Vasai Virar Charming Call GirlVashi Affordable Call Girls ,07506202331,Vasai Virar Charming Call Girl
Vashi Affordable Call Girls ,07506202331,Vasai Virar Charming Call Girl
 
Mass storage systems presentation operating systems
Mass storage systems presentation operating systemsMass storage systems presentation operating systems
Mass storage systems presentation operating systems
 
Call Girls Amethi 9332606886 HOT & SEXY Models beautiful and charming call g...
Call Girls Amethi  9332606886 HOT & SEXY Models beautiful and charming call g...Call Girls Amethi  9332606886 HOT & SEXY Models beautiful and charming call g...
Call Girls Amethi 9332606886 HOT & SEXY Models beautiful and charming call g...
 
🌹Patia⬅️ Vip Call Girls Bhubaneswar 📱9777949614 Book Well Trand Call Girls In...
🌹Patia⬅️ Vip Call Girls Bhubaneswar 📱9777949614 Book Well Trand Call Girls In...🌹Patia⬅️ Vip Call Girls Bhubaneswar 📱9777949614 Book Well Trand Call Girls In...
🌹Patia⬅️ Vip Call Girls Bhubaneswar 📱9777949614 Book Well Trand Call Girls In...
 

Bar graph

  • 3. Why Is He Doing This?
  • 4. The Problem:  Two frames (Views) and a data structure (Model).  Observer Pattern:
  • 5. Observer Pattern Times Four:  JTextField: ActionListener receives event after you hit <return>.  The ActionListener updates the Data (Model) object. JTextField ActionListener addActionListener actionPerformed anonymous object
  • 6. Observer Pattern Times Four:  Data: Is observed by the BarGraph (ChangeListener) for any changes to the model. Data ChangeListener attach stateChanged BarGraph
  • 7. Observer Pattern Times Four:   BarGraph: MouseListener receives event when you click on the bar graph. mousePressed() moves the bar and updates Data object. BarGraph MouseListener addMouseListener mousePressed Mouse Adapter
  • 8. Observer Pattern Times Four:  Data: Is observed by TextFrame (ChangeListener) for any changes to data. Data ChangeListener attach stateChanged TextFrame
  • 12. Data.java public class Data { public Data(ArrayList<Double> d) { data = d; listeners = new ArrayList<ChangeListener>(); } public ArrayList<Double> getData() { return (ArrayList<Double>) (data.clone()); } public void attach(ChangeListener c) { listeners.add(c); } }
  • 13. Data.java public class Data { public void update(int loc, double val) { if (val > MAX) val = MAX; if (val < 0) val = 0; data.set(loc, new Double(val)); // for each listener, let the listener know a change has occurred for (ChangeListener l : listeners) { l.stateChanged(new ChangeEvent(this)); } } public Double get(int i) { return data.get(i); } private ArrayList<Double> data; private ArrayList<ChangeListener> listeners; public static final double MAX = 100.0; }
  • 14. TextFrame: public class TextFrame extends JFrame implements ChangeListener { public TextFrame (Data md) { d = md; ActionListener l = new ActionListener() { public void actionPerformed(ActionEvent e) { JTextField c = (JTextField) e.getSource(); int ndx = findBox(c); if (ndx >= 0) d.update(ndx,Double.parseDouble(c.getText().trim())); } }; JPanel tPanel = new JPanel(); tPanel.setLayout(new BoxLayout(tPanel,BoxLayout.PAGE_AXIS)); ... } } tf = new JTextField[5]; for (int i = 0; i < 5; i++) { tf[i] = new JTextField(10); tf[i].addActionListener(l); tPanel.add(tf[i]); } add(tPanel, BorderLayout.CENTER); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setVisible(true);
  • 15. TextFrame: @Override public void stateChanged(ChangeEvent e) { // how I notify the text frame if the bar graph changes for (int i = 0; i < 5; i++){ double d1 = d.get(i).doubleValue(); String s = tf[i].getText().trim(); double d2; try { d2 = Double.parseDouble(s); } catch(NumberFormatException nfe ) { d2 = 0.0; } if (Math.abs(d1 - d2) > 0.01) tf[i].setText(new Double(d1).toString()); } } public int findBox(JTextField t) { int i = 0; while (i < 5) { if (t == tf[i]) return i; i++; } return -1; } } private Data d; private JtextField[ ] tf;
  • 16. BarGraph: public class BarGraph extends JFrame implements ChangeListener { public BarGraph(Data md) { d = md; setLocation(0,300); Icon barIcon = new Icon() { public int getIconWidth() { return WIDTH; } public int getIconHeight() { return HEIGHT; } } public void paintIcon(Component c, Graphics g , int x, int y) { Graphics2D g2 = (Graphics2D) g; Color cl = g2.getColor(); for (int i = 0; i < 5; i++) { Rectangle2D.Double rectangle = new Rectangle2D.Double(0, (HEIGHT/6)*i, WIDTH*(d.get(i).doubleValue()/Data.MAX), (HEIGHT/6)); g2.setPaint(Color.cyan); g2.fill(rectangle); g2.setColor(Color.black); g2.draw(rectangle); } Rectangle2D.Double rectangle = new Rectangle2D.Double(0,375,WIDTH,(HEIGHT/6)); g2.setPaint(cl); g2.fill(rectangle);g2.setColor(cl);g2.draw(rectangle); } }; // end of barIcon panel ...
  • 17. BarGraph: public class BarGraph extends JFrame implements ChangeListener { public BarGraph(Data md) { ... add(new JLabel(barIcon)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MouseListener m = new MouseAdapter() { public void mousePressed(MouseEvent e){ Point2D p = e.getPoint(); double dy = p.getY(); int barToMove = (int)(dy / BARHEIGHT); if (barToMove < 0 || barToMove > 4) return; double dx = p.getX(); double newV = (dx/WIDTH)* Data.MAX; d.update(barToMove, newV); } }; addMouseListener(m); pack(); setVisible(true); } // end constructor } // end class