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.