SlideShare a Scribd company logo
1 of 4
Download to read offline
HELP
Modify the code so that the ListItem contains two values, instead of just one. Name the new
value valB. Make all the modifications necessary to display BOTH values -- valA and valB -- for
each ListItem in the GUI window. Supply a second set of values for valB, to complement the
given valA.
Then, without modifying the compareTo() method you just created in part c., change how the
ListItems are sorted, such that they are sorted from highest to lowest according to the value of
the new field, valB.
Solution
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import javax.swing.Icon;
public class ListItem implements Comparable
{
private int valA;
private int valB;
public ListItem (int valA,int B)
{
this.valA = valA;
}
public int getValA ()
{
return valA;
}
public int getValB()
{
return valB;
}
public Icon createIcon (int diameter) {
return new ItemIcon (this, diameter);
}
@Override
public int compareTo (ListItem o) {
// TODO Auto-generated method stub
return 0;
}
public class ItemIcon implements Icon
{
private final static int DEFAULT_THICKNESS = 5;
private final static int DEFAULT_DIAMETER = 50;
private int width = DEFAULT_DIAMETER;
private int height = DEFAULT_DIAMETER;
private int thickness = DEFAULT_THICKNESS;
private int borderSize = DEFAULT_THICKNESS;
private int valA;
private int valB;
public ItemIcon (int valA,int valB) {
this.valA = valA;
this.valB=valB;
}
public ItemIcon (int valA, int valB,int diameter)
{
this.valA=valA;
this.valB=valB;
setDiameter (diameter);
}
public ItemIcon (ListItem item) {
this.valA = item.getValA();
this.valB=item.getValB();
}
public ItemIcon (ListItem item, int diameter) {
this (item);
setDiameter (diameter);
}
public void setDiameter (int diameter) {
width = diameter;
height = diameter;
}
public int getIconHeight() {
return height + 2 * DEFAULT_THICKNESS;
}
public int getIconWidth() {
return width + 2 * DEFAULT_THICKNESS;
}
public void paintIcon (Component comp, Graphics g, int x, int y)
{
// get graphics context
Graphics2D g2 = (Graphics2D) g;
// set stroke size and color
g2.setPaint (Color.BLACK);
Stroke s = new BasicStroke (thickness);
g2.setStroke (s);
// draw white-filled circle with red border
Ellipse2D e1 = new Ellipse2D.Float (x + borderSize, y + borderSize, height, width);
g2.draw (e1);
g2.setPaint (Color.WHITE);
g2.fill (e1);
g2.setPaint (Color.RED);
// draw text
Font f = new Font ("Arial", Font.BOLD, 14);
FontMetrics fm = g.getFontMetrics (f);
String str = ((Integer) valA).toString();
Rectangle2D rect = fm.getStringBounds (str, g2);
int textHeight = (int) rect.getHeight();
int textWidth = (int) rect.getWidth();
int panelHeight= getIconHeight();
int panelWidth = getIconWidth();
// Center text horizontally and vertically
int offsetX = (panelWidth - textWidth) / 2;
int offsetY = (panelHeight - textHeight) / 2 + fm.getAscent();
// Draw the string.
g2.setFont (f);
g2.drawString (str, x + offsetX, y + offsetY);
}
}
}

More Related Content

Similar to HELPModify the code so that the ListItem contains two values, inst.pdf

Please help!!I wanted to know how to add a high score to this prog.pdf
Please help!!I wanted to know how to add a high score to this prog.pdfPlease help!!I wanted to know how to add a high score to this prog.pdf
Please help!!I wanted to know how to add a high score to this prog.pdf
JUSTSTYLISH3B2MOHALI
 
package chapter15;import javafx.application.Application;import j.pdf
package chapter15;import javafx.application.Application;import j.pdfpackage chapter15;import javafx.application.Application;import j.pdf
package chapter15;import javafx.application.Application;import j.pdf
KARTIKINDIA
 
Need help with questions 2-4. Write assembly code to find absolute v.pdf
Need help with questions 2-4. Write assembly code to find absolute v.pdfNeed help with questions 2-4. Write assembly code to find absolute v.pdf
Need help with questions 2-4. Write assembly code to find absolute v.pdf
feelinggifts
 
