Resizing Jtable column widths wont work - resize

//can set column widths using percentages
private void setPreferredTableColumnWidths(JTable table, double[] percentages)
{
Dimension tableDim = table.getSize();
double total = 0;
for(int i = 0; i < table.getColumnModel().getColumnCount(); i++)
total += percentages[i];
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
for(int i = 0; i < table.getColumnModel().getColumnCount(); i++)
{
TableColumn column = table.getColumnModel().getColumn(i);
column.setPreferredWidth((int) (tableDim.width * (percentages[i] / total)));
}
}
I want to be able to change the widths of a JTable based on a percentage of the total width of the table. The above code looks like it should work, but I only get equal width columns when I call it using "setPreferredTableColumnWidths(table, new double[] {.01,.4,.2,.2,.2});"
What could be the problem?

Try to add the line:
table.doLayout();
At the end of the method.

Your code works fine but depending on where you call it the table may or may not have a width. In the constructor of a Panel (or whatever contains the table) there is not width yet.
I put the call to your method in the paint() method. But be sure to keep track and only call it once or it will resize the columns over and over.
#Override
public void paint(Graphics g) {
super.paint(g);
if(! this.initialSizeSet){
this.setPreferredTableColumnWidths(this.table, new double[] {0.1, 0.1, 0.5, 0.3});
this.initialSizeSet = true;
}
}

Related

MPAndroidChart circles and numbers only for first and last points of a dataset

I am trying to generate a plot with MPAndroidChart. Specifically, a function which mainly depends on logarithms. For that reason I created the following for loop of data point entries (I am pretty sure this is not the best approach, but I decided to go for it)
yValues.add(new Entry(value3,value1));
float PointData = 100.0f;
for (int i=1 ; i<PointData ; i++){
yValues.add( new Entry( value3+(i/PointData)*(value4-value3),value1+(value2-value1) * Float.parseFloat(String.valueOf(Math.log(value3+(i/PointData)*(value4-value3)/value3)/Math.log(value4/value3))) ) );
}
yValues.add(new Entry(value4,value2));
My problem is that I have to set
set1.setDrawCircles(false);
set1.setDrawValues(false);
otherwise my plot will be filled with circles and numbers. I would like though to be able to draw these two (circles and numbers) for just the first (defined by "yValues.add(new Entry(value3,value1));") and last (defined by "yValues.add(new Entry(value4,value2));") data set points. I spent a considerable amount of time but with no luck unfortunately. As a result, I would really appreciate if someone can help me towards solving this issue. I have also attached the whole java file in case someone wants to have a look at the code.
public class cyl_1plane_tempplot extends AppCompatActivity {
LineChart mChart;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cyl_1plane_tempplot);
Intent localintent = getIntent();
float value1 = localintent.getFloatExtra("key1", -1);
float value2 = localintent.getFloatExtra("key2", -1);
float value3 = localintent.getFloatExtra("key4", -1);
float value4 = localintent.getFloatExtra("key5", -1);
mChart = findViewById(R.id.cond_cyl_1layer_plot);
mChart.setDragEnabled(true);
mChart.setScaleEnabled(false);
mChart.getDescription().setEnabled(false);
mChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTH_SIDED);
float graph_excess_X = 10f;
XAxis bottomAxis = mChart.getXAxis();
bottomAxis.setAxisMinimum(value3 - (value4 - value3)/graph_excess_X);
bottomAxis.setAxisMaximum(value4 + (value4 - value3)/graph_excess_X);
ArrayList<Entry> yValues = new ArrayList<>();
yValues.add(new Entry(value3,value1));
float PointData = 100.0f;
for (int i=1 ; i<PointData ; i++){
yValues.add( new Entry( value3+(i/PointData)*(value4-value3),value1+(value2-value1) * Float.parseFloat(String.valueOf(Math.log(value3+(i/PointData)*(value4-value3)/value3)/Math.log(value4/value3))) ) );
}
yValues.add(new Entry(value4,value2));
ArrayList<ILineDataSet> lineDataSets = new ArrayList<>();
LineDataSet set1 = new LineDataSet(yValues, "Title");
set1.setFillAlpha(110);
int color = getResources().getColor(R.color.blue_chart_color);
set1.setColor(color);
set1.setDrawCircles(false);
set1.setDrawValues(false);
set1.setLineWidth(3f);
lineDataSets.add(set1);
LineData data = new LineData(lineDataSets);
mChart.setData(data);
}
}
Thank you all in advance.
Oh buddy this can be done please follow instructions below:
Design an icon which you want to display instead of circle on first and last value.
Place that icon in drawable folder of you project.
Set draw values and circles to false. This will hide circles and values for every entry.
set1.setDrawCircles(false);
set1.setDrawValues(false);
Get your specific value on which you want to display your icon as:
set1.getEntryForIndex(index).setIcon(getDrawable(R.drawable.icon));
In above line replace index with your entry index for example for first entry use 0. Also replace icon with your icon name in drawables.

