I'm writing an application where I show markers on a map using the library Unfolding Maps.
The application lets the user choose a year and depending on that value other markers are shown. This works perfect, but when the user selects another year I can't seem to get the other markers shown.
I've tried to completely renew the map but after that first time it doesn't redraw a map.
Besides that I've tried to add a MarkerManager to the map but then I get errors of OpenGL.
setup function:
public void setup() {
/*
* Provincies, set zoom to 10
*/
size(1000, 800, OPENGL);
this.map = new UnfoldingMap(this, new Microsoft.RoadProvider());
map.zoomAndPanTo(belgie, 8);
map.setPanningRestriction(belgie, 1);
map.setZoomRange(8, 8);
drawOtherMarker();
//map.getMarkerManager(markerRemarck.get(keyM)).enableDrawing();
MapUtils.createDefaultEventDispatcher(this, map);
}
function that creates the MarkerManager and adds it to the map
private void createMarkers(ArrayList<double[]> dataMarker) {
float size = 0;
int i = 0;
ListIterator<double[]> dataIt = dataMarker.listIterator();
ArrayList<SimplePointMarker> markList = new ArrayList<SimplePointMarker>();
while (dataIt.hasNext()) {
double[] element = dataIt.next();
SimplePointMarker point = new SimplePointMarker(new Location(
element[0], element[1]));
if (element[2] > 1000)
size = 120;
else if (element[2] > 100)
size = 60;
else if (element[2] > 10)
size = 30;
else
size = 20;
point.setRadius(size);
point.setColor(color(64,64,64,100));
point.setStrokeColor(color(178,34,34));
point.setStrokeWeight(30);
point.setStrokeWeight(0);
point.setId(Integer.toString(i++));
markList.add(point);
}
MarkerManager man = new MarkerManager(markList);
map.addMarkerManager(man);
}
When the map is drawn and another set of markers must be drawn the following function is called:
public void drawOtherMarker(){
if(!markerRemarck.containsKey(keyM)){
markerRemarck.put(keyM, totalMark++);
createMarkers(dataMarkerProvince);
}
if(dataMarkerTown != null){
markerRemarck.put(keyM + "city", totalMark++);
createMarkers(dataMarkerTown);
}
for(int i = 0; i < totalMark;++i)
map.getMarkerManager(i).disableDrawing();
map.getMarkerManager(markerRemarck.get(keyM)).enableDrawing();
}
another function brings in the data required to make the markers. I just don't seem to manage to add a MarkerManager after the first draw.
EDIT
This map is placed inside a JPanel. And the years are choosen with the help of a JComboBox. Once a year is chosen the "dataMarker" variable containing the information for creating markers is updated
thanks in advance for helping me out.
Related
I work for my school project on MPAndroidChart especially a realtime graph, i would like to display Time of the value.
I set a IndexAxisValueFormatter with a "getFormattedValue", it work but refresh every label and not just the last, I try to have for each entry in my graph a XLabel who show the time, I really don't know how to do that you're help would be welcome.
Code for create an entry and display it :
void creaGraph() {
ArrayList<ILineDataSet> dataSets = new ArrayList<>();
if (boxCO2.isChecked()) {
A_CO2.add(new Entry(indice, listData.recup_data(indice - 1).getCO2()));
LineDataSet setCO2 = new LineDataSet(A_CO2, "CO2");
setCO2.setAxisDependency(YAxis.AxisDependency.LEFT);
paramSet(setCO2);
setCO2.setColor(Color.RED);
setCO2.setCircleColor(Color.RED);
dataSets.add(setCO2);
}
LineData data = new LineData(dataSets);
graph.setData(data);
data.notifyDataChanged();
graph.notifyDataSetChanged();
graph.invalidate();
}
The override of getFormattedValue
#Override
public String getFormattedValue(float value) {
return listData.recup_data(GraphPage.indice - 1).getTemps();
}
And a picture of my issue
Every label are refresh when a new entry come
Also, I see after the 7th values entry no longer have a time values
You never use value in getFormattedValue. The string you construct there should be based on value or it will show the same thing for every axis entry.
Something like this:
#Override
public String getFormattedValue(float value) {
return makeDateStringAt(value);
}
For example, if you have a chart with axis values at 0, 1, 2, 3 then getFormattedValue will be called 4 times with 0f, 1f, 1f, and 3f as its arguments and you should use those inputs to create the appropriate string to show at those positions on the axis.
I have a game template which has two classes as its levels.
The game has a player, enemies, treasure chests, and walls.
When I run the program, it loads Level 1, successfully. I have designed the code so if a certain condition is met, Level 2 must load. Now, Level 2 loads. But certain elements from Level 1 remain on screen, ignoring certain elements from Level 2.
To be exact:
From Level 2, it loads the player's initial coordinates, the treasure chests, and the final destination, ignoring the enemies and the walls.
It brings with itself from Level 1 to Level 2: the walls, the treasure chests, and the enemies. The latter two are in a frozen state. They don't function.
//Instance variables:
private int lvlCount = 1;
// Game() method
public Game(Stage stage) {
this.score = new SimpleIntegerProperty(0);
this.mobs = new ArrayList<>();
this.hitBoxes = new ArrayList<>();
this.treasure = new ArrayList<>();
this.newLevel = new ArrayList<>();
this.enemyCount = 0;
bg();
loadLevel(1);
living();
stuff();
mob2();
finalText();
stage.setOnShown(e -> this.run());
}
//The switch for loading the Levels:
void loadLevel(int in) {
switch (in) {
case 1:
this.level = new Level1();
break;
case 2:
this.level = new Level2(); // placeholder for second level
break;
}
hitBoxes.clear(); // clear the list of hitboxes
mobs.clear(); // remove any existing old mobs
this.getChildren().addAll(hitBoxes); // add all the hitboxes for the walls to the scene
background.setImage(level.getImage()); // get the background image
hitBoxes.addAll(level.getWalls()); // get all the wall hitboxes
enemyCount = level.getEnemyCount(); // get the enemy count from the level
chestCount = level.treasureCount(); // get the treasure count from the level
this.initTreasure(chestCount); // add the treasure chests for the level
this.initMobs(enemyCount); // initialize our mobs
lvlCount = level.lvlCount();
this.initLevel(lvlCount);
}
//The collision check:
private void collisionCheck() {if (r.getFill().equals(Color.WHITE)){lvlCount--;}
//Main Game loop method:
private void play() {
AnimationTimer gameLoop = new AnimationTimer() {
public void handle(long arg0) {
for (int i = 0; i < mobs.size(); i++) {
MOB m = mobs.get(i);
if (m instanceof Enemy) {
((Enemy) m).moveCheck(arg0);
}
}
collisionCheck();
if (lvlCount == 0){
loadLevel(2);} //The condition that loads Level 2
gameLoop.start();
}
Please help me to figure out how I can load Level 2, successfully.
Please note that it does not produce any errors in the console. So, I am having a very hard time figuring out where the problem lies.
I only pasted the code that I thought is necessary to achieve my goal. Here you can see the full code, for veiwing purposes only, no download necessary:
http://paste.mooc.fi/955c6d30
Thank you very much in advance.
I would like to use column percentage sizing to force the table to take on the width of the parent.
This does not work when I hide column(s) by default because the setColumnPercentageSizing() method does not seem to exclude hidden columns and does not correctly calculate the width.
Is there an easy way to adjust this in my code?
Example:
public void example(){
createGlazedListsGridLayer();
autoResizeColumns();
nattable.configure();
}
public GlazedListsGridLayer createGlazedListsGridLayer(){
SortedList<T> sortedList = new SortedList<>(eventList, null);
this.bodyDataProvider = new ListDataProvider<>(sortedList,
columnPropertyAccessor);
this.bodyDataLayer = new DataLayer(this.bodyDataProvider);
ColumnHideShowLayer columnHideShowLayer = new
ColumnHideShowLayer(bodyDataLayer);
// In this example, hide the first column
columnHideShowLayer.hideColumnPositions(Lists.newArrayList(0));
this.bodyLayerStack = new DefaultBodyLayerStack(new
GlazedListsEventLayer<>(columnHideShowLayer, eventList));
//...etc
}
protected void autoResizeColumns() {
glazedListsGridLayer.getBodyDataLayer().setColumnPercentageSizing(true);
nattable.addConfiguration(new DefaultNatTableStyleConfiguration() {
{
cellPainter = new LineBorderDecorator(new TextPainter(false,
true, 5, true));
}
});
}
UPDATE
It's not ideal but this is the closest I could get to it
public void adjustColumnWidth() {
getBodyDataLayer().setColumnPercentageSizing(false);
// Avoid the first column since it's hidden
for (int x = 1; x <= numColumns; x++) {
getBodyDataLayer().setColumnWidthByPosition(x,
getParent().getSize().x / numColumns, true);
}
}
UPDATE 2
Here are a couple of different things I tried in various combinations. None of them seem to keep the column hidden after a table is dynamically populated with data.
protected void enableAutoResizeColumns() {
getBodyDataLayer().setColumnPercentageSizing(true);
getBodyDataLayer().setDefaultColumnWidthByPosition(0, 0);
getBodyDataLayer().setColumnWidthByPosition(0, 0);
getBodyDataLayer().setColumnWidthPercentageByPosition(0, 0);
getNatTable().addConfiguration(new
DefaultNatTableStyleConfiguration() {
{
cellPainter = new LineBorderDecorator(new TextPainter
(false, true, 5, true));
}
});
}
Currently there is no solution for that. The reason for this is that the column widths are calculated in the DataLayer. The ColumnHideShowLayer sits on top of it and simply hides columns. It doesn't communicate back to the DataLayer that something is hidden.
In the end the ColumnHideShowLayer would need to re-trigger percentage size calculation based on the hidden state. But there is currently no API for that.
Feel free to create an enhancement ticket and provide a patch if you have an idea how to solve it.
I'm a beginner. I'm making a kid's math game. All it has to do is add the 2 pictures given. The problem is, I don't know how the JTextField will know that the answer is correct. Here's the picture.
ImageIcon[] images = new ImageIcon[10];
images[0] = new ImageIcon("0.jpg");
images[1] = new ImageIcon("1.jpg");
images[2] = new ImageIcon("2.jpg");
images[3] = new ImageIcon("3.jpg");
images[4] = new ImageIcon("4.jpg");
images[5] = new ImageIcon("5.jpg");
images[6] = new ImageIcon("6.jpg");
images[7] = new ImageIcon("7.jpg");
images[8] = new ImageIcon("8.jpg");
images[9] = new ImageIcon("9.jpg");
ImageIcon[] image = new ImageIcon[10];
image[0] = new ImageIcon("0.jpg");
image[1] = new ImageIcon("1.jpg");
image[2] = new ImageIcon("2.jpg");
image[3] = new ImageIcon("3.jpg");
image[4] = new ImageIcon("4.jpg");
image[5] = new ImageIcon("5.jpg");
image[6] = new ImageIcon("6.jpg");
image[7] = new ImageIcon("7.jpg");
image[8] = new ImageIcon("8.jpg");
image[9] = new ImageIcon("9.jpg");
int image_number = (int) (Math.random() * 10);
int image_number1 = (int) (Math.random() * 10);
Pic1.setIcon(images[image_number]);
Pic2.setIcon(image[image_number1]);
I'm not sure if I understood your question correctly, but you can store the value of a pic in some variable in Image.
It's been some time since I've coded in java, but I remember there are fields to store data.
After which all the JTextField has to do, is get the data associated with the 2 images currently being shown and see if the answer is correct or not.
Another good option, can be to name the images equal to their value i.e. an image named "7.png" is actually a diagram showing 7, that way all you have to do, is figure out what 2 images are being shown, parse their name and check for accuracy with the user input.
Update
Just did some quick research - http://docs.oracle.com/javase/7/docs/api/javax/swing/ImageIcon.html
ImageIcon contains a field description, you can simply set that field when creating instances of it & retrieve the one currently rendered.
ImageIcon[] images = new ImageIcon[10];
images[0] = new ImageIcon("0.jpg");
images[0].setDescription("0");
//Other code
then somewhere at some other place, click of a button or keypress of JTextField, it can be retrieved like :
Integer x=Integer.parseInt(Pic1.getIcon().getDescription());
One thing you might consider doing is creating a wrapper class around the image, which not only maintains a reference to the image, but the value of the image
public class NumberImage {
private Image image;
private int value;
public NumberImage(int value, Image image) {
this.value = value;
this.image = image;
}
public int getValue() {
return value;
}
public Image getImage() {
return image;
}
}
This way, you can calculate the answer by simply adding the two values properties together within your program
The other thing you already have is the two indexes of the images to be displayed, wch is the same numerical value of the image to be displayed...
That is, based on your example, image_number == 7 and image_number1 == 5, therefore, you already have the information you need to calculate the answer ;)
I have create a table with bars which shows frequency of the words in a text.I show the number of special word which user click on them or frequency of whole words in the text. I fetch my list of list and send it to the fill table function. All thing is OK but when I select a special word and then click to show whole words I get indexoutofbounds exception. I guess it is because I change my datasource. It is really strange but simple. However, I could not solve it.
public void fill_count_table(List<RootWordSet> source){
final List<RootWordSet> mysource=source;
if(source!=null){
for(int i=0;i<source.size();i++){
TableItem ti=new TableItem(count_table, SWT.NONE);
ti.setText(source.get(i).getRoot());
}
count_table.addListener(SWT.PaintItem, new Listener() {
public void handleEvent(Event event) {
if (event.index == 1) {
try{
GC gc = event.gc;
TableItem item = (TableItem)event.item;
int index = count_table.indexOf(item);
System.out.println(mysource.size());
int percent = mysource.get(index).getWordNumber();
org.eclipse.swt.graphics.Color foreground = gc.getForeground();
org.eclipse.swt.graphics.Color background = gc.getBackground();
gc.setForeground(Display_1.getSystemColor(SWT.COLOR_BLUE));
gc.setBackground(Display_1.getSystemColor(SWT.COLOR_DARK_BLUE));
int width = (tc2.getWidth() - 1) * percent / 100;
gc.fillGradientRectangle(event.x, event.y, width, event.height, true);
Rectangle rect2 = new Rectangle(event.x, event.y, width-1, event.height-1);
gc.drawRectangle(rect2);
gc.setForeground(Display_1.getSystemColor(SWT.COLOR_RED));
String text = Integer.toString(percent) ;
Point size = event.gc.textExtent(text);
int offset = Math.max(0, (event.height - size.y) / 2);
gc.drawText(text, event.x+2, event.y+offset, true);
gc.setForeground(background);
gc.setBackground(foreground);
}
catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
});
}
else{
count_table.removeAll();
count_table.redraw();
}
}
this is the line that make error: int percent = mysource.get(index).getWordNumber();
I really do not know what happens when I change datasources. when I shift to each other it stuck. Even I put a println to check the size of datasource but it was quite strange it had two size. One belong to former datasource and one belong to newwr. Anyway If I remove this graphic part table fill correctly. What do you think?
change this line:
int percent = mysource.get(index).getWordNumber();
to
int percent = mysource.get(index-1).getWordNumber();