Can the program detect when one drawing stops and another starts?when I stop drawing, and start in another
If so, the current ArrayList is finished being updated and another ArrayList for the new drawing needs to be created.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Can the program detect when one drawing stops and another starts?when I stop drawing, and start in another
If so, the current ArrayList is finished being updated and another ArrayList for the new drawing needs to be created.
If you don't understand my answer, don't ignore it, ask a question.
Norm I did, what you said. I created ArrayList of ArrayLists (of Points).
Here is my code:
import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.util.ArrayList; import javax.swing.JPanel; public class Drawing extends JPanel implements MouseListener, MouseMotionListener { private int x, y; private ArrayList<ArrayList<Point>> Lists = new ArrayList<ArrayList<Point>>(); public Drawing() { setBackground(Color.WHITE); addMouseListener(this); addMouseMotionListener(this); } public void paintComponent(Graphics g) { super.paintComponent(g); Color colour = GUI.getColour(); g.setColor(colour); ((Graphics2D) g).setStroke(new BasicStroke(2)); for(int i=0; i<Lists.size()-1; i++) { ArrayList<Point> p_list = Lists.get(i); for(int j=0; j<=p_list.size()-2; j++) { Point p1, p2 = null; p1 = p_list.get(j); p2 = p_list.get(j+1); g.drawLine(p1.x, p1.y, p2.x, p2.y); } } } public void mouseDragged(MouseEvent e) { x = e.getX(); y = e.getY(); Lists.get(Lists.size()-1).add(new Point(x, y)); repaint(); } public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) { x = e.getX(); y = e.getY(); Lists.get(Lists.size()-1).add(new Point(x, y)); repaint(); } public void mouseMoved(MouseEvent e) {} public void mouseClicked(MouseEvent e) { ArrayList<Point> pointsList = new ArrayList<Point>(); Lists.add(pointsList); x = e.getX(); y = e.getY(); Lists.get(Lists.size()-1).add(new Point(x, y)); repaint(); } public void mouseEntered(MouseEvent e) {} }
Why does not it work properly?
There are no warnings, but while compiling, there are lots of errors:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1 at java.util.ArrayList.elementData(Unknown Source) at java.util.ArrayList.get(Unknown Source) at Menu.DrawingArea.mouseReleased(DrawingArea.java:54) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$400(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
Thanks for understanding and your help!
The posted error is not a compiler error. That error happened when the code was executed.while compiling, there are lots of errors:
The code at line 54 uses an index (-1) that is out of bounds. Look at the code on line 54 and see why it used that invalid index.Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.elementData(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at Menu.DrawingArea.mouseReleased(DrawingArea.java:54 )
One problem with the code is that there are no comments in it describing what it is going to do and how it is going to do it.
If you don't understand my answer, don't ignore it, ask a question.
Norm, I don't see it. Hmm, maybe when I add the first item to the list, then it can't "appeal" to him? I have no idea how to solve it.
Try debugging the code by adding lots of println() statements that print out messages when all the methods are executed. Include the size of the arraylists in all the messages so you can see what the computer ssees when the code is executed. The print outs will show you what is happening.I don't see it.
If you don't understand my answer, don't ignore it, ask a question.
Norm, it stops beforeI cant handle with it... what is the solution?g.drawLine(p1.x, p1.y, p2.x, p2.y);
What was printed out by the println() statements I suggested that you add to all the methods so you can see what the computer sees when it executes?
You need to find out why the exception happens so it can be fixed. Why is the index -1?it stops
If you don't understand my answer, don't ignore it, ask a question.
fkmk (May 31st, 2014)
Norm, I have changed the code, and now there are no errors, but it still connects last Point with another from new List, so while drawing after Mouse Release and again Mouse Click last Point connects with new one from new Mouse Click.
Here is the code:
import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import javax.swing.JPanel; import GUI.GUI; import java.awt.Graphics2D; import java.util.ArrayList; public class Drawing extends JPanel implements MouseListener, MouseMotionListener { public Drawing() { setBackground(Color.WHITE); addMouseListener(this); addMouseMotionListener(this); } private int x, y; private ArrayList<ArrayList<Point>> Lists = new ArrayList<ArrayList<Point>>(); public void paintComponent(Graphics g) { super.paintComponent(g); Color colour = GUI.getColour(); g.setColor(colour); ((Graphics2D) g).setStroke(new BasicStroke(2)); for(int i=0; i<Lists.size(); i++) { ArrayList<Point> p_list = Lists.get(i); for(int j=0; j<p_list.size()-1; j++) { Point p1, p2 = null; p1 = p_list.get(j); p2 = p_list.get(j+1); g.drawLine(p1.x, p1.y, p2.x, p2.y); } } } public void mouseDragged(MouseEvent e) { x = e.getX(); y = e.getY(); Lists.get(Lists.size()-1).add(new Point(x, y)); repaint(); } public void mousePressed(MouseEvent e) { ArrayList<Point> pointsList = new ArrayList<Point>(); Lists.add(pointsList); x = e.getX(); y = e.getY(); Lists.get(Lists.size()-1).add(new Point(x, y)); repaint(); } public void mouseReleased(MouseEvent e) { x = e.getX(); y = e.getY(); Lists.get(Lists.size()-1).add(new Point(x, y)); repaint(); } public void mouseMoved(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} }
Could you take a look at this? Thank you for you engagement!
EDIT:// Solved it! Pasted correct code. Thank you so much for your answers! I appreciate it
Last edited by fkmk; May 31st, 2014 at 04:21 PM.
Where are the comments in the code that describe the logic that the program is using to do its job? As requested in post#28
If you don't understand my answer, don't ignore it, ask a question.
fkmk (May 31st, 2014)
Norm, I have deleted it, because I solved the problem. Thank you so much for all your help! I owe you a lot!
Glad you solved the problem.
If you don't understand my answer, don't ignore it, ask a question.
fkmk (May 31st, 2014)
Hi guys! I have another problem...
I have two classes:
import java.awt.Color; import javax.swing.JPanel; import Toolkit.Pencil; public class Drawing extends JPanel { private Pencil pencil; public Drawing() { setBackground(Color.WHITE); pencil = new Pencil(); addMouseListener(pencil); addMouseMotionListener(pencil); } }
and
import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.util.ArrayList; import GUI.Drawing; import GUI.GUI; public class Pencil implements MouseListener, MouseMotionListener { private int x, y; private ArrayList<ArrayList<Point>> pointsList = new ArrayList<ArrayList<Point>>(); public void paintComponent(Graphics g) { super.paintComponent(g); ((Graphics2D) g).setStroke(new BasicStroke(2)); for(int i=0; i<pointsList.size(); i++) { ArrayList<Point> p_list = pointsList.get(i); for(int j=0; j<p_list.size()-1; j++) { Color colour = GUI.getColour(); g.setColor(colour); Point p1, p2 = null; p1 = p_list.get(j); p2 = p_list.get(j+1); g.drawLine(p1.x, p1.y, p2.x, p2.y); } } } public void mousePressed(MouseEvent e) { ArrayList<Point> Points = new ArrayList<Point>(); pointsList.add(Points); x = e.getX(); y = e.getY(); pointsList.get(pointsList.size()-1).add(new Point(x, y)); repaint(); } public void mouseDragged(MouseEvent e) { x = e.getX(); y = e.getY(); pointsList.get(pointsList.size()-1).add(new Point(x, y)); repaint(); } public void mouseReleased(MouseEvent e) { x = e.getX(); y = e.getY(); pointsList.get(pointsList.size()-1).add(new Point(x, y)); repaint(); } public void mouseMoved(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} }
Class Drawing is something like "Drawing Area" and Pencil is specific tool to draw in this Area. By default there is Pencil.
Compiler does not see repaint() method in the Pencil class.
I have been trying lots of ways to solve it, included extending Drawing() to Pencil, but then there is a problem with StackOverflowError.
Any ideas?
If you have errors, copy the error messages and paste it here. If its huge from a stack overflow only copy and paste enough to see where the recursive calls are.
What does that mean?Compiler does not see repaint()
Hint: Always put a @Override annotation statement before any method that a class overrides to allow the compiler to find errors.
If you don't understand my answer, don't ignore it, ask a question.
For above code compiler shows:
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: The method repaint() is undefined for the type Pencil at Toolkit.Pencil.mousePressed(Pencil.java:45) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$400(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
For code with Pencil extending Drawing() compiler shows:
Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError at java.awt.Component.setForeground(Unknown Source) at javax.swing.JComponent.setForeground(Unknown Source) at javax.swing.LookAndFeel.installColors(Unknown Source) at javax.swing.LookAndFeel.installColorsAndFont(Unknown Source) at javax.swing.plaf.basic.BasicPanelUI.installDefaults(Unknown Source) at javax.swing.plaf.basic.BasicPanelUI.installUI(Unknown Source) at javax.swing.JComponent.setUI(Unknown Source) at javax.swing.JPanel.setUI(Unknown Source) at javax.swing.JPanel.updateUI(Unknown Source) at javax.swing.JPanel.<init>(Unknown Source) at javax.swing.JPanel.<init>(Unknown Source) at javax.swing.JPanel.<init>(Unknown Source) at GUI.Drawing.<init>(Drawing.java:21) at Toolkit.Pencil.<init>(Pencil.java:18)
That error seems to say what the problem is: Pencil does NOT have a repaint() method.The method repaint() is undefined for the type Pencil
That looks like an execution error, not a compiler error. The first error you posted was a compiler error. IDEs are trying to muddle and lose the concepts of compiling and execution. I have no idea why they are doing that. I find the concepts useful.For code with Pencil extending Drawing() compiler shows:
If that is all the error message, I don't see why there is a stack overflow.
If you don't understand my answer, don't ignore it, ask a question.
Norm, yes, Pencil does not have repaint() method, but I need Pencil have it, without extending it with JPanel.
What would it do?
Did you add the @Override statement before the methods that Pencil is trying to override?
If you don't understand my answer, don't ignore it, ask a question.
Norm, I have added @Override adnotation before each MouseListener, and MouseMotionListener method already, but it still does not work, same problem occures. repaint() is undefined for the type Pencil. Maybe I did it wrong? :/ I don't know.
Last edited by fkmk; June 1st, 2014 at 07:46 AM.
Where is the repaint() method defined? If it is not defined you can't call it. That's very basic java.
What about the other methods? For example: paintComponent()added @Override adnotation before each MouseListener, and MouseMotionListener method
It looks like you need to do some reading about how java works. These are some basic concepts that you need to understand.
There are lots of good topics here: The Really Big Index
If you don't understand my answer, don't ignore it, ask a question.
Norm, thank you! But I just wonder if there is a way to connect these to classes (Drawing and Pencil).
Define what each of those classes is supposed to do.
If Drawing has the GUI stuff, and Pencil has the logic. Have method(s) in the Drawing class call method(s) in the Pencil class and pass a reference to the Graphics object that is passed to the paintComponent() method that is overridden in the Drawing class.
If you don't understand my answer, don't ignore it, ask a question.
fkmk (June 2nd, 2014)
Norm thank you, I have solved it!
But another problem has occured...
The problem is, that after pressing buttons, there is no action in JPanel. Take a look at this:
Here is my code for calling buttons (Pencil, Paintbrush etc.):
if(action.getSource() == newDrawingArea) { drawingArea = new Drawing(); } if(action.getSource() == pencilButton) { Drawing.pencil = new Pencil(drawingArea); pencil_condition = true; lines_condition = false; paintbrush_condition = false; Drawing.paintbrush = null; Drawing.lines = null; } if(action.getSource() == linesButton) { Drawing.lines = new Lines(drawingArea); lines_condition = true; pencil_condition = false; paintbrush_condition = false; Drawing.paintbrush = null; Drawing.pencil = null; } if(action.getSource() == paintbrushButton) { Drawing.paintbrush = new Paintbrush(drawingArea); paintbrush_condition = true; lines_condition = false; pencil_condition = false; Drawing.lines = null; Drawing.pencil = null; }
The class, where Objects should create is:
As you can see, I have added Boolean type conditions, but it does not work properly.public class Drawing extends JPanel { public static Pencil pencil; public static Lines lines; public static Paintbrush paintbrush; public Drawing() { setBackground(Color.WHITE); pencil = new Pencil(this); GUI.pencil_condition = true; addMouseListener(pencil); addMouseMotionListener(pencil); addMouseListener(lines); addMouseMotionListener(lines); addMouseListener(paintbrush); addMouseMotionListener(paintbrush); this.repaint(); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); if(GUI.pencil_condition == true) { GUI.lines_condition = false; GUI.paintbrush_condition = false; paintbrush = null; lines = null; pencil.paintComponent(g); } if(GUI.lines_condition == true) { System.out.println("include lines!"); GUI.pencil_condition = false; GUI.paintbrush_condition = false; paintbrush = null; pencil = null; lines.paintComponent(g); } if(GUI.paintbrush_condition == true) paintbrush.paintComponent(g); } }
How can I fix it?
Thanks for your help!
Last edited by fkmk; June 2nd, 2014 at 04:52 PM.
Don't try to save space by putting more than one statement on a line. It makes the code hard to read.
Please explain.it does not work properly.
If you don't understand my answer, don't ignore it, ask a question.
Norm Ok, sorry.
I changed the code a bit:
Here is a part code of calling specific tools in one class:
static int warunek; if(action.getSource() == newDrawingArea) { drawingArea = new Drawing(); } if(action.getSource() == pencilButton) { setWarunek(1); } if(action.getSource() == linesButton) { setWarunek(2); } if(action.getSource() == paintbrushButton) { setWarunek(3); } public void setWarunek(int warunek) { GUI.warunek = warunek; }
and another class od Drawing panel:
public class Drawing extends JPanel { private Tool pencil; private Tool lines; private Tool paintbrush; public Drawing() { pencil = new Pencil(this); addMouseListener(pencil); addMouseMotionListener(pencil); lines = new Lines(this); addMouseListener(lines); addMouseMotionListener(lines); paintbrush = new Paintbrush(this); addMouseListener(paintbrush); addMouseMotionListener(paintbrush); setBackground(Color.WHITE); this.repaint(); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); if(GUI.warunek == 1) pencil.paintComponent(g); if(GUI.warunek == 2) lines.paintComponent(g); if(GUI.warunek == 3) paintbrush.paintComponent(g); } }
After launching program, pencil is selected and drawing is possible, but only after selecting other button, e.g lines, previous "drawing" disappear.
EDIT:// Ok, I know, why it crashes, but it still does not work, as I want. After pressing button previous "picture" does not save.
Sorry, I don't understand.After pressing button previous "picture" does not save.
What is previous picture?
What does save mean? What is being saved? Where is it being saved?
If you don't understand my answer, don't ignore it, ask a question.