how to extract information from jpanel - java

I am trying a project called white board sharing in which I need to get the information from a panel and that information is like some drawings so how can I get it from a panel. In that panel I am drawing some rectangles circles.

In case you want to react on mouse events you might want to investigate the following sections of the Java API.
MouseListener
MouseMotionListener
Also if you want to react on mouse events that are triggered when the users moves his mouse over a drawn element you might be interested in Graphics2D and the classes that implement Shape.

You can pass a Graphics object to the panel for it to paint the items that were drawn.

It may help to think of the board as a view of some model that records an abstract representation of the board's content. As a concrete example, this GraphPanel has a very simple model containing nothing more than a List<Node> and a List<Edge>. These two members could be wrapped and serialized as discussed in this tutorial.

Related

How to add 2 JComponent to JPanel at the same place

How to add 2 JComponent to JPanel at the same place stretched on full JPanel? I tryed to:
add(ship,null);
add(ground,null);
I'm guessing as to your use case, but is "ground" a background image containing JPanel? If so, then add it in such a way so that it fills the container, perhaps via BorderLayout.CENTER with the container using BorderLayout of course.
The ship, I'm guessing is a sprite that is drawn on the background. If so, consider having it not be a JPanel but rather a logical object that holds an image Sprite that can be drawn in the ground's paintComponent method.
As always the devil will be found in the details. You first.
Edit
You state in comment:
Stage is my background and it extends JPanel. Ship is a player element (JComponent). Ground (JComponent) is something which player can't touch. I wanted to have this two elements (ground and ship) like layers where the image is drawn. I need to have one point reference to check collisions.
I assume that you have a paintComponent(Graphics g) override method in your Stage object where you draw your background image.
Based on this assumption, I change my previous recommendation in that I feel that neither ground nor ship should extend JPanel since you don't really need for them to do this. You already have a JPanel on which you can do your drawing and game interaction, so why create more when they'll only clutter things up? The only exception I see is if either ground or ship need to hold other components, and if so, then yes, they should extend JPanel.
Consider:
What is important to both of these objects, both ground and ship, are their locations and their images.
Give both ground and ship a BufferedImage field that can hold their images, with getters and setters.
Give them x and y int position fields and width and height, with getters and setters that can be used to test for collision and can be used to move their respective sprite (image).
Give them a draw(Graphics g) method that draws their respective images at the x an y location when called and passed a valid Graphics instance.
Have Stage hold an instance of each ground and ship, and have stage's paintComponent(Graphics g) method draw both instances by calling their respective draw methods.
Edit 2
You now ask:
One more question: When should i have keyListener?
I recommend that you not use a KeyListener.
KeyListener is a low-level construct, and in general you should prefer to use higher-level constructs such as Key Bindings as they are safer and easier to use without causing side effects or having problems. For instance, it is easy to run into problems with focus issues if you use a KeyListener since it only works if the component listened to has the focus. This is easily circumvented if you used key bindings instead.
You can find the Key Bindings tutorial here
and an example of using it with animation in my answer here.

What is the use of Canvas in AWT?

I've been searching many sources yet I still cannot find a decent explanation. Why should I use it, what is its' purpose and why does it differ from JPanels and such?
The speciality of Canvas is that, like Window, it can provide customized hardware-accelerated double-buffering and page-flipping. See BufferStrategy.
A canvas is for drawing on, basically. It also serves like a Panel for creating a custom AWT-based component, but unlike Panel it can't contain other components.
From my understanding, Canvas is just the AWT version of Swing's JComponent. You shouldn't use it directly, unless you're making a pure AWT app. You can find more info here.
public class Canvas
extends Component
implements Accessible
A Canvas component represents a blank rectangular area of the screen onto which the application can draw or from which the application can trap input events from the user.
An application must subclass the Canvas class in order to get useful functionality such as creating a custom component. The paint method must be overridden in order to perform custom graphics on the canvas.
A canvas is for drawing on, basically. It also serves like a Panel for creating a custom AWT-based component, but unlike Panel it can't contain other components.

Java Swing/AWT - painting objects with context menus

