g2.setPaint issues

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Fred
Posts: 38
Joined: Sat Dec 12, 2015 4:57 pm
antibot: No, of course not.

g2.setPaint issues

Post by Fred » Thu Jan 26, 2017 10:55 pm

All,

I have a requirement using a customized VectorRenderer to show all associated vectors related to the one selected.
Its a bit involved but bear with me on this.

When creating the VectorXYDataset when the data exceeds the length of the screen another point the first vector
is created from a start point and the delta in X runs to the end of that screen. It then wraps by starting at the beginning
of the next line and continues until the full length of the vector has been accounted for. Each line represents a vectorseries
entry in the VectorXYDatatset. Now this works correctly. When an X/Y point is mouse-clicked, the first entry in the the series
(item=0) is colored (white) is presented correctly. However, the remaining lines show some shade of white along with the original
color for the series. what I desire is for all vectors to have the same color (white) as the first item (0) in the series.

Code snippet below:

Code: Select all

@Override
public void drawItem(Graphics2D g2, XYItemRendererState state, Rectangle2D dataArea, PlotRenderingInfo info, XYPLot plot,
                                ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, int series, int item, CrosshairState crosshairState, int pass) {

  double x = dataset.getXValue(series, item);
  double y = dataset.getYValue(series, item);

  double dx = 0.0;
  double dy = 0.0;

  if (dataset instanceof VectorXYDataset) {
      dx = ((VectorXYDataset)dataset).getVectorXValue(series, item);
      dy = ((VectorXYDataset)dataset).getVectorYValue(series, item);
  }

  double xx0 = domainAxis.valueToJava2D(x, dataArea, plot.getDomainAxisEdge());
  double yy0 = rangeAxis.valueToJava2D(y, dataArea, plot.getRangeAxisEdge());

  double xx1 = domainAxis.valueToJava2D(x + dx, dataArea, plot.getDomainAxisEdge());
  double yy1 = rangeAxis.valueToJava2D(y + dy, dataArea, plot.getRangeAxisEdge());

  Line2D line;
  PlotOrientation orientation = plot.getOrientation();

  if (orientation.equals(PlotOrientation.HORIZONTAL)) {
      line = new Line2D.Double(yy0, xx0, yy1, xx1);
  }
  else {
      line = new Line2D.Double(xx0, yy0, xx1, yy1);
  }

  boolean isSelected = false;

  if (SelectedRaster.isItemSelected(series, item)) {
      g2.setPaint(Color.WHITE);
      g2.setStroke(new BasicStroke(2.0f));
      isSelected = true;
  }
  else {
      g2.setPaint(getItemPaint(series, item));
      g2.setStorke(getItemStroke(series, item));
  }

  g2.draw(line);

  if (isSelected) {
      int itemCount = getItemCountOfSeries(series);
      for (int k=0; k<itemCount; k++){
           if (k != item){
               x = dataset.getXValue(series, k);
               y = dataset.getYValue(series, k);
               if (dataset instanceof VectorXYDataset) {
                    dx = ((VectorXYDataset)dataset).getVectorXValue(series, item);
                    dy = ((VectorXYDataset)dataset).getVectorYValue(series, item);
               }
               xx0 = domainAxis.valueToJava2D(x, dataArea, plot.getDomainAxisEdge());
               yy0 = rangeAxis.valueToJava2D(y, dataArea, plot.getRangeAxisEdge());
               xx1 = domainAxis.valueToJava2D(x + dx, dataArea, plot.getDomainAxisEdge());
               yy1 = rangeAxis.valueToJava2D(y + dy, dataArea, plot.getRangeAxisEdge());
               PlotOrientation nextOrient = plot.getOrientation();
               Line2D nextLine;
               if (nextOrient.equals(PlotOrientation.HORIZONTAL)) {
                   nextLine = new Line2D.Double(yy0, xx0, yy1, xx1);
               }
               else {
                     nextLine = new Line2D.Double(xx0, yy0, xx1, yy1);
               }
               g2.setPaint(Color.WHITE);
               g2.setStroke(new BasicStroke(2.0f));
               g2.draw(nextLine);
           }
      }
  }
}
The last IF condition does fire and does high-light/color the items in the series where item != 0.
But the color used is what I'm having an issue with.

Suggestions?

Fred

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: g2.setPaint issues

Post by paradoxoff » Fri Jan 27, 2017 1:09 pm

Fred wrote:When creating the VectorXYDataset when the data exceeds the length of the screen another point the first vector
is created from a start point and the delta in X runs to the end of that screen. It then wraps by starting at the beginning
of the next line and continues until the full length of the vector has been accounted for.
I didn´t really understand what you meant by these sentences.
What I have noted is that when SelectedRaster.isItemSelected(series, item) is indeed true, you are drawing all the items except the current one twice: once in white, once in the item paint.
If you want to draw all vector items of a series in white if SelectedRaster.isItemSelected(series, item) returns true for the first item (i. e. when item = 0), you need to change your logic.

Fred
Posts: 38
Joined: Sat Dec 12, 2015 4:57 pm
antibot: No, of course not.

Re: g2.setPaint issues

Post by Fred » Fri Jan 27, 2017 6:15 pm

Thanks Paradoxoff. Your comment about it paint twice gave me the food for thought.
I was trying to get the entire painting in 1 pass and forgot that VectorRenderer.drawItem
firing each time.

I've put in a better condition statement to check the SERIES and if that series has been
selected (via a mouse-clicked-event) then it will draw the correct color for each item in
the series.

Its working! Many Thanks!

Fred

Locked