Java Simple Line Drawing Program - java

I want to create a simple java application for drawing only lines.
My program is like that now;
User can draw everything by draggin his mouse, but by the time he release his finger, I deleted everything from the screen and I draw a line withrespect to first mouse coordinates and the last mouse coordinates.
However, because everytime I cleared the screen, user can only draw one line.
If I dont clean the screen, there are lines but also curves and etc which are created while user dragging his mouse.
How should I find a solution for that problem ?
Thanks.

One straightforward way to solve your problem is to retain state in the program. Every time a line is drawn, store it in an ArrayList of point-pairs. When the user successsfully draws one line, store the start point and end point for that line into the ArrayList. Each time the user draws another line, add that pair of points to the ArrayList. Then, when it is time to draw "all the lines", clear the screen and then use a loop, and draw one line for each stored pair of points.
Somewhere in your program there is a class that has a lifetime that is "as long as a drawing", or "as long as the application runs." That's a good place to keep state.

On mouse down, store the position.
On mouse up, make a new line object (define your own class) with the up and down points.
Remove the stored mouse down (Since you don't need it anymore!)
Add your new line object to a list of lines you define
When you paint, always clear everything and draw each line you have stored.
Optionally, if you're on mouse down, also draw a line between current stored mouse down position and the current mouse position.

Store the start and end points of the lines in an object that is put in an expandable collection such as an ArrayList. When it comes time to draw, draw all the lines in the list.

Custom Painting Approaches suggest two approaches. The first to store/redraw the lines as already suggested here. The second approach is to draw directly to a BufferedImage.

Related

How to have mutliple methods of the same type? ("Processsing" programming)

all.
I am newer to programming, but headstrong.
I have an assignment for class, in which we have to use the "draw" method (which I understand is just a function, but in Java) and I need to leave a "trail". I already know how to leave a trail, but I want to leave a specific trail (mainly boot prints from my character's shoe). So I decided to write a separate draw method, but I can only use one. Is there any way to write another draw method?
If not, is there a way to leave a "trail" behind the man where I move him?
What I mean by trail: Using the draw method, I create a box that moves with my mouse. The draw command loops over and over, many times a second. However, it doesn't erase the previous draw, so a trail of boxes a left as they are draw out. I just want to have a trail of a specific shape, one that I can define.
Thank you all.
You have three options:
Option 1: Simply don't clear out old frames, by not calling the background() function every time draw() is called. This will cause your old drawings to stick around, which will look like a trail. This will work for simple stuff like circles, but will not work if you want your trail to be different from your drawing, or to do something like fade out over time.
Option 2: Store your trail in some kind of data structure. You might use an ArrayList that contains PVector instances, for example. Then each frame, clear out the old frames by calling background(), and then iterate over the data structure to draw your trail. Then add and remove from that data structure to change the trail over time.
Option 3: Draw your trail to an offscreen buffer. Hint: look up the createGraphics() function in the reference. This is similar to what you were trying to do, but instead of having a second draw() function you would draw the trail to the buffer. Then each frame, you would draw the buffer to the screen, and finally draw the object to the screen.

Slowing the Move of an Object in a For Loop Java

I am currently working on a project that moves a circle from a space to space when button is pressed. I designed as following: When the button is pressed, it increments the coordinates of circle, in a for loop from 0 to 10.
The problem is, the motion I wanted with for loop doesn't show up on the screen but it only shows the object on the first and the last coordinates, so it doesn't move one by one.
Any suggestion would be appreciated,
Kutay Demireren
You need to set a frame draw length that the human eye can see in order for motion to work properly. The can be achieved in many ways, but the easiest is with a thread.sleep at the end of each for loop iteration for however long you want the different pictures to be visible for.

Trying to make a game viewer without changing the location of every object when camera changes

I've build a simple click and drag background for a program I've written which is all good and working. It adds on the distance moved by the mouse while held down. However thinking ahead I'd have to do this for all objects every time I moved the image as I was going to give the objects positions relative to the image and this might cause a few issues with processing power.
What I would like to do is have the background in a set position within the program and move the users view over the image without having to change the position of every object every tick, the problem I'm having is where to start and how to build something like this.
Thanks in advance
Well you need to tell your program where the objects are supposed to be drawn somehow.
It sounds like you are currently thinking to draw your frame by looping through your objects and drawing them at their location, and when you move the background, updating the object location.
If that method isn't sitting well with you, you could create a sort of 'map' (2-d array of pixels/locations) that you fill sections in on when you create new objects. Then when you go to draw your frame, you could scan through some range of positions in your 'map' and draw the objects that you find. When the background is moved, you could then keep the offset in a single location and use that when determining what range of map values to scan through.
i'm not positive how the processing implications of these two methods compare, i'm sure it depends on the size of your frame, level of detail, and number of objects that you have offscreen at any time.

Drawing a line connecting two rectangles

I am making my own class diagram app in Swing/AWT but i stopped at this functionality:
I want to draw a line between the Class rectangle that already selected and to the target Class rectangle, but line has a feature which is whenever i move one of the rectangles the line that join them get bend in a straight fashion following the moving rectangle , i hope the following picture demonstrate what i want to achieve:
A general guideline or a sample code is highly appreciated
I don't know Java, but the steps you could follow are these:
Find the middle of each line of the rectangles (should be easy, just avarage x1+x2 and y1+y2)
Determine the edges that are closest to each other using Pythagoras formula on the points you got in the previous step.
Start drawing a line starting at xa,ya (first point you got in the step above), and draw it in the direction away from the rectangle. You should know this direction, because you can know the line segment this point is on.
Do the same for xb,yb (point on the second rectangle). If the lines are in opposite directions, you should draw them to halfway xa-xb or ya-yb (depending on if you're drawing horizontally or vertically). If they are perpendicular (is that the right word?) you should draw them to the point where they cross, so you draw the line from xa,ya to xa,yb or xa,ya to xb, ya, depending if you draw the horizontal or vertical line.
There should be some additional check to see if the rectangles overlap. You should not draw lines in the same direction for example. Maybe it would suffice for you to draw just a diagonal line between the two points in those cases where you cannot determine how to draw these straight lines.
For the implementation you could build a line class that uses the observer pattern to listen to the two rectangles it follows, so it can update itself whenever one of them moves or resizes.
http://java-sl.com/connector.html
Hope this helps.
Try with observer pattern. All lines that are connected with a moving object should be notified with new position of the object and 'bent' properly. Of course, first implement some logic that will connect 2 objects.
try creating a class named "ConnectingLine" or similar. this class will then have several segments (that's the name of these line parts in dia, which is currently my favorite uml modeling tool) which will be calculated one by one. you'll have a sepaparate class for this of course ;) called maybe "LineSegment". i think this should make it easier for you to perform the mathematical calculations required to perform this task.
this could also enable making segments "auto routed or not" easy d(^_^)b

Pacman maze in Java

So I'm building the pacman game in Java to teach myself game programming.
I have the basic game window with the pacman sprite and the ghost sprites drawn, the pacman moves with the arrow keys, doesn't move beyond the walls of the window, etc. Now I'm trying to build the maze, as in this picture:
Without giving me the direct/complete solution to this, can someone guide me as to how this can be built? I'm talking only about the boundaries and the pipes('T' marks) here which you can't go through and you have to go around. Not the dots which the pacman eats yet.
Here are my questions:
1) What's the most efficient algorithm/method for creating this maze? Will it have to be drawn each time the paint() method is called or is there a way to draw it only at the start of the game and never again?
2) How will this actually be drawn to the screen? I assume the fillRect() will be used?
3) Any hints on collision detection (so the pacman/ghosts can't go through the walls) would be helpful.
4) Any hints on how the vacant space between the pipes will be calculated so the dots can be filled between them will also be very helpful.
Thanks
I wouldn't do it that way.
I'd draw the graphical map and then create a 2D data array which represents the map. The data map would be responsible for determining collisions, eating dots, where candy is and where the ghosts are. Once all the logic for everything is handled just use the 2D array to display everything in their proper pixel coordinates over the graphical map.
For example the user is pressing the left key. First you determine that pacman is at element 3, 3. Element 3, 2 contains information denoting a wall so you can implement the code to make him ignore the command.
EDIT:
Each element would represent about where a dot could be. For example:
No, looking at the board I would say the array would look something like this.
d,d,d,d,d,d,d,d,d,d,d,d,w,w,d,d,d,d,d,d,d,d,d,d,d,d
d,w,w,w,w,d,w,w,w,w,w,d,w,w,d,w,w,w,w,w,d,w,w,w,w,d
p,w,w,w,w,d,w,w,w,w,w,d,w,w,d,w,w,w,w,w,d,w,w,w,w,p
d,w,w,w,w,d,w,w,w,w,w,d,w,w,d,w,w,w,w,w,d,w,w,w,w,d
d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d
And so on. You might want to pick a more flexible data structure than just characters however since some areas need to contain a bunch of different information. IE even though the ghost spawning area is blank, pacman isn't allowed in there. The movement of the ghosts and pacman is different for the side escapes, the candy spawn point is a blank spot but if you want to remain flexible you'll want to denote where this is on a per map basis.
Another thing you'll want to remember is that pacman and the ghosts are often inbetween points so containing information that represents a percentage of a space they're taking up between 1,2 and 1,3 is important for collision detection as well as determining when you want to remove dots, powerups and candy from the board.
You can paint the map into a BufferedImage and just drawImage that on every paint(). You'll get quite reasonable performance this way.
If you are happy with the walls being solid, you can draw each square wall block with fillRect. If you wish to get the same look as in the picture, you need to figure how to draw the lines in the right way and use arcs for corners.
The Pacman game map is made of squares and Pacman and the ghosts always move from one square to the neighbouring square in an animated step (i.e. you press right, the pacman moves one square to the right). That means that collision detection is easy: simply don't allow moves to squares that are not empty.
I do not understand what you are trying to ask here.
1) Just to give my advice on redrawing. Something that you can do if you find redrawing the entire image is slow, is determine only the elements that have changed on the screen and redraw those. An approach for this would be the following: Determine the sprites that have moved. Determine (approximate) a rectangle around those sprites. Redraw those rectangles only. This way you are only refreshing parts of the screen and not the whole screen. This should result in an increase in performance over redrawing the entire screen.
The other answers have been reasonable for the other questions you have asked.

Categories

Resources