HELP Modify the code so that the ListItem contains two value

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

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 BOT
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 BOT
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 BOT

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site