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

PDF
Please read this carefully needs to be in JAVA        Java 2D intr.pdf
DOCX
To change this template
PPTX
Online birth certificate system and computer engineering
PDF
PLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdf
PPT
PDF
Phospholipids make up most of the lipid part of the cell membrane. S.pdf
PDF
Most human genetic disorders are due to autosomal recessive inheritan.pdf
PDF
List all cyclic subgroups of U_24.SolutionThe group consists of.pdf
Please read this carefully needs to be in JAVA        Java 2D intr.pdf
To change this template
Online birth certificate system and computer engineering
PLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.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
List all cyclic subgroups of U_24.SolutionThe group consists of.pdf

More from monikajain201 (20)

PDF
Java Data StructuresI know how to construct the Node class but sti.pdf
PDF
It is our intention to produce interference fringes by illuminating s.pdf
PDF
Find the second virial coefficient for the Dieterici equation of sta.pdf
PDF
Find the composite phi and velocity field. Find the velocity vector.pdf
PDF
EnvironmentalCivil Engineering QuestionWhen chemicals are added t.pdf
PDF
Did Carl Linnaeus incorporate evolutionary thinking in the developme.pdf
PDF
Do you think wood is mechanically homogeneous (i.e. as strong in all.pdf
PDF
Design a database to store details about U.S. presidents and their t.pdf
PDF
Chapter 1 - A View of Life and its Chemical Basis. 1. Explain the.pdf
PDF
CHOOSE THE CORRECT ANSWER1) PVC Corporation is considering an inv.pdf
PDF
Civil Infrastructure Problems Why the public is not concerned about t.pdf
PDF
Cell Structure and FunctionLabel each of the arrows in the followi.pdf
PDF
C Language ProblemThere are more than one wrong things I think.P.pdf
PDF
Briefly discuss the adaptive changes seen in the chordates over the o.pdf
PDF
Argue in favor or against the following statements (include examples.pdf
PDF
27. What is a prosthetic group Identify prosthetic groups in the fol.pdf
PDF
What is the role of sequence numbers in the rdt protocolWhat is t.pdf
PDF
Which of the following statements about mitosis is true O a. Mitosis.pdf
PDF
Which of the following factors would NOT contribute to allopatric spe.pdf
PDF
What would be the exact Materials and Method for this experiment2.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
Find the second virial coefficient for the Dieterici equation of sta.pdf
Find the composite phi and velocity field. Find the velocity vector.pdf
EnvironmentalCivil Engineering QuestionWhen chemicals are added t.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
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
CHOOSE THE CORRECT ANSWER1) PVC Corporation is considering an inv.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
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
Argue in favor or against the following statements (include examples.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
Which of the following statements about mitosis is true O a. Mitosis.pdf
Which of the following factors would NOT contribute to allopatric spe.pdf
What would be the exact Materials and Method for this experiment2.pdf

Recently uploaded (20)

PDF
FORM 1 BIOLOGY MIND MAPS and their schemes
PDF
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
PPTX
Introduction to pro and eukaryotes and differences.pptx
PPTX
Computer Architecture Input Output Memory.pptx
PDF
Trump Administration's workforce development strategy
PPTX
Share_Module_2_Power_conflict_and_negotiation.pptx
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
PDF
What if we spent less time fighting change, and more time building what’s rig...
PDF
HVAC Specification 2024 according to central public works department
PDF
advance database management system book.pdf
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
PDF
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
PPTX
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
PDF
Empowerment Technology for Senior High School Guide
PPTX
20th Century Theater, Methods, History.pptx
PDF
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
PPTX
Virtual and Augmented Reality in Current Scenario
PDF
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
PPTX
B.Sc. DS Unit 2 Software Engineering.pptx
FORM 1 BIOLOGY MIND MAPS and their schemes
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
Introduction to pro and eukaryotes and differences.pptx
Computer Architecture Input Output Memory.pptx
Trump Administration's workforce development strategy
Share_Module_2_Power_conflict_and_negotiation.pptx
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
What if we spent less time fighting change, and more time building what’s rig...
HVAC Specification 2024 according to central public works department
advance database management system book.pdf
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
Empowerment Technology for Senior High School Guide
20th Century Theater, Methods, History.pptx
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
Virtual and Augmented Reality in Current Scenario
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
B.Sc. DS Unit 2 Software Engineering.pptx

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); } } }