Hello. I need help fixing this Java Code on Eclipse. Please fix part.pdf
Hello. I need help fixing this Java Code on Eclipse. Please fix part.pdfHello. I need help fixing this Java Code on Eclipse. Please fix part.pdf
Hello. I need help fixing this Java Code on Eclipse. Please fix part.pdf
flashfashioncasualwe
 
VISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLEVISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLE
Darwin Durand
 
Set up a JavaFX GUI-based program that shows a 10 times 10 grid of la.pdf
Set up a JavaFX GUI-based program that shows a 10 times 10 grid of la.pdfSet up a JavaFX GUI-based program that shows a 10 times 10 grid of la.pdf
Set up a JavaFX GUI-based program that shows a 10 times 10 grid of la.pdf
xlynettalampleyxc
 
Scala+swing
Scala+swingScala+swing
Scala+swing
perneto
 
Please read this carefully needs to be in JAVA        Java 2D intr.pdf
Please read this carefully needs to be in JAVA        Java 2D intr.pdfPlease read this carefully needs to be in JAVA        Java 2D intr.pdf
Please read this carefully needs to be in JAVA        Java 2D intr.pdf
PRATIKSINHA7304
 

Similar to HELPModify the code so that the ListItem contains two values, inst.pdf (20)

CSharp v1.0.2
CSharp v1.0.2CSharp v1.0.2
CSharp v1.0.2
 
Awt
AwtAwt
Awt
 
Please help!!I wanted to know how to add a high score to this prog.pdf
Please help!!I wanted to know how to add a high score to this prog.pdfPlease help!!I wanted to know how to add a high score to this prog.pdf
Please help!!I wanted to know how to add a high score to this prog.pdf
 
Applet
AppletApplet
Applet
 
AWT New-3.pptx
AWT New-3.pptxAWT New-3.pptx
AWT New-3.pptx
 
Java awt
Java awtJava awt
Java awt
 
Java_practical_handbook
Java_practical_handbookJava_practical_handbook
Java_practical_handbook
 
package chapter15;import javafx.application.Application;import j.pdf
package chapter15;import javafx.application.Application;import j.pdfpackage chapter15;import javafx.application.Application;import j.pdf
package chapter15;import javafx.application.Application;import j.pdf
 
Need help with questions 2-4. Write assembly code to find absolute v.pdf
Need help with questions 2-4. Write assembly code to find absolute v.pdfNeed help with questions 2-4. Write assembly code to find absolute v.pdf
Need help with questions 2-4. Write assembly code to find absolute v.pdf
 
Hello. I need help fixing this Java Code on Eclipse. Please fix part.pdf
Hello. I need help fixing this Java Code on Eclipse. Please fix part.pdfHello. I need help fixing this Java Code on Eclipse. Please fix part.pdf
Hello. I need help fixing this Java Code on Eclipse. Please fix part.pdf
 
Chap1 1.4
Chap1 1.4Chap1 1.4
Chap1 1.4
 
VISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLEVISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLE
 
qooxdoo Form Management
qooxdoo Form Managementqooxdoo Form Management
qooxdoo Form Management
 
AJUG April 2011 Cascading example
AJUG April 2011 Cascading exampleAJUG April 2011 Cascading example
AJUG April 2011 Cascading example
 
JDD 2016 - Pawel Byszewski - Kotlin, why?
JDD 2016 - Pawel Byszewski - Kotlin, why?JDD 2016 - Pawel Byszewski - Kotlin, why?
JDD 2016 - Pawel Byszewski - Kotlin, why?
 
Set up a JavaFX GUI-based program that shows a 10 times 10 grid of la.pdf
Set up a JavaFX GUI-based program that shows a 10 times 10 grid of la.pdfSet up a JavaFX GUI-based program that shows a 10 times 10 grid of la.pdf
Set up a JavaFX GUI-based program that shows a 10 times 10 grid of la.pdf
 
662305 11
662305 11662305 11
662305 11
 
Scala+swing
Scala+swingScala+swing
Scala+swing
 