Sprite Smooth movement and facing position according to movement

i'm trying to make this interaction with keyboard for movement using some sprites and i got stuck with two situations.
1) The character movement is not going acording to the animation itself (it only begin moving after one second or so while it's already being animated). What i really want it to do is, to move without a "initial acceleration feeling" that i get because of this problem
2) I can't think of a way to make the character face the position it should be facing when the key is released. I'll post the code here, but since it need images to work correctly and is not so small i made a skecth available at this link if you want to check it out: https://www.openprocessing.org/sketch/439572
PImage[] reverseRun = new PImage [16];
PImage[] zeroArray = new PImage [16];
void setup(){
size(800,600);
//Right Facing
for(int i = 0; i < zeroArray.length; i++){
zeroArray[i] = loadImage (i + ".png");
zeroArray[i].resize(155,155);
}
//Left Facing
for( int z = 0; z < reverseRun.length; z++){
reverseRun[z] = loadImage ( "mirror" + z + ".png");
reverseRun[z].resize(155,155);
}
}
void draw(){
frameRate(15);
background(255);
imageMode(CENTER);
if(x > width+10){
x = 0;
} else if (x < - 10){
x = width;}
if (i >= zeroArray.length){
i = 3;} //looping to generate constant motiion
if ( z >= reverseRun.length){
z = 3;} //looping to generate constant motiion
if (isRight) {
image(zeroArray[i], x, 300);
i++;
} //going through the images at the array
else if (isLeft) {
image(reverseRun[z],x,300);
z++;
} going through the images at the array
else if(!isRight){
image(zeroArray[i], x, 300);
i = 0; } //"stoped" sprite
}
}
//movement
float x = 300;
float y = 300;
float i = 0;
float z = 0;
float speed = 25;
boolean isLeft, isRight, isUp, isDown;
void keyPressed() {
setMove(keyCode, true);
if (isLeft ){
x -= speed;
}
if(isRight){
x += speed;
}
}
void keyReleased() {
setMove(keyCode, false);
}
boolean setMove(int k, boolean b) {
switch (k) {
case UP:
return isUp = b;
case DOWN:
return isDown = b;
case LEFT:
return isLeft = b;
case RIGHT:
return isRight = b;
default:
return b; }
}
The movement problem is caused by your operating system setting a delay between key presses. Try this out by going to a text editor and holding down a key. You'll notice that a character shows up immediately, followed by a delay, followed by the character repeating until you release the key.
That delay is also happening between calls to the keyPressed() function. And since you're moving the character (by modifying the x variable) inside the keyPressed() function, you're seeing a delay in the movement.
The solution to this problem is to check which key is pressed instead of relying solely on the keyPressed() function. You could use the keyCode variable inside the draw() function, or you could keep track of which key is pressed using a set of boolean variables.
Note that you're actually already doing that with the isLeft and isRight variables. But you're only checking them in the keyPressed() function, which defeats the purpose of them because of the problem I outlined above.
In other words, move this block from the keyPressed() function so it's inside the draw() function instead:
if (isLeft ){
x -= speed;
}
if(isRight){
x += speed;
}
As for knowing which way to face when the character is not moving, you could do that using another boolean value that keeps track of which direction you're facing.
Side note: you should really try to properly indent your code, as right now it's pretty hard to read.
Shameless self-promotion: I wrote a tutorial on user input in Processing available here.

array lists in java: exclude the first element from `for` loop

I am taking an introduction to Java programing class and I have an array list where I need to exclude the first element from my for loop that finds an average. The first element in the array list is a weight for the average (which is why it needs to be excluded). I also need to drop the lowest value from the remainder of the array list hence my second for loop. I have tried to create a copy of the list and also tried to create a sub list but I cannot get it to work.
public static double Avgerage(ArrayList<Double> inputValues) {
double avg;
double sum = 0;
double weightValue = inputValues.get(0);
double lowest = inputValues.get(0);
for (int i = 1; i > inputValues.size(); i++) {
if (inputValues.get(i) < lowest) {
lowest = inputValues.get(i);
}
}
for (int i = 0; i < inputValues.size(); i++) {
sum = sum + inputValues.get(i);
}
double average = (sum - lowest) / (inputValues.size() - 1);
avg = average * weightValue;
return avg;
}
To start with good programming practice, you should work with interfaces rather than classes, where possible. The appropriate interface here is List<Double>, and when you create it in your class, you should use
List<Double> nameOfList = new ArrayList<Double>();
What we're doing is creating an object which has the behaviour of a List, with the underlying implementation of an ArrayList (more info here.
With regards to the question, you don't appear to be excluding the first element, as you said you wished to - both for loops iterate through all values in the list. Remember to treat the ArrayList like an array - accessing an element does not modify it, like it might in a Queue.
I have edited your code below to demonstrate this, and have also included some other optimisations and corrected the sign error on line 7:
public static double average(List<Double> inputValues) {
double sum = 0;
//Exclude the first element, as it contains the weight
double lowest = inputValues.get(1);
for (int i = 2; i < inputValues.size(); i++) {
lowest = Math.min(inputValues.get(i), lowest);
}
for (int i = 1; i < inputValues.size(); i++) {
sum += inputValues.get(i);
}
double average = (sum - lowest) / (inputValues.size() - 1);
//Scale by the weight
avg *= inputValues.get(0);
return avg;
}
Note: The convention in java is to use camelCase for method names, I have adjusted accordingly.
Also, I don't know your requirements, but optimally, you should be providing logical parameters. If possible do the following before calling the function:
int weight = inputValues.get(0);
inputValues.remove(0);
//And then you would call like this, and update your method signature to match
average(inputValues, weight);
I don't do this inside the method, as the context implies that we would not be modifying values.

