Using JColorChooser in processing to change drawing line color

My program only draws lines, I want to use JColorChooser to choose a line color as soon as I start drawing. can anyone give me a hand? I’ve got this so far.


import java.awt.*;
import javax.swing.*;



public static color colorsillo;
public static JColorChooser elegirColor = new JColorChooser ();
void setup() { //PRIMERO HACEMOS NUESTRO SETUP Y DEFINIMOS..
  size(400, 400); //...EL TAMAÑO DE LA VENTANA CON COORDENADAS X y Y


}


void draw() { 
  if (mousePressed) {
    
    colorsillo = elegirColor.showDialog (null,"Hola", colorsillo);
    
    line(pmouseX, pmouseY, mouseX, mouseY); 
    fill(255);
  }
   
}

Hi @Ideally,

This code works for me (hit a key to display the color picker):

import javax.swing.JColorChooser;
import java.awt.Color;
import javax.swing.JFrame;

color bgcolor = color(0,100,200,150);

void setup() {
  size(200, 200);
}

void draw() {
  background(0);
  fill(bgcolor);
  rect(0,0,width,height);
}

void keyPressed() {
  bgcolor = pickColor(bgcolor);
}

color pickColor(int c) {
  Color javaColor = new Color(c, true);
  javaColor  = JColorChooser.showDialog(null , "Java Color Chooser", javaColor); 
  if (javaColor != null ) {  
    c = (int)alpha(c) << 24 | (javaColor.getRed() << 16 ) | (javaColor.getGreen() << 8 ) | (javaColor.getBlue() << 0 );
    println( "Picked:" + hex(c) );
    return c;
  }
  return color(0);
}

The solution was found here: java - Using JColorChooser inside Processing 3.5 - Stack Overflow

1 Like

I think I’m almost there, JColorChooser it appears but line doesn’t change it’s color D:

import java.awt.*;
import javax.swing.*;

color bgcolor = color(0,100,200,150);


public static JColorChooser elegirColor = new JColorChooser ();
void setup() { //PRIMERO HACEMOS NUESTRO SETUP Y DEFINIMOS..
  size(400, 400); //...EL TAMAÑO DE LA VENTANA CON COORDENADAS X y Y
}

void keyPressed (){
  bgcolor = pickColor (bgcolor);
}

  
void draw() { 

  if (mousePressed && mouseButton==LEFT) {
    fill(bgcolor);
    line(pmouseX, pmouseY, mouseX, mouseY); 
    
  } 
}

color pickColor(int c) {
  Color javaColor = new Color(c, true);
  javaColor  = JColorChooser.showDialog(null , "Java Color Chooser", javaColor); 
  if (javaColor != null ) {  
    c = (int)alpha(c) << 24 | (javaColor.getRed() << 16 ) | (javaColor.getGreen() << 8 ) | (javaColor.getBlue() << 0 );
    println( "Color Elegido:" + hex(c) );
    return c;
  }
  return color(0);
}

I finished my program, it turns out that to change the color of a line you need to use the stroke () command.

So even though the color window appeared I was not using the right method, I simply changed the fill to stroke.

Thank you very much!

I hope this will help many people in the future.

This is the finished code:


import java.awt.*;
import javax.swing.*;


color lineColor = color(0,100,200,150);

void setup() { 
  size(400, 400);
}

void draw() {
  
  
  if (mousePressed && mouseButton==LEFT) {
    
    stroke(lineColor);
    line(pmouseX, pmouseY, mouseX, mouseY); 

  }}

void keyPressed (){
  lineColor = pickColor (lineColor);
}

color pickColor(int c) {
  Color javaColor = new Color(c, true);
  javaColor  = JColorChooser.showDialog(null , "Java Color Chooser", javaColor); 
  if (javaColor != null ) {  
    c = (int)alpha(c) << 24 | (javaColor.getRed() << 16 ) | (javaColor.getGreen() << 8 ) | (javaColor.getBlue() << 0 );
    println( "Color Elegido:" + hex(c) );
    return c;
  }
  return color(0);
}
2 Likes

I didn’t know about this JColorChooser color picker :hushed: that’s cool!

1 Like