Please read this carefully needs to be in JAVA        Java 2D intr.pdf
Please read this carefully needs to be in JAVA        Java 2D intr.pdfPlease read this carefully needs to be in JAVA        Java 2D intr.pdf
Please read this carefully needs to be in JAVA        Java 2D intr.pdf
 
This code currently works... Run it and get a screen shot of its .docx
 This code currently works... Run it and get a screen shot of its .docx This code currently works... Run it and get a screen shot of its .docx
This code currently works... Run it and get a screen shot of its .docx
 

More from monikajain201

Most human genetic disorders are due to autosomal recessive inheritan.pdf
Most human genetic disorders are due to autosomal recessive inheritan.pdfMost human genetic disorders are due to autosomal recessive inheritan.pdf
Most human genetic disorders are due to autosomal recessive inheritan.pdf
monikajain201
 
Design a database to store details about U.S. presidents and their t.pdf
Design a database to store details about U.S. presidents and their t.pdfDesign a database to store details about U.S. presidents and their t.pdf
Design a database to store details about U.S. presidents and their t.pdf
monikajain201
 
Chapter 1 - A View of Life and its Chemical Basis. 1. Explain the.pdf
Chapter 1 - A View of Life and its Chemical Basis. 1. Explain the.pdfChapter 1 - A View of Life and its Chemical Basis. 1. Explain the.pdf
Chapter 1 - A View of Life and its Chemical Basis. 1. Explain the.pdf
monikajain201
 
CHOOSE THE CORRECT ANSWER1) PVC Corporation is considering an inv.pdf
CHOOSE THE CORRECT ANSWER1) PVC Corporation is considering an inv.pdfCHOOSE THE CORRECT ANSWER1) PVC Corporation is considering an inv.pdf
CHOOSE THE CORRECT ANSWER1) PVC Corporation is considering an inv.pdf
monikajain201
 
Civil Infrastructure Problems Why the public is not concerned about t.pdf
Civil Infrastructure Problems Why the public is not concerned about t.pdfCivil Infrastructure Problems Why the public is not concerned about t.pdf
Civil Infrastructure Problems Why the public is not concerned about t.pdf
monikajain201
 
Briefly discuss the adaptive changes seen in the chordates over the o.pdf
Briefly discuss the adaptive changes seen in the chordates over the o.pdfBriefly discuss the adaptive changes seen in the chordates over the o.pdf
Briefly discuss the adaptive changes seen in the chordates over the o.pdf
monikajain201
 