Rescale X across all plots in ZedGraph

I can successfully zoom x-only across all plots using the following code:
zg1.IsEnableHZoom = true;
zg1.IsEnableVZoom = false;
zg1.IsSynchronizeXAxes = true;
foreach (GraphPane gp in zg1.MasterPane.paneList)
{
> //What code can I put here?
}
My problem is that using this code, the Y-axis remains at a max and min based on the original view of the data. I want the Y-axis to autoscale so that the max and min is ONLY based on the data visible due to the x-axis zoom (per graph pane, of course). Is there some command, or brute force method, that I can use on each of the graph panes in the for loop shown above? Thanks ahead of time for anyone's help.
You can use this in the loop (assuming X Axis scale MinAuto and MaxAuto are false)
foreach (GraphPane gp in zg1.MasterPane.paneList)
{
gp.YAxis.Scale.MinAuto = true;
gp.YAxis.Scale.MaxAuto = true;
// This will force ZedGraph to calculate the Min and the Max of the Y axis
// based on the X axis visible range
gp.IsBoundedRanges = true;
}
zg1.MasterPane.AxisChange();
I had the same problem before and could not find a way other than to inspect all the curve points.
I added an event handler to the Paint event to do this, I'm sure there are ways this can be optimized.
Something like this:
private void graph_Paint(object sender, PaintEventArgs e)
{
double min = Double.MaxValue;
double max = Double.MinValue;
CurveItem curve = graph.GraphPane.CurveList[0];
for (int i = 0; i < curve.Points.Count; i++)
{
if (curve.Points[i].X > graph.GraphPane.XAxis.Scale.Min &&
curve.Points[i].X < graph.GraphPane.XAxis.Scale.Max)
{
min = Math.Min(curve.Points[i].Y, min);
max = Math.Max(curve.Points[i].Y, max);
}
}
if (min != Double.MaxValue)
{
graph.GraphPane.XAxis.Scale.Min = min;
graph.GraphPane.XAxis.Scale.Max = max;
}
}

Kinect background removal

