The document provides instructions to modify a ListItem class to contain two values, valA and valB, instead of just one value. It asks to display both values for each ListItem in the GUI and provide a second set of values for valB. It also asks to change the sorting of the ListItems to sort by valB from highest to lowest without modifying the compareTo method.
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);
}
}
}