Creating scalable checkered margin in java swing - java

I need to write programm which must drawing graphs.. I use java with swing (if you think that swing it is not good decigion please offer other tools) .. And my teacher say that I must drawing graphs on checkered margin and I don't know what use in this situation.. If someone do something like this please help me.. How make this checkered margin (and my teacher say to me that this checkered margin must be scalable)
UPDATE
Oh I forget to say that I mast drawing graph by myself (set 2 dots and then my function must draw line between this dots)

For reference, MyPanel draws a gray checkerboard in its background; SineTest shows how to plot using cartesian coordinates.

Related

Is there a function to find color of point in AWT/SWING

So, I've created this little maze building algorithm (trying to understand how Java AWT & Swing works).
Now, I've used the Graphics (paint method) to create the maze itself, meaning I didn't store it anywhere.
Now I wanted to know if there is a way to know if at given a specific point on the JFrame, is there a way to tell which color it is?
If so, how can I do it?
Let's say for example in my maze I want to see what color is on the PURPLE dot (seeing if it's a wall there basically).
Is there a way to do it, or do I have to do a work-around that?
Thanks.
There is no direct way to a Component's image buffer. But there are two indirect ways.
(Better) Create an Image (Either BufferedImage or Component.createImage) with the same height and width as your Component. You can then run myComponent.paint(myBuffer.getGraphics());. This will draw the component on the image, and from there you can get the pixel Color you are interested in
(Alternative) You can use java.awt.Robot to capture the screen in an Image. Use this image similar as described above

Drawing in a grid on an AWT Canvas

Hi I'm currently making an application for some research I am doing. I need to be able to draw on a grid in a canvas. More detail:
I need to create the grid, possibly displaying it in the canvas itself, with a customizable number of vertical/horizontal grid markers.
I need to be able to draw points that snap only to grid line intersections.
I need to be able to draw lines that can only snap to grid line intersections as well.
I need a way to fill in an area that has been bounded by the lines that I drew with a certain color.
Please note that I am not looking for someone to write the program for me, I just need help taking that first step and where to look for additional resources etc. I have no idea where to begin. Also I am using NetBeans GUI Builder, if that is helpful. All I have now is the canvas set up. I have no idea where to go from there. Thank you very much.

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

Creating a small images from JPanel

I'm writing simple slide program in Java. In that program, I draw lines, ellipses, rectangles and etc in each slide. What is more, like a powerpoint I want to show all of my slides' symbolic small pictures on the JList.
How should I create small images from all elements in JPanel?
Thanks.
You might look at capturing a panel's image using Screen Image, discussed here.
Addendum: See also ComponentImageCapture.
If I understood your task correct, you must inherit from JPanel and overload method paintComponent(Graphics g). Inside you can write something like g.drawLine(0,0, 10, 10)

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?

Categories