I followed the code provided by Robert Levy at this link: http://channel9.msdn.com/coding4fun/kinect/Display-Kinect-color-image-containing-only-players-aka-background-removal
I tried implementing it into my existing code, and have had inconsistent results. If the user is in the kinect's field of view when the program starts up it will remove the background some of the time. If the user walks into the field of view it will not pick them up.
namespace KinectUserRecognition
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
//Kinect Runtime
Runtime kinect = Runtime.Kinects[0];
PlanarImage colorImage;
PlanarImage depthImage;
bool isDepthImage;
WriteableBitmap player1;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
isDepthImage = false;
//UseDepthAndPlayerIndex and UseSkeletalTracking
kinect.Initialize(RuntimeOptions.UseDepthAndPlayerIndex | RuntimeOptions.UseColor);// | RuntimeOptions.UseSkeletalTracking);
//register for event
kinect.VideoFrameReady += new EventHandler<ImageFrameReadyEventArgs>(nui_VideoFrameReady);
kinect.DepthFrameReady += new EventHandler<ImageFrameReadyEventArgs>(nui_DepthFrameReady);
//Video image type
kinect.VideoStream.Open(ImageStreamType.Video, 2, ImageResolution.Resolution640x480,
ImageType.Color);
//DepthAndPlayerIndex ImageType
kinect.DepthStream.Open(ImageStreamType.Depth, 2, ImageResolution.Resolution320x240,
ImageType.DepthAndPlayerIndex);
}
void nui_VideoFrameReady(object sender, ImageFrameReadyEventArgs e)
{
colorImage = e.ImageFrame.Image;
image1.Source = BitmapSource.Create(colorImage.Width, colorImage.Height, 96, 96,
PixelFormats.Bgr32, null, colorImage.Bits, colorImage.Width * colorImage.BytesPerPixel);
if (isDepthImage)
{
player1 = GeneratePlayerImage(e.ImageFrame, 1);
image3.Source = player1;
}
}
void nui_DepthFrameReady(object sender, ImageFrameReadyEventArgs e)
{
//Convert depth information for a pixel into color information
byte[] ColoredBytes = GenerateColoredBytes(e.ImageFrame);
depthImage = e.ImageFrame.Image;
image2.Source = BitmapSource.Create(depthImage.Width, depthImage.Height, 96, 96, PixelFormats.Bgr32, null,
ColoredBytes, depthImage.Width * PixelFormats.Bgr32.BitsPerPixel / 8);
isDepthImage = true;
}
private WriteableBitmap GeneratePlayerImage(ImageFrame imageFrame, int playerIndex)
{
int depthWidth = kinect.DepthStream.Width;
int depthHeight = kinect.DepthStream.Height;
WriteableBitmap target = new WriteableBitmap(depthWidth, depthHeight, 96, 96, PixelFormats.Bgra32, null);
var depthRect = new System.Windows.Int32Rect(0, 0, depthWidth, depthHeight);
byte[] color = imageFrame.Image.Bits;
byte[] output = new byte[depthWidth * depthHeight * 4];
//loop over each pixel in the depth image
int outputIndex = 0;
for (int depthY = 0, depthIndex = 0; depthY < depthHeight; depthY++)
{
for(int depthX = 0; depthX < depthWidth; depthX++, depthIndex +=2)
{
short depthValue = (short)(depthImage.Bits[depthIndex] | (depthImage.Bits[depthIndex + 1] << 8));
int colorX, colorY;
kinect.NuiCamera.GetColorPixelCoordinatesFromDepthPixel(
imageFrame.Resolution,
imageFrame.ViewArea,
depthX, depthY, //depth coordinate
depthValue, //depth value
out colorX, out colorY); //color coordinate
//ensure that the calculate color location is within the bounds of the image
colorX = Math.Max(0, Math.Min(colorX, imageFrame.Image.Width - 1));
colorY = Math.Max(0, Math.Min(colorY, imageFrame.Image.Height - 1));
output[outputIndex++] = color[(4 * (colorX + (colorY * imageFrame.Image.Width))) + 0];
output[outputIndex++] = color[(4 * (colorX + (colorY * imageFrame.Image.Width))) + 1];
output[outputIndex++] = color[(4 * (colorX + (colorY * imageFrame.Image.Width))) + 2];
output[outputIndex++] = GetPlayerIndex(depthImage.Bits[depthIndex]) == playerIndex ? (byte)255 : (byte)0;
}
}
target.WritePixels(depthRect, output, depthWidth * PixelFormats.Bgra32.BitsPerPixel / 8, 0);
return target;
//return output;
}
private static int GetPlayerIndex(byte firstFrame)
{
//returns 0 = no player, 1 = 1st player, 2 = 2nd player...
//bitwise & on firstFrame
return (int)firstFrame & 7;
}
}
}
-Edit 1-
I think I've narrowed the problem down, but I'm not sure of a way to resolve it. I assumed that having only one person in the kinect's field of view would return a value of one from my "GetPlayerIndex" method. This is not the case. I was hoping to produce a separate image for each person with the background removed. What type of values should I assume to receive from:
-Edit 2-
From my tests I've noticed that I can a max value of 6 for the player index, but the index that I get isn't consistent. If there a way to know what player index will be assigned to a skeleton? For example, if I were the only person in the fov would there be a way to know that my player index would always be 1?
The player index is not guaranteed to be anything. Once it catches a skeleton, the index will stay the same for that skeleton until it loses sight of it, but you can't assume that the first player will be 1, the second 2, etc.
What you'll need to do is determine a valid skeleton index prior to the player1 = GeneratePlayerImage(e.ImageFrame, 1); call, or alter the GeneratePlayerImage function to find an index. If you're only interested in removing the background and leaving the pixels for all the people in the frame untouched, just change this:
output[outputIndex++] = GetPlayerIndex(depthImage.Bits[depthIndex]) == playerIndex ? (byte)255 : (byte)0;
to this, which will just check for ANY player, instead of a specific player:
output[outputIndex++] = GetPlayerIndex(depthImage.Bits[depthIndex]) != 0 ? (byte)255 : (byte)0;
The other two ways I can think of to do this for a specific player instead of all players:
Open the Kinect's Skeleton feed, and loop through the array of skeletons it gives you to find a valid index. Create a global integer to hold this index, then call the GeneratePlayerImage method with this global integer.
Change the GeneratePlayerImage method to check for a player index in each pixel, and if one is found use that index to remove the background for the entire image (ignore any other index it finds).