Argue in favor or against the following statements (include examples.pdf
Argue in favor or against the following statements (include examples.pdfArgue in favor or against the following statements (include examples.pdf
Argue in favor or against the following statements (include examples.pdf
monikajain201
 
What is the role of sequence numbers in the rdt protocolWhat is t.pdf
What is the role of sequence numbers in the rdt protocolWhat is t.pdfWhat is the role of sequence numbers in the rdt protocolWhat is t.pdf
What is the role of sequence numbers in the rdt protocolWhat is t.pdf
monikajain201
 

More from monikajain201 (20)

Phospholipids make up most of the lipid part of the cell membrane. S.pdf
Phospholipids make up most of the lipid part of the cell membrane. S.pdfPhospholipids make up most of the lipid part of the cell membrane. S.pdf
Phospholipids make up most of the lipid part of the cell membrane. S.pdf
 
Most human genetic disorders are due to autosomal recessive inheritan.pdf
Most human genetic disorders are due to autosomal recessive inheritan.pdfMost human genetic disorders are due to autosomal recessive inheritan.pdf
Most human genetic disorders are due to autosomal recessive inheritan.pdf
 
List all cyclic subgroups of U_24.SolutionThe group consists of.pdf
List all cyclic subgroups of U_24.SolutionThe group consists of.pdfList all cyclic subgroups of U_24.SolutionThe group consists of.pdf
List all cyclic subgroups of U_24.SolutionThe group consists of.pdf
 
Java Data StructuresI know how to construct the Node class but sti.pdf
Java Data StructuresI know how to construct the Node class but sti.pdfJava Data StructuresI know how to construct the Node class but sti.pdf
Java Data StructuresI know how to construct the Node class but sti.pdf
 
It is our intention to produce interference fringes by illuminating s.pdf
It is our intention to produce interference fringes by illuminating s.pdfIt is our intention to produce interference fringes by illuminating s.pdf
It is our intention to produce interference fringes by illuminating s.pdf
 
Find the second virial coefficient for the Dieterici equation of sta.pdf
Find the second virial coefficient for the Dieterici equation of sta.pdfFind the second virial coefficient for the Dieterici equation of sta.pdf
Find the second virial coefficient for the Dieterici equation of sta.pdf
 
Find the composite phi and velocity field. Find the velocity vector.pdf
Find the composite phi and velocity field. Find the velocity vector.pdfFind the composite phi and velocity field. Find the velocity vector.pdf
Find the composite phi and velocity field. Find the velocity vector.pdf
 
EnvironmentalCivil Engineering QuestionWhen chemicals are added t.pdf
EnvironmentalCivil Engineering QuestionWhen chemicals are added t.pdfEnvironmentalCivil Engineering QuestionWhen chemicals are added t.pdf
EnvironmentalCivil Engineering QuestionWhen chemicals are added t.pdf
 
Did Carl Linnaeus incorporate evolutionary thinking in the developme.pdf
Did Carl Linnaeus incorporate evolutionary thinking in the developme.pdfDid Carl Linnaeus incorporate evolutionary thinking in the developme.pdf
Did Carl Linnaeus incorporate evolutionary thinking in the developme.pdf
 
Do you think wood is mechanically homogeneous (i.e. as strong in all.pdf
Do you think wood is mechanically homogeneous (i.e. as strong in all.pdfDo you think wood is mechanically homogeneous (i.e. as strong in all.pdf
Do you think wood is mechanically homogeneous (i.e. as strong in all.pdf
 
Design a database to store details about U.S. presidents and their t.pdf
Design a database to store details about U.S. presidents and their t.pdfDesign a database to store details about U.S. presidents and their t.pdf
Design a database to store details about U.S. presidents and their t.pdf
 
Chapter 1 - A View of Life and its Chemical Basis. 1. Explain the.pdf
Chapter 1 - A View of Life and its Chemical Basis. 1. Explain the.pdfChapter 1 - A View of Life and its Chemical Basis. 1. Explain the.pdf
Chapter 1 - A View of Life and its Chemical Basis. 1. Explain the.pdf
 
CHOOSE THE CORRECT ANSWER1) PVC Corporation is considering an inv.pdf
CHOOSE THE CORRECT ANSWER1) PVC Corporation is considering an inv.pdfCHOOSE THE CORRECT ANSWER1) PVC Corporation is considering an inv.pdf
CHOOSE THE CORRECT ANSWER1) PVC Corporation is considering an inv.pdf
 
Civil Infrastructure Problems Why the public is not concerned about t.pdf
Civil Infrastructure Problems Why the public is not concerned about t.pdfCivil Infrastructure Problems Why the public is not concerned about t.pdf
Civil Infrastructure Problems Why the public is not concerned about t.pdf
 
Cell Structure and FunctionLabel each of the arrows in the followi.pdf
Cell Structure and FunctionLabel each of the arrows in the followi.pdfCell Structure and FunctionLabel each of the arrows in the followi.pdf
Cell Structure and FunctionLabel each of the arrows in the followi.pdf
 
C Language ProblemThere are more than one wrong things I think.P.pdf
C Language ProblemThere are more than one wrong things I think.P.pdfC Language ProblemThere are more than one wrong things I think.P.pdf
C Language ProblemThere are more than one wrong things I think.P.pdf
 
Briefly discuss the adaptive changes seen in the chordates over the o.pdf
Briefly discuss the adaptive changes seen in the chordates over the o.pdfBriefly discuss the adaptive changes seen in the chordates over the o.pdf
Briefly discuss the adaptive changes seen in the chordates over the o.pdf
 
Argue in favor or against the following statements (include examples.pdf
Argue in favor or against the following statements (include examples.pdfArgue in favor or against the following statements (include examples.pdf
Argue in favor or against the following statements (include examples.pdf
 
27. What is a prosthetic group Identify prosthetic groups in the fol.pdf
27. What is a prosthetic group Identify prosthetic groups in the fol.pdf27. What is a prosthetic group Identify prosthetic groups in the fol.pdf
27. What is a prosthetic group Identify prosthetic groups in the fol.pdf
 
What is the role of sequence numbers in the rdt protocolWhat is t.pdf
What is the role of sequence numbers in the rdt protocolWhat is t.pdfWhat is the role of sequence numbers in the rdt protocolWhat is t.pdf
What is the role of sequence numbers in the rdt protocolWhat is t.pdf
 

Recently uploaded

SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
CaitlinCummins3
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
中 央社
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
Peter Brusilovsky
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
中 央社
 

Recently uploaded (20)

e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 

HELPModify the code so that the ListItem contains two values, inst.pdf

  • 1. HELP Modify the code so that the ListItem contains two values, instead of just one. Name the new value valB. Make all the modifications necessary to display BOTH values -- valA and valB -- for each ListItem in the GUI window. Supply a second set of values for valB, to complement the given valA. Then, without modifying the compareTo() method you just created in part c., change how the ListItems are sorted, such that they are sorted from highest to lowest according to the value of the new field, valB. Solution import java.awt.BasicStroke; import java.awt.Color; import java.awt.Component; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Stroke; import java.awt.geom.Ellipse2D; import java.awt.geom.Rectangle2D; import javax.swing.Icon; public class ListItem implements Comparable { private int valA; private int valB; public ListItem (int valA,int B) { this.valA = valA; } public int getValA ()
  • 2. { return valA; } public int getValB() { return valB; } public Icon createIcon (int diameter) { return new ItemIcon (this, diameter); } @Override public int compareTo (ListItem o) { // TODO Auto-generated method stub return 0; } public class ItemIcon implements Icon { private final static int DEFAULT_THICKNESS = 5; private final static int DEFAULT_DIAMETER = 50; private int width = DEFAULT_DIAMETER; private int height = DEFAULT_DIAMETER; private int thickness = DEFAULT_THICKNESS; private int borderSize = DEFAULT_THICKNESS; private int valA; private int valB; public ItemIcon (int valA,int valB) { this.valA = valA; this.valB=valB; }
  • 3. public ItemIcon (int valA, int valB,int diameter) { this.valA=valA; this.valB=valB; setDiameter (diameter); } public ItemIcon (ListItem item) { this.valA = item.getValA(); this.valB=item.getValB(); } public ItemIcon (ListItem item, int diameter) { this (item); setDiameter (diameter); } public void setDiameter (int diameter) { width = diameter; height = diameter; } public int getIconHeight() { return height + 2 * DEFAULT_THICKNESS; } public int getIconWidth() { return width + 2 * DEFAULT_THICKNESS; } public void paintIcon (Component comp, Graphics g, int x, int y) { // get graphics context Graphics2D g2 = (Graphics2D) g; // set stroke size and color
  • 4. g2.setPaint (Color.BLACK); Stroke s = new BasicStroke (thickness); g2.setStroke (s); // draw white-filled circle with red border Ellipse2D e1 = new Ellipse2D.Float (x + borderSize, y + borderSize, height, width); g2.draw (e1); g2.setPaint (Color.WHITE); g2.fill (e1); g2.setPaint (Color.RED); // draw text Font f = new Font ("Arial", Font.BOLD, 14); FontMetrics fm = g.getFontMetrics (f); String str = ((Integer) valA).toString(); Rectangle2D rect = fm.getStringBounds (str, g2); int textHeight = (int) rect.getHeight(); int textWidth = (int) rect.getWidth(); int panelHeight= getIconHeight(); int panelWidth = getIconWidth(); // Center text horizontally and vertically int offsetX = (panelWidth - textWidth) / 2; int offsetY = (panelHeight - textHeight) / 2 + fm.getAscent(); // Draw the string. g2.setFont (f); g2.drawString (str, x + offsetX, y + offsetY); } } }