So i am new to JMapViewer (and StackOverFlow). What I am currently trying to do is to develop a real time graphical representation of a network, where the background is a map of the area. So far it's been going well, however one issue is bugging me. When I create a MapMarkerCircle, and set the name, I want to create a new line, to show information below it.
I've tried "\n", but this won't work. I've tried enclosing it in html format
and using <br> to break the line, but again, it just prints the entire thing as if it was a string.
If anyone has come across this issue before, I would really appreciate any help.
Here is a small bit of code where the problem is occuring. Note that "RateCircle" extends "MapMarkerCircle".
Coordinate dataPoint= new Coordinate((pmpLinks[i].getRecieverSite().getLat()+pmpLinks[i].getTransmitter().getLat())/2, (pmpLinks[i].getRecieverSite().getLon()+pmpLinks[i].getTransmitter().getLon())/2);
String rateStringName="<html>"+inRateAsString+"<br>kb/s</html>";
pmpCanopyRatePoints[i]=new RateCircle(allPMPrateLayer[i],rateStringName, dataPoint);
map().addMapMarker(pmpCanopyRatePoints[i]);
MapMarkerCircle::paint calls MapMarkerCircle::paintText, which calls Graphics::drawString, which accords no special meaning to control characters or markup. Starting from this example, the implementation of paintText() below draws a second line beneath the first.
I've updated the example to suggest a way to associate the marker's name and value. It uses a Map<String, Integer>, passing a Map.Entry<String, Integer> as a parameter to the RateCircle constructor.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JFrame;
import org.openstreetmap.gui.jmapviewer.Coordinate;
import org.openstreetmap.gui.jmapviewer.JMapViewer;
import org.openstreetmap.gui.jmapviewer.MapMarkerCircle;
import org.openstreetmap.gui.jmapviewer.Style;
/**
* #see https://stackoverflow.com/a/38265252/230513
* #see https://stackoverflow.com/a/33857113/230513
*/
public class RateCircleTest {
private void display() {
JFrame f = new JFrame("London");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMapViewer map = new JMapViewer() {
#Override
public Dimension getPreferredSize() {
return new Dimension(320, 240);
}
};
Coordinate london = new Coordinate(51.5072, -0.1275);
map.setDisplayPosition(london, 16);
Map<String, Integer> rates = new HashMap<>();
rates.put("London", 42);
for (Map.Entry<String, Integer> entry : rates.entrySet()) {
map.addMapMarker(new RateCircle(entry, london));
}
f.add(map);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private static class RateCircle extends MapMarkerCircle {
private static final int R = 12;
private Map.Entry<String, Integer> entry;
public RateCircle(Map.Entry<String, Integer> entry, Coordinate coord) {
super(null, "", coord, R, STYLE.FIXED, getDefaultStyle());
this.entry = entry;
Style style = getStyle();
style.setBackColor(Color.cyan);
style.setColor(Color.red);
}
#Override
public void paintText(Graphics g, Point position) {
super.paintText(g, position);
g.drawString(entry.getKey(), position.x + R + 2, position.y + R);
g.drawString(entry.getValue() + " kb/s", position.x + R + 2,
position.y + R + g.getFontMetrics().getHeight());
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new RateCircleTest()::display);
}
}
You can use System.lineSeparator():
String rateStringName = inRateAsString + System.lineSeparator() + "kb/s";
Related
I've got a slight problem, I'm writing a gps tracking app to track several objects at once. The data comes in over a serial interface, this is coming in fine from what I can tell. The issue is that I need to continually update the JPanel where the map is created and displayed.
public JPanel mapDisplay(){
JPanel mapPanel = new JPanel();
mapPanel.setSize(560, 540);
Coordinate start = new Coordinate (-34.9286, 138.6);
trackMap.addMapMarker(new MapMarkerDot(1Lat, 1Lon));
trackMap.setDisplayPosition(start,8);
System.out.println(1Lat);
mapPanel.add(trackMap);
mapPanel.setVisible(true);
return mapPanel;
}
This is what I have and it's happy to display the point once but won't update. If I print out the 1Lat variable in the serial method it continually prints, however it only does it once here.
A lot of the answers I've found refer to setting markers by arrays, however that won't work in this case as the objects I'm tracking could be anywhere.
Any help would be greatly appreciated :)
Is it possible to use a worker thread and not use an ArrayList? I would run the risk of missing data if I do.
Not necessarily. In a SwingWorker, your implementation of the doInBackground() method can publish() results as they become available. Note in particular that "Results from multiple invocations of publish() are often accumulated for a single invocation of process()." In your process(), simply loop through the List<Coordinate>, update the route and repaint() the map.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.SwingWorker;
import org.openstreetmap.gui.jmapviewer.Coordinate;
import org.openstreetmap.gui.jmapviewer.JMapViewer;
import org.openstreetmap.gui.jmapviewer.MapPolygonImpl;
/**
* #see http://stackoverflow.com/a/37193636/230513
*/
public class MapWorkerTest {
private final List<Coordinate> route = new ArrayList<>();
private void display() {
JFrame f = new JFrame("MapWorker");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMapViewer map = new JMapViewer() {
#Override
public Dimension getPreferredSize() {
return new Dimension(640, 480);
}
#Override
public String getToolTipText(MouseEvent e) {
Coordinate c = (Coordinate) getPosition(e.getX(), e.getY());
return c.getLat() + " " + c.getLon();
}
};
map.setToolTipText("");
Coordinate start = new Coordinate(-34.9286, 138.6);
route.add(start);
MapPolygonImpl poly = new MapPolygonImpl(route);
poly.setColor(Color.blue);
map.addMapPolygon(poly);
map.setDisplayPosition(start, 10);
f.add(map);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
new MapWorker(map, start).execute();
}
private class MapWorker extends SwingWorker<Void, Coordinate> {
private final JMapViewer map;
private Coordinate last;
public MapWorker(JMapViewer map, Coordinate start) {
this.map = map;
this.last = start;
}
#Override
protected Void doInBackground() throws Exception {
while (!isCancelled()) {
last = new Coordinate(last.getLat() + 0.0025, last.getLon() + 0.01);
publish(last);
Thread.sleep(1000);
}
return null;
}
#Override
protected void process(List<Coordinate> chunks) {
for (Coordinate c : chunks) {
route.add(c);
}
map.repaint();
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new MapWorkerTest()::display);
}
}
Multiple route management left as a exercise.
I recently have implemented clipping in my VTK Java program. I used BoxWidget to control what should be clipped. However, i'm having an issue with vtkRenderWindowInteractor that attached to BoxWidget. The program freezes at the renderWindowInteractor.Start() statement (I've remarked it in my code).
This is my re-simulate code :
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import vtk.*;
public class VTKWindowInteractor extends JPanel {
static {
if (!vtkNativeLibrary.LoadAllNativeLibraries()) {
for (vtkNativeLibrary lib : vtkNativeLibrary.values()) {
if (!lib.IsLoaded()) {
System.out.println(lib.GetLibraryName() + " not loaded");
}
}
System.out.println("Make sure the search path is correct: ");
System.out.println(System.getProperty("java.library.path"));
}
vtkNativeLibrary.DisableOutputWindow(null);
}
private vtkPanel renWin;
private vtkRenderWindowInteractor renderWindowInteractor;
private vtkPolyDataMapper mapper;
private vtkActor coneActor;
private vtkPlanes planes;
private vtkBoxWidget boxWidget;
public VTKWindowInteractor() {
setLayout(new BorderLayout());
renWin = new vtkPanel();
add(renWin, BorderLayout.CENTER);
renWin.setMinimumSize(new Dimension(50, 50));
renWin.GetRenderer().SetBackground(0, 0, 0); // black
renWin.GetRenderWindow().AddRenderer(renWin.GetRenderer());
}
public void render() {
mapper = new vtkPolyDataMapper();
vtkConeSource cone = new vtkConeSource();
cone.SetHeight(3.0);
cone.SetRadius(1.0);
cone.SetResolution(10);
mapper.SetInputConnection(cone.GetOutputPort());
coneActor = new vtkActor();
coneActor.SetMapper(mapper);
renWin.GetRenderer().AddActor(coneActor);
planes = new vtkPlanes();
renderWindowInteractor = new vtkRenderWindowInteractor();
renderWindowInteractor.SetRenderWindow(renWin.GetRenderWindow());
boxWidget = new vtkBoxWidget();
boxWidget.SetInteractor(renderWindowInteractor);
boxWidget.SetPlaceFactor(1.25);
boxWidget.PlaceWidget(coneActor.GetBounds());
boxWidget.AddObserver("InteractionEvent", this, "executeClipping");
renderWindowInteractor.Initialize();
boxWidget.On();
renWin.Render();
renWin.resetCamera();
/**************************************/
// This is where the freeze come from //
// //
/************************************/
renderWindowInteractor.Start(); // if i comment out this line, the program works but the boxWidget cannot be resized or rescale or moved
}
public void executeClipping() {
planes = new vtkPlanes();
boxWidget.GetPlanes(planes);
mapper.SetClippingPlanes(planes);
planes.Delete();
}
public static final int WINDOW_WIDTH = 1000;
public static final int WINDOW_HEIGHT = 500;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
VTKWindowInteractor _vtkRendererPanel = new VTKWindowInteractor();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("......");
frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
frame.setVisible(true);
frame.setLayout(new BorderLayout());
frame.add(_vtkRendererPanel);
_vtkRendererPanel.render();
}
});
}
}
I have been finding my mistake for hours and hours and frustrated hence come here to seek for help. If anyone have experienced this or know what did I do wrong please correct me. Thanks !!!
VTK version : 6.2.0
OK. I finally solved the problem. The follows is the quote from vtkPanel.java
/*
*
* Java AWT component that encapsulate vtkRenderWindow, vtkRenderer, vtkCamera,
* vtkLight.
*
* If a vtkInteractor is needed, use vtkCanvas instead. This is necessary when
* Widget and Picker are used.
*
* #author Kitware */
so I changed my
vtkPanel
to
vtkCanvas
&
renderWindowInteractor = new vtkRenderWindowInteractor();
to
renderWindowInteractor = renWin.getRenderWindowInteractor();
and it solves the problem.
Thanks and this is for anyone who are going to face similiar problem as me in the future.
I am developing one application using Jung2 which will show the connection type between two point,there is two different type of connection are available.I want to show each type of connection using different color.For that i want to add two different colored edge between two vertex from A to B it will be black and B to A it will be green.Can any one help me with an example...
You have to define a Transformer that receives an edge and returns a Paint - in this case, the Paint can simply be a Color. This Transformer has to be assigned to the RenderContext as
ArrowFillPaintTransformer
ArrowDrawPaintTransformer
EdgeDrawPaintTransformer
The decision of whether the edge has to be painted in green or in black is based on the vertices in this example (The statement if (s.equals("v1") && d.equals("v0")) means that it is the edge from "v1" to "v0").
import java.awt.Color;
import java.awt.Paint;
import javax.swing.JFrame;
import org.apache.commons.collections15.Transformer;
import edu.uci.ics.jung.algorithms.layout.FRLayout;
import edu.uci.ics.jung.graph.DirectedSparseGraph;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.visualization.VisualizationViewer;
public class JUNGEdgeColorsTest
{
public static void main(String[] args)
{
JFrame jf = new JFrame();
final Graph<String, String> g = getGraph();
VisualizationViewer<String, String> vv =
new VisualizationViewer<String, String>(new FRLayout<String, String>(g));
Transformer<String, Paint> colorTransformer = new Transformer<String, Paint>()
{
#Override
public Paint transform(String e)
{
final String s = g.getSource(e);
final String d = g.getDest(e);
if (s.equals("v1") && d.equals("v0"))
{
return Color.GREEN;
}
return Color.BLACK;
}
};
vv.getRenderContext().setArrowFillPaintTransformer(colorTransformer);
vv.getRenderContext().setArrowDrawPaintTransformer(colorTransformer);
vv.getRenderContext().setEdgeDrawPaintTransformer(colorTransformer);
jf.getContentPane().add(vv);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}
public static Graph<String, String> getGraph()
{
Graph<String, String> g = new DirectedSparseGraph<String, String>();
g.addVertex("v0");
g.addVertex("v1");
g.addEdge("e0", "v0", "v1");
g.addEdge("e1", "v1", "v0");
return g;
}
}
Actually, i have already ask this question in here. But, i'm making mistake. I haven't already get the solution.
First, at the question before, i can get Rectangle with
Rectangle rectangle = textArea.modelToView( textArea.getCaretPostion() );
I'm also get X and Y position.
I'm creating a editor that can add new Text Area each i press Enter key. XY position with code above always give same return in every Text Area. Look my code.
import java.awt.Container;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;
public class forquestion extends JFrame {
Container textAreaBox;
LinkedList<JTextComponent> textArea;
int nameTA;
public forquestion() {
int nameTA = 0;
textArea = new LinkedList<>();
textAreaBox = Box.createVerticalBox();
textAreaBox.add(Box.createVerticalGlue());
addLine();
this.add(textAreaBox);
this.setVisible(true);
}
public static void main(String[] args) {
forquestion app = new forquestion();
app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
public void addLine () {
JTextComponent temp_ta = createTextComponent();
textArea.add(temp_ta);
textAreaBox.add(textArea.getLast());
textAreaBox.add(Box.createVerticalGlue());
}
protected JTextComponent createTextComponent() {
JTextArea ta = new JTextArea("test");
/*if (count%2==0)
ta.setForeground(Color.red);
else
ta.setForeground(Color.GREEN);*/
ta.setFont(new Font("Courier New",Font.PLAIN,16));
ta.setLineWrap(true);
ta.setWrapStyleWord(true);
ta.setName(Integer.toString(nameTA));
nameTA+=1;
basicKey("ENTER", enter, ta);
ta.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent ev) {
try {
taMousePressed(ev);
} catch (BadLocationException ex) {
Logger.getLogger(forquestion.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
return ta;
}
public void basicKey(String s, Action a, JTextArea ta) {
ta.getInputMap().put(KeyStroke.getKeyStroke(s), s);
ta.getActionMap().put(s, a);
}
Action enter = new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
addLine();
}
};
private void taMousePressed(java.awt.event.MouseEvent ev) throws BadLocationException {
int now_focus = Integer.parseInt(ev.getComponent().getName());
int _caret;
_caret = textArea.get(now_focus).getCaretPosition();
Rectangle rectangle = textArea.get(now_focus).modelToView(_caret);
double x = rectangle.getX();
//int xc = textArea.get(now_focus).getLocation().x;
double y = rectangle.getY();
//int yc = textArea.get(now_focus).getLocation().y;
//double h = rectangle.getHeight();
//double w = rectangle.getWidth();
System.out.println(x);
System.out.println(y);
//System.out.println(xc);
//System.out.println(yc);
//System.out.println(h);
//System.out.println(w);
System.out.println("");
}
}
My code will print XY position each time you press a Text Area. But, the display always same in every text area. (Try to make many Text Area and give some text) Btw, it just simple code. You need change the window frame size for update the new text area after you press enter key..hahaha.
So, my question is: How can i get the XY position of caret (text cursor) in any Text Area. I want to display JPopmenu there. :)
I hope this question clear for you. Thx before.
The Rectangle reported back is relative to the text area, where it's 0x0 position is the top, left corner of the component.
If you use something like...
popup.show(textArea.get(now_focus), rectangle.x, rectangle.y + rectangle.height);
Where popup is a JPopupMenu, it will make the required translations to the screen itself.
Now. Having said that. Personally, I would prefer to use the popup API support provided by Swing. This is going to mean needing to create a custom component that extends from JTextArea to achieve it...
public class MyPopupTextArea extends JTextArea {
/*...*/
public Point getPopupLocation(MouseEvent evt) {
Rectangle rectangle = textArea.get(now_focus).modelToView(_caret);
Point p = rectangle.getLoction();
p.y += rectangle.height;
return p;
}
}
Then, based on your needs, you can use setComponentPopup to provide a shared instance of the JPopupMenu or, if required, create a custom JPopupMenu for each instance of the custom editor and use setComponentPopup as you see fit...no messing about with mouse listeners ;)
I am playing with the JUNG library and I want to create a graph, whose nodes are some gui(jframe presumably) items.
I want each of the nodes to have a few buttons, one text field, menu, etc.
And my question is: is this possible?
If yes, I have two other questions:
1. How should I approach it(I am new to Java and I am not familiar with the best practices )
2. What interface should I use(I am looking at .visualisation.decorators.* currenlty).
Thank you in advance.
OK, eventually I succeeded creating a graph of JPanels and JFrames. I will continue with JPanels. And here is the result:
But now I face some other problems:
The frames can be moved, but only if they are picked at the upper left corner. How can I make the whole JPanel area pickable?
I need the JPanels to be accessible, e.g. I should be able to press the button, write some text in a textbox(this is not implemented yet) and probably resize the JPanel. Currently the JPanels are "inactive/no focus" for some reason. I tried the enable method, no success. Tried some "focus" methods, again failed.
Any suggestions on the above two questions?
Here is how my code looks like:
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.geom.Point2D;
import javax.swing.BorderFactory;
import javax.swing.CellRendererPane;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import edu.uci.ics.jung.algorithms.layout.CircleLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.graph.DirectedSparseGraph;
import edu.uci.ics.jung.visualization.RenderContext;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.visualization.control.ModalGraphMouse;
import edu.uci.ics.jung.visualization.picking.PickedState;
import edu.uci.ics.jung.visualization.renderers.Renderer;
import edu.uci.ics.jung.visualization.transform.shape.GraphicsDecorator;
public class GraphPanel extends Container
{
static final long serialVersionUID = 420001L;
DirectedSparseGraph<Number, Number> graph = null;
VisualizationViewer<Number, Number> vv = null;
PickedState<Number> pickedState = null;
public GraphPanel(Number[][] nodes_list)
{
try
{
graph = new DirectedSparseGraph<Number, Number>();
construct_graph(nodes_list);
vv = new VisualizationViewer<Number, Number>
(new CircleLayout<Number, Number>(graph), new Dimension(400, 400));
vv.getRenderer().setVertexRenderer(new MyRenderer());
// The vertex pick listener
pickedState = vv.getPickedVertexState();
pickedState.addItemListener(new ItemListener()
{
#Override
public void itemStateChanged(ItemEvent e)
{
Object subject = e.getItem();
if (subject instanceof Number)
{
Number vertex = (Number) subject;
if (pickedState.isPicked(vertex))
{
System.out.println("Vertex " + vertex + " is now selected");
}
else
{
System.out.println("Vertex " + vertex + " no longer selected");
}
}
}
});
// The following code adds capability for mouse picking of
// vertices/edges. Vertices can even be moved!
final DefaultModalGraphMouse<Number, Number> graphMouse = new DefaultModalGraphMouse<Number, Number>();
vv.setGraphMouse(graphMouse);
graphMouse.setMode(ModalGraphMouse.Mode.PICKING);
}
catch (Exception e)
{
System.err.println("Failed to construct graph!\n");
System.err.println("Caught Exception: " + e.getMessage());
}
}
/*Attach the graph panel/container to a specified frame*/
public void attach_to_frame(JFrame frame)
{
frame.setContentPane(vv);
}
/*This one should be reimplemented*/
private void construct_graph(Number[][] nodes_list)
{
int i = 0;
/*add the nodes*/
for(i=0; i<nodes_list.length; i++)
{
graph.addVertex(i);
graph.addEdge(nodes_list[i][0], nodes_list[i][1], nodes_list[i][2]);
}
}
/*re-implement the render functionality to work with internal frames(JInternalFrame)*/
static class MyRenderer extends JPanel implements Renderer.Vertex<Number, Number>
{
static final long serialVersionUID = 420000L;
#Override
public void paintVertex(RenderContext<Number, Number> rc,
Layout<Number, Number> layout, Number vertex)
{
try
{
GraphicsDecorator graphicsContext = rc.getGraphicsContext();
Point2D center = layout.transform(vertex);
Dimension size = new Dimension(100, 80);
System.out.printf("Vertex[%d] X = %d Y = %d: Running paintVertex()\n", vertex, (int)center.getX(), (int)center.getY());
JPanel sv = new JPanel();
sv.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
sv.setBackground(Color.GREEN);
sv.setPreferredSize(size);
sv.add(new JButton("Button1"));
//OK
graphicsContext.draw(sv, rc.getRendererPane(), (int)center.getX(),
(int)center.getY(), size.width, size.height, true);
}
catch (Exception e)
{
System.err.println("Failed to render images!\n");
System.err.println("Caught Exception: " + e.getMessage());
}
}
}
public static void main(String[] args)
{
/*Create the window*/
JFrame frame = new JFrame("BLABLA");
Number[][] list = {{0, 1, 3}, {1, 3, 1}, {2, 2, 3}, {3, 2, 0}};
GraphPanel g = new GraphPanel(list);
g.attach_to_frame(frame);
frame.getContentPane().setPreferredSize(new Dimension(640, 480));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}/*2*/
Yes it is possible (I think). I would recommend you change your VertexLabeler to one that you implement yourself. Something in the lines of...
VisualizationImageServer<V, E> vv = new ...;
vv.getRenderContext().setVertexLabelRenderer(new MyVertexRenderer());
class MyVertexRenderer extends JFrame implements VertexLabelRenderer {}
But I'm not sure how it is going to work in terms of positioning it later. You might need to change the VertexShapeTransofrmer aswell with vv.getRenderContext().setVertexShapeTransformer().
I must say, I've spent a lot of time trying to get Jung to render a graph nicely like graphviz with very little success.