I want to make something akin to a diagram editor - an application that allows the user to create, view and edit a bunch of shapes on a canvas. My GUI has essentially three parts - a standard JMenuBar, one JPanel on the right side for showing info about the shapes and a JPanel next to it that should be used for visualizing the shapes.
Aside from that, I have a package that defines the shapes. For simplicity's sake say there is just a Square, containing the following information: coordinates on the canvas, size, user-defined name and description, color.
The main class of the project (an extension of JFrame) contains lists of Squares.
Now, I could visualize the shapes by simply drawing them in the JPanel using drawrect and whatnot, but I want to create an interactive editor - if the user right-clicks on a visualized shape, a context menu would pop-up allowing him to move it, change its properties or remove the shape altogether. Clicking an empty spot in the JPanel would allow the user (again, via a menu) to create a new shape.
Is there an automated way to do this as opposed to manually keeping a matrix mapping each pixel of the canvas to a certain shape and checking it upon right click? One where I could say, for example, draw this here within the JPanel and do something onclick...
A related question, when I edit the shape in the JPanel, how do I access the Square/Circle list in the main application class, so that I can really change it? I suppose this is simple, but right now I don't know how I would do it.
Please point me in the right direction, anything will be appreciated, tips, examples, links to relevant tutorials...
As suggested by #eugener, classes that implement the Shape interface have a contains() method that is useful for this. GraphPanel is an example that illustrates several of the features you mention.
The most common way to accomplish this is to allow shapes to determine if point is inside the shape. Hopefully your code is object oriented and each type of your shape is a class.
All you have to do is to define a method such as boolean isInside( point: Point) for each shape. Once you have those all you have to do is walk the shapes in the reverse z-order (from top to bottom) and and see where the mouse click point lends. If it does not lend on any shape - you clicked the canvas. Once you have this info you can show an appropriate menu.
Hope this helps

Jpanel / Jframe / buffering graphics etc game structure direction

I'd like some advice regarding structure of a game I'm working on. Specifically where to place painting methods.
Currently there is a applet wrapper class for a Jpanel which runs the game loop.
The game itself is meant to simulate a very large area. objects will have x&y values which themselves will part of a larger x&y grid.
i.e. object1 position is 150000x30000 in grid block 1,5.
objects will need to be able to move into neighbouring grids, however I'd rather not run each grid block until needed as 99% of them will be empty.
Currently the UI is a Jpanel with a few buttons + listeners, a large drawing pane is needed to display the objects.
my question is:
what class should this internal drawing pane be based on? I'd like to have control to zoom and pan around the grid. it only needs to draw what is visible, but object movements will continue in the game loop.
what painting strategy would be applicable for simple (icons really when zoomed out) moving around vast areas, I'm guessing relying on the EDT to repaint isn't going to be good enough?
I'm not really after specific code, I want to learn myself how to do this, I just need pointing in the right direction, as most things I read don't seam to quite cover what I'm after, or don't make use of JRE6+ features.
Many Thanks
Rather than paint each grid cell in your drawing pane, why don't you have each object repaint itself onto the grid?

Java2D: Capturing an event on a Line object

I have a JPanel which has a line, circle, etc. Now when I click on the line, will the event get reported as a line event or a general JFrame event. I need to be able to move the line if the user clicks on the line and moves it. Is this possible in Java2D?
Yes, but you will need to do some work (see java.awt.Shape). Basically you need to track a list of Shapes. The JPanel will recieve a mouse event, which you can translate to (x,y) coordinates. You can then call Shape.contains(x,y) to see if your various shapes were clicked on.
This will work well for Circle, Polygon, Arc, etc; however in the case of Line2D, it won't work as easily, but you can use Line2D.intersects() with a small rectangle around the mouse click (this is also good UI since you don't want to force the user to click exactly on a pixel that is hard to see).
There is no such concept as a "line event" unless you decide to implement one.
I would suggest adding a MouseListener and a MouseMotionListener to the Canvas or JPanel onto which your geometric shapes are drawn. Use the MouseListener's mousePressed(MouseEvent) callback to determine whether a given shape has been clicked on. Once you've established this, use MouseMotionListener's mouseDragged(MouseEvent) method to move and redraw the shape as the mouse cursor is moved.
Here's a simple example that demonstrates some of the techniques adduced in other answers.
I created a canvas markup library in Java a few years back and if you don't need to worry about transforms on the canvas (scaling, rotation, etc.) it is very easy to do.
Basically you just need to maintain a collection of the canvas shapes in a List (not a Set because Z order is probably important). The mouse listener will be on your canvas, and not on individual shapes. Add new items to the beginning of your collection (or iterate the list backwards later).
When the canvas receives a mouse down event iterate through your collection of shapes until you find one that is underneath your mouse coordinates. The easiest way to do this is to have your shapes implement an interface that defines some sort of hitPoint(int x, int y) method. That way your rectangles can implement a contains(), lines can do intersects() or graphics paths, you can account for some hit padding, etc.
Taking it one step further, your shapes should define their own draw(Graphics2D g) method so that you can easily do things like selection boxes, or set the paint mode to XOR to make shape 'moving' easier. The paintComponent method of your canvas would just have to iterate through your collection of shapes, calling shape.draw(g) on each one, passing in the graphics instance provided to the paintComponent method.

Categories