0

The following program is supposed to allow free hand drawing on the one frame and the other as a button to open the color selector. The code for the main class can be found:

package clickTesting;

import Desksnap.Utils.Line;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.LinkedList;
import java.util.List;


public class Main {
    public static Color currentColor = null;

    public static void main(String[] args) {
        Viewer gui = new Viewer();
        button gui1 = new button();
        gui.setVisible(true);
        gui1.setVisible(true);
        boolean lol = true;
        while (lol) {
            gui.repaint(500);
        }
    }


    static class Viewer extends JFrame implements MouseMotionListener, MouseListener {
        public List<Line> lines = new LinkedList<Line>();
        private int index = 0;
        private Point[] arr = new Point[100000];

        public Viewer() {
            addMouseListener(this);
            addMouseMotionListener(this);
        }

        private void initComponents() {
            PhotoWindow = new JFrame();
            setUndecorated(true);
            {
                Container PhotoWindowContentPane = PhotoWindow.getContentPane();
                PhotoWindowContentPane.setLayout(null);
                PhotoWindowContentPane.setPreferredSize(new Dimension(410, 539));
                PhotoWindow.pack();
                PhotoWindow.setLocationRelativeTo(PhotoWindow.getOwner());
                addMouseListener(this);


            }
        }

        private JFrame PhotoWindow;


        @Override
        public void mousePressed(MouseEvent e) {
            arr[index] = new Point(e.getX(), e.getY());
            index++;
            System.out.println(index);
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            lines.add(new Line(Main.currentColor, arr));
            arr = new Point[100000];
            index = 0;
            System.out.println("Released");
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            arr[index] = new Point(e.getX(), e.getY());
            index++;
            System.out.println(index);
        }


        @Override
        public void paint(Graphics g) {
            super.paintComponents(g);
            if (lines.size() >= 0) {
                for (Line z : lines) {
                    if (z != null) {
                        Point[] lel = z.getLine();
                        g.setColor(z.getColor());
                        if (lel != null) {
                            for (int i = 0; i < lel.length; i++)
                                if (lel[i] != null) {
                                    g.drawLine(lel[i].x, lel[i].y, lel[i + 1].x, lel[i + 1].y);
                                }
                        } else {
                            System.out.println("Nope");
                        }
                    }
                }
            } else {
                System.out.println("Nothing In List Yet!");
            }
        }


        //Unused
        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseExited(MouseEvent e) {
        }

        @Override
        public void mouseMoved(MouseEvent e) {
        }

        @Override
        public void mouseClicked(MouseEvent e) {
        }
    }


    public static class button extends JFrame implements MouseMotionListener, MouseListener {
        public button() {
            this.addMouseListener(this);
            this.addMouseMotionListener(this);
        }

        private void initComponents() {
            PhotoWindow = new JFrame();
            setUndecorated(true);
            {
                Container PhotoWindowContentPane = PhotoWindow.getContentPane();
                PhotoWindowContentPane.setLayout(null);
                PhotoWindowContentPane.setPreferredSize(new Dimension(410, 539));
                PhotoWindow.pack();
                PhotoWindow.setLocationRelativeTo(PhotoWindow.getOwner());
                addMouseListener(this);
            }
        }

        private JFrame PhotoWindow;


        @Override
        public void mousePressed(MouseEvent e) {
            JColorChooser color = new JColorChooser();
            Main.currentColor = color.showDialog(
                    button.this,
                    "Choose Background Color",
                    Color.BLACK);
        }

        @Override
        public void mouseReleased(MouseEvent e) {
        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseExited(MouseEvent e) {
        }

        @Override
        public void mouseDragged(MouseEvent e) {
        }

        @Override
        public void mouseMoved(MouseEvent e) {
        }

        @Override
        public void mouseClicked(MouseEvent e) {
        }
    }
}

This class supports the drawing, recording of the lines and the main loop:

package Desksnap.Utils;

import java.awt.*;
import java.util.*;
import java.util.List;


public class Line {
    Color color = null;
    Point[] points = null;

 public Line(Color color, Point[] points){
     color = this.color;
     points = this.points;
 }

    public Color getColor(){
        return color;
    }
    public Point[] getLine(){
        return points;
    }


}

This is the line class used.

After drawing a line the program just prints 'Nope' into the console.

Output: Nope x 10000000

Please help me understand what I've done wrong.

halfer
  • 19,824
  • 17
  • 99
  • 186
Jordan
  • 11
  • 3
  • [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – MadProgrammer Oct 16 '14 at 04:18
  • See this `while (lol) {gui.repaint(500);}` this is `while (true) { System.out.println("Very Bad");}`! You do not control the painting process within Swing, Swing will tell you when it wants something painted and you will respond to that request. `repaint` is a "request" you make to the `RepaintManager` to inform it that you would like be update when it deems it appropriate. See [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html) and [Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/) for more details on the painting – MadProgrammer Oct 16 '14 at 04:20
  • haha :P Thanks for the edit/cleanup of the post, i'm new to the site and didn't really want to try and over complicate it by messing it all up :s – Jordan Oct 16 '14 at 04:21

1 Answers1

0

To start with, NEVER do this...

while (lol) {
    gui.repaint(500);
}

You do not control the paint process, that is the domain of the RepaintManager. If you want your component updated, use repaint ONCE and let the RepaintManager figure it out.

The core of your problem is here...

public Line(Color color, Point[] points) {
    color = this.color;
    points = this.points;
}

You are assigning the values of this.color to color and this.points to points, which has no effect. You should be assigning them the other way around (color to this.color)

You will, also want to take a closer look at:

Calling super.paintComponents from within paint is a bad idea, you should be calling super.paint and let it take care of the appropriate paint calls.

Actually, you shouldn't be overriding paint without a really good reason, espically of top level containers. Instead, start by extending your paint class from a JPanel and override it's paintComponent method instead.

You can then add this component to what ever container you want.

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thank-You very much! This fixed the nope problem and seems to override all problems leading up to it. The multi GUI is only until i put a button on the main one, this isn't the program i'm working on just an aspect of it in a very basic/bad form for the sake of tetsing. the only problem is now after drawring/releasing it won't draw on the frame anymore XD – Jordan Oct 16 '14 at 04:41
  • I see ^_^ worked for the first line but didn't draw any other's , just pulled errors – Jordan Oct 16 '14 at 05:58
  • You array is up to `10000` elements long, which is filled with `null` values to begin with. In you paint loop, you are not check to see if the next point is `null` (and you will need to check for `index + 1` as well)...This would be better server with a `LinkedList`... – MadProgrammer Oct 16 '14 at 06:01
  • Hey, dunno if you're still around but I have ran into another problem, your help so far has been wonderful and my project is going great: my current code: Main: http://pastebin.com/NTMkap80 Line: http://pastebin.com/mAK74gZs and my new error is: http://pastebin.com/8AjyCY2A Thank-you very much in advanced ! – Jordan Oct 16 '14 at 22:48
  • Edit: Removing all of the i+1 i-1's made the code error free and working fine but the lines are dotted and no actual solid lines, so close but yet so far...<3 plz halp – Jordan Oct 16 '14 at 22:56
  • Re Edit: I found the problem, silly mistake of calling a index which was non existant/inpossible, now all i need is to thicken this line ^_^ – Jordan Oct 16 '14 at 23:03
  • Yep, was going to mention grouping each two points to form a line, discard any mismatching pairs (so if you end up with a `Point` and `null` you'd throw it away) ;) – MadProgrammer Oct 16 '14 at 23:07