I'm using PrimeFaces, in order to build a BarChart. My goal is to compare some Testprojects to each other. Each Bar should be a Testproject from which you can read off the different Phases in which the project is situated.
My question is: I have a View where I can add new Testprojects. When I add one, there is a new Bar. But If I want to add another one, the old Bar is owerwritten. Of course It is, because of set... but I would like to do something like add(testprojectProducer.getTestproject().getName(), 1).
The chart schould be able to remembe the old bar and add the new one. Is there a way or do I have to say goodby to primefaces and build my own chart?
Here is my code:
#ManagedBean
public class ChartView implements Serializable {
private static final long serialVersionUID = 9181815723688700642L;
private BarChartModel barModel;
#Inject
private TestprojectProducer testprojectProducer;
#PostConstruct
public void init() {
createBarModel();
new ArrayList<String>();
}
private BarChartModel initBarModel() {
BarChartModel model = new BarChartModel(); //Diagram
ChartSeries cs= new ChartSeries(); // Balken
cs.setLabel("Testprojekte");
cs.set("Testprojekt 1",5);
cs.set(testprojectProducer.getTestproject().getName(), 1);
model.addSeries(cs);
return model;
}
public void createBarModel() {
barModel =initBarModel();
barModel.setTitle("");
barModel.setLegendPosition("");
barModel.setSeriesColors("C80000");
Axis xAxis = barModel.getAxis(AxisType.X);
xAxis.setLabel("Testprojekte");
Axis yAxis = barModel.getAxis(AxisType.Y);
yAxis.setLabel("Phasen");
yAxis.setMin(0);
yAxis.setMax(7);
}
public BarChartModel getBarModel() {
return barModel;
}
public void setBarChartModel(BarChartModel barModel){
this.barModel= barModel;
}
}
I hope there is a solution to my problem. Thank you!
I resolved my problem. When you use 'HashMap', then It's not that hard:
private BarChartModel initBarModel() {
BarChartModel model = new BarChartModel();
ChartSeries cc = new ChartSeries();
cc.setLabel("Testprojekte");
HashMap<Object, Number> groupData = new HashMap<Object, Number>();
for (int i = 0; i < 3; i++) {
groupData.put(testProjectListProducer.getList().get(i).getName(), 2);
}
cc.setData(groupData);
model.addSeries(cc);
return model;
}
Related
Update:
WorldConfiguration and World are still the same,
WorldConfiguration setup = new WorldConfigurationBuilder()
.with(new HelloSystemArtemis())
.with(new RenderSystem(batch, environment))
.with(new BulletSystem(this))
.with(new PlayerSystem(perspectiveCamera, batch, gameUi, this))
.with(new AthmosphereSystem(modelComponent,
bulletComponent))
.with(new MovementSystem(this))
.with(new StatusSystem(this))
.with(new EnemySystem(this))
.build();
World world = new World(setup);
world.setDelta(delta);
int entityId = world.create();
world.edit(entityId).create(HelloComponentArtemis.class).message = "\n\rHello Oxyddians!\n\r";
Decided to post full code for clarity reason, in fact can't explain from where the problem is coming, even if I got errors that point me to
#All({ModelComponentArtemis.class, ModelComponentArtemis.class})
public class AthmosphereSystem extends BaseSystem {
private static OxyddiA game;
private static Assets assets = new Assets();
private ComponentMapper<ModelComponentArtemis> mc;
private ComponentMapper<BulletComponentArtemis> bc;
public AthmosphereSystem(ComponentMapper<ModelComponentArtemis> mc,
ComponentMapper<BulletComponentArtemis> bc) {
this.mc = mc;
this.bc = bc;
}
#Override
protected void processSystem() {
int entity = world.create();
ModelComponentArtemis mc = new ModelComponentArtemis(assets.Athmosphere,0,0,0);
mc.create(entity); //Not working
BulletComponentArtemis bc = new BulletComponentArtemis();
btCollisionShape shape = Bullet.obtainStaticNodeShape(assets.Athmosphere.nodes);
bc.bodyInfo = new btRigidBody.btRigidBodyConstructionInfo(0, null, shape, Vector3.Zero);
bc.body = new btRigidBody(bc.bodyInfo);
bc.body.userData = entity;
bc.motionState = new MotionState(mc.instance.transform);
((btRigidBody) bc.body).setMotionState(bc.motionState);
bc.create(entity); //Not working
return entity; //Not working as well
}
}
ModelComponent used in this approach
public class ModelComponentArtemis extends Component {
public Model model;
public ModelInstance instance;
public ModelComponentArtemis(Model model, float x, float y, float z)
{
this.model = model;
this.instance = new ModelInstance(model, new Matrix4().setToTranslation(x, y, z));
}
public void reset() {
}
}
BulletComponent used in this approach
public class BulletComponentArtemis extends Component {
public MotionState motionState;
public btRigidBody.btRigidBodyConstructionInfo bodyInfo;
public btCollisionObject body;
public Model model;
public void init() {
ModelComponentArtemis modelComponent = new ModelComponentArtemis(model,0,0,0);
BulletComponentArtemis bulletComponent = new BulletComponentArtemis();
btCollisionShape shape = Bullet.obtainStaticNodeShape(model.nodes);
bulletComponent.bodyInfo = new btRigidBody.btRigidBodyConstructionInfo(0, null, shape, Vector3.Zero);
bulletComponent.body = new btRigidBody(bulletComponent.bodyInfo);
Object bCuD = null;
bulletComponent.body.userData = bCuD;
bulletComponent.motionState = new MotionState(modelComponent.instance.transform);
((btRigidBody) bulletComponent.body).setMotionState(bulletComponent.motionState);
}
public void reset() {
}
}
And of course the BulletSystem used in this project
#All(ModelComponentArtemis.class)
public class BulletSystem extends EntitySystem
{
public Entity e;
public final btCollisionConfiguration collisionConfiguration;
public final btCollisionDispatcher dispatcher;
public final btBroadphaseInterface broadphase;
public final btConstraintSolver solver;
public final btDiscreteDynamicsWorld collisionWorld;
public GameWorld gameWorld;
public PreGameWorld preGameWorld;
private final btGhostPairCallback ghostPairCallback;
public final int maxSubSteps = 5;
public final float fixedTimeStep = 0.2f / 60f;
private EntityContactListener myContactListener;
public BulletSystem(GameWorld gameWorld)
{
super(Aspect.all(ModelComponentArtemis.class));
this.gameWorld = gameWorld;
myContactListener = new EntityContactListener();
myContactListener.enable();
collisionConfiguration = new btDefaultCollisionConfiguration();
dispatcher = new btCollisionDispatcher(collisionConfiguration);
broadphase = new btAxisSweep3(new Vector3(-1000, -1000, -1000), new Vector3(1000, 1000, 1000));
solver = new btSequentialImpulseConstraintSolver();
collisionWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
ghostPairCallback = new btGhostPairCallback();
broadphase.getOverlappingPairCache().setInternalGhostPairCallback(ghostPairCallback);
this.collisionWorld.setGravity(new Vector3(0f, -0.5f, 0f));
}
public BulletSystem(PreGameWorld preGameWorld)
{
this.preGameWorld = preGameWorld;
myContactListener = new EntityContactListener();
myContactListener.enable();
collisionConfiguration = new btDefaultCollisionConfiguration();
dispatcher = new btCollisionDispatcher(collisionConfiguration);
broadphase = new btAxisSweep3(new Vector3(-1000, -1000, -1000), new Vector3(1000, 1000, 1000));
solver = new btSequentialImpulseConstraintSolver();
collisionWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
ghostPairCallback = new btGhostPairCallback();
broadphase.getOverlappingPairCache().setInternalGhostPairCallback(ghostPairCallback);
this.collisionWorld.setGravity(new Vector3(0f, -0.5f, 0f));
}
public void update(float deltaTime)
{
collisionWorld.stepSimulation(deltaTime, maxSubSteps, fixedTimeStep);
}
protected void processSystem(int entityId) {
this.world.getEntity(entityId).getComponent(BulletComponentArtemis.class);
}
#Override
protected void processSystem() {
}
public void dispose()
{
collisionWorld.dispose();
if (solver != null) solver.dispose();
if (broadphase != null) broadphase.dispose();
if (dispatcher != null) dispatcher.dispose();
if (collisionConfiguration != null)
collisionConfiguration.dispose();
ghostPairCallback.dispose();
}
}
With all that in mind, I know I just can't find any solution,
Maybe it will be clearer like that for anyone wishing to help
Thanks anyawy.
Couple of things to note:
you don't need to inject entity instances
working with Entity and EntityEdit is slower than working with int and ComponentMapper/Archetype/EntityTransmuter, but fine for the beginning.
What you probably want to do is creating an AtmosphereSystem (extending BaseSystem) and create the entity within the initialize method or offer a public method (non-static).
If you need to call those entity factory methods, just inject the AtmosphereSystem (or anything extending BaseSystem) by adding a field "AtmosphereSystem atmosphereSytem;" inside any other BaseSystem.
See this gist for a similar use case (factory methods for creating entities).
Hi I am trying to plot linegraph using primefaces. But I am getting the graph for 1st quadrant but I have data for 4th quadrant as well.
My code is:
Bean class:
private LineChartModel lineModel1;
#PostConstruct
public void init() {
createLineModels();
}
public LineChartModel getLineModel1() {
return lineModel1;
}
private void createLineModels() {
lineModel1 = initLinearModel();
lineModel1.setTitle("Linear Chart");
lineModel1.setLegendPosition("e");
Axis yAxis = lineModel1.getAxis(AxisType.Y);
yAxis.setMin(-10);
yAxis.setMax(10);
}
private LineChartModel initLinearModel() {
LineChartModel model = new LineChartModel();
LineChartSeries series1 = new LineChartSeries();
series1.setLabel("Series 1");
series1.set(1, -2);
series1.set(0, 1);
series1.set(1, 3);
model.addSeries(series1);
return model;
}
The output Graph looks like this:
Generated Graph
I am using Marker clusters in my application. I want to increase the grid size. I have customize the DefaultClusterRenderer class, but I didn't find any thing in it to increase the size.Please help me out how to increase it.
following is the code of customized clusterRenderer
public class MyClusterRenderer extends DefaultClusterRenderer<MyItem> {
public MyClusterRenderer(Context context, GoogleMap map, ClusterManager clusterManager) {
super(context, map, clusterManager);
}
#Override
protected boolean shouldRenderAsCluster(Cluster<MyItem> cluster) {
//start clustering if at least 2 items overlap
return cluster.getSize() > 4;
}
#Override
protected void onBeforeClusterItemRendered(MyItem item,MarkerOptions markerOptions){
if(item.isRegister()==true)
{
BitmapDescriptor markerDescriptor = BitmapDescriptorFactory.defaultMarker(340);
markerOptions.icon(markerDescriptor);
//markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.x));
}
else if(item.isRegister()==false)
{
BitmapDescriptor markerDescriptor = BitmapDescriptorFactory.defaultMarker(60);
markerOptions.icon(markerDescriptor).title("false");
}
}
}
A simple solution is use the NonHierarchicalDistanceBasedAlgorithm. Create a new calss and extends it with NonHierarchicalDistanceBasedAlgorithm, then override the method getClusters(double zoom). In this method you will be able to set the grid size, by changing the value of zoomSpecificSpan, In my i change the first value to 200.0D, and it works for me.
public Set<? extends Cluster<T>> getClusters(double zoom) {
int discreteZoom = (int)zoom;
double zoomSpecificSpan = 200.0D / Math.pow(2.0D, (double)discreteZoom) / 256.0D;
HashSet visitedCandidates = new HashSet();
HashSet results = new HashSet();
HashMap distanceToCluster = new HashMap();
HashMap itemToCluster = new HashMap();
PointQuadTree var10 = this.mQuadTree;
I've to JTables. I drag a row from the first table and drop it to the second.
The DnD operation works fine so far, but how can easily refresh the second table after
dropping operation? I've implemented a TableModelListener, but it works only when I
double click on a line of a table.
My question: which event listener do I need to solve my problem? Any solutions or examples?
btw: the DnD operation is performing with the tranferHandler
ok here's some code:
TableExample for creating the to tables
ListHandler1 for DnD operations
SearchRenderer for updating row heights after DnD or table changings
please keep in mind that i wrote quickly. ..
Thanks and regards!
TableExample:
package GUI_Examples;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.*;
public class TableExample implements TableModelListener{
String [] title = new String [] {"Title A", "Title B"};
String [] title2 = new String [] {"Title C", "Title D"};
Object [][] data = new String [][] {{"aaaaaaaaaaaa aaaaaa aaaaaaa", "bbbbbbbb bbbb bbbbbb bbbbbb"},
{"cccccccccc cccccccc ccccccc", "ddddddd ddd dddddddd dddddd"},
{"eeeeeeeeee eeeeeeee eeeeeee", "fffffff ffff ffffff fffffff"}};
Object [][] data2 = new String [][] {{"",""}};
private JTable table;
private JTable table2;
private JFrame frame;
private DefaultTableModel model;
private DefaultTableModel model2;
private JScrollPane pane1;
private JScrollPane pane2;
private SearchRenderer1 myRenderer;
private SearchRenderer1 myRenderer2;
TableExample() {} //constructor
public JPanel createTable() {
JPanel panel = new JPanel();
//creating tables and table models
model = new DefaultTableModel(data, title);
model2 = new DefaultTableModel(data2, title2);
table = new JTable(model);
table2 = new JTable(model2);
//setting renderers
myRenderer = new SearchRenderer1();
table.setDefaultRenderer(String.class, myRenderer);
table.getColumnModel().getColumn(0).setCellRenderer(myRenderer);
table.getColumnModel().getColumn(1).setCellRenderer(myRenderer);
myRenderer2 = new SearchRenderer1();
table2.setDefaultRenderer(String.class, myRenderer2);
table2.getColumnModel().getColumn(0).setCellRenderer(myRenderer2);
table2.getColumnModel().getColumn(1).setCellRenderer(myRenderer2);
//setting sizes
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.getColumnModel().getColumn(0).setPreferredWidth(60);
table.getColumnModel().getColumn(1).setPreferredWidth(60);
table2.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table2.getColumnModel().getColumn(0).setPreferredWidth(60);
table2.getColumnModel().getColumn(1).setPreferredWidth(60);
//Drag&Drop operations
table.setDragEnabled(true);
table2.setDropMode(DropMode.INSERT);
table2.setTransferHandler(new ListHandler1(model2));
pane1 = new JScrollPane(table);
pane2 = new JScrollPane(table2);
pane1.setPreferredSize(new Dimension(150,300));
pane2.setPreferredSize(new Dimension(150,300));
updateRowHeights();
updateRowHeights2();
panel.add(pane1);
panel.add(pane2);
return panel;
}
void showTable() {
//create and show frame
JPanel testPanel = createTable();
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(testPanel);
frame.pack();
frame.setVisible(true);
}//showTable
void updateRowHeights() {
for (int row = 0; row < table.getRowCount(); row++) {
int rowHeight = table.getRowHeight();
Component comp = table.prepareRenderer(table.getCellRenderer(row, 1), row, 1);
rowHeight = Math.max(rowHeight, comp.getPreferredSize().height);
table.setRowHeight(row, rowHeight);
}
}
void updateRowHeights2() {
for (int row = 0; row < table2.getRowCount(); row++) {
int rowHeight = table2.getRowHeight();
Component comp2 = table2.prepareRenderer(table2.getCellRenderer(row, 1), row, 1);
rowHeight = Math.max(rowHeight, comp2.getPreferredSize().height);
table2.setRowHeight(row, rowHeight);
}
}
public static void main(String[] args) {
TableExample example = new TableExample();
example.showTable();
}//main
#Override
public void tableChanged(TableModelEvent e) {
updateRowHeights();
updateRowHeights2();
}
}//TableExample
ListHandler
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import javax.swing.TransferHandler;
import javax.swing.table.DefaultTableModel;
public class ListHandler1 extends TransferHandler {
DefaultTableModel model = new DefaultTableModel();
ListHandler1(DefaultTableModel tableModel) {
this.model=tableModel;
} //constructor
/**
*
*/
private static final long serialVersionUID = 1L;
/* canImport: This method tests suitability of a drop operation. Here we filter out the clipboard paste operations
* and allow only String drop operations. If the method returns false, the drop operation is cancelled.(non-Javadoc)
* #see javax.swing.TransferHandler#canImport(javax.swing.TransferHandler.TransferSupport)
*/
public boolean canImport(TransferSupport support) {
if (!support.isDrop()) {
return false;
}
return support.isDataFlavorSupported(DataFlavor.stringFlavor);
}
/*The importData() method transfers the data from the clipboard or from the drag and drop operation to the drop location.
* (non-Javadoc)
* #see javax.swing.TransferHandler#importData(javax.swing.TransferHandler.TransferSupport)
*/
public boolean importData(TransferSupport support) {
if (!canImport(support)) {
return false;
}
Transferable transferable = support.getTransferable(); //The Transferable is the class, where the data is bundled.
String line;
try {
line = (String) transferable.getTransferData(DataFlavor.stringFlavor); //We retrieve our data.
} catch (Exception e) {
return false;
}
String item [] = line.split("\t");
model.addRow(new Object[]{item[0],item[1]});
return true;
}
}
SearchRenderer:
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class SearchRenderer1 extends JTextArea implements TableCellRenderer {
private static final long serialVersionUID = 1L;
public SearchRenderer1() {
setLineWrap(true);
setWrapStyleWord(true);
} //constructor
public Component getTableCellRendererComponent (JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column ) {
this.setText((String)value);
//for update row height
this.setSize(table.getColumnModel().getColumn(column).getWidth(),Short.MAX_VALUE);
if (isSelected) {
this.setBackground(new java.awt.Color(255, 240, 200));
}
else {
this.setBackground(new java.awt.Color(255, 255, 255));
}
return this;
}//getTableCellRendererComponent
}
I wonder if there is a way to change to source code format automatically produced
by Net Beans IDE in GUI - applet applications. For example placement of the items in the source code are relational but what if I want them in absolute coordinates. I am asking this question because I need source code in that format so that I can easily change source code and can do some manual job. More specially, I want to create a Button Group of 12x8 array with no gap between them . But using IDE to do this takes long time and indeed, I couldn't even placed the buttons with no gap between them. Any help highly appreciated!
This is simple to put together manually. GUI builders usually harm more than they help.
Here's the test run:
And here's the code. I put the classes together in one file to make it easier to paste. The classes should be in separate files.
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class ButtonArray implements Runnable {
#Override
public void run() {
JFrame frame = new JFrame("JButton Array Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ButtonPanel buttonPanel = new ButtonPanel();
frame.add(buttonPanel.getMainPanel());
frame.setLocationByPlatform(true);
// frame.setSize(new Dimension(800, 600));
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new ButtonArray());
}
public class ButtonPanel {
private static final int WIDTH = 12;
private static final int HEIGHT = 8;
private JButton[][] buttonArray;
private JPanel mainPanel;
public ButtonPanel() {
buttonArray = new JButton[WIDTH][HEIGHT];
createPartControl();
}
private void createPartControl() {
mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(HEIGHT, WIDTH));
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
buttonArray[j][i] =
new JButton(createButtonText(j, i));
mainPanel.add(buttonArray[j][i]);
}
}
}
private String createButtonText(int j, int i) {
StringBuilder builder = new StringBuilder();
builder.append("(");
builder.append(i);
builder.append(", ");
builder.append(j);
builder.append(")");
return builder.toString();
}
public JPanel getMainPanel() {
return mainPanel;
}
}
}
You need to use some grid like layout for the panel (ex. FormLayout) configure it and simply add all buttons there.