Change drawLine Color

how do you change the color of a line drawn with drawLine

Page 1 of 1

11 Replies - 30422 Views - Last Post: 28 April 2009 - 06:40 PM Rate Topic: -----

#1 cmoney12051  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 11-March 09

Change drawLine Color

Posted 27 April 2009 - 04:56 PM

How do i change the color of the lines i'm creating. its a drawing app, and when it makes i line i want to click on a color and have the lines change to that color. this is what i have so far

import java.awt.*;
import java.awt.Color;
import java.lang.Math;
import java.awt.event.*;
import java.awt.Graphics;
import java.applet.Applet;

public class draw extends Applet implements ActionListener, MouseListener,
MouseMotionListener {

Button bDraw, bLine, bOval, bRect, bRounded,bSelect1,bSelect2,bSelect3,bSelect4,bHeading;
Point dot[] = new Point[1000];
Point start, end;
int dots = 0;
boolean mouseUp = false;
boolean draw = false;
boolean line = false;
boolean oval = false;
boolean rectangle = false;
boolean rounded = false;
Font font;


public void init()
{

setLayout(null);
font= new Font("Monospaced", Font.BOLD, 24);
bHeading = new Button("MY PAINT APPLICATION");
bHeading.setSize(500,40);
bHeading.setLocation(0,0);
bHeading.setFont(font);
bHeading.setForeground(Color.white.brighter());
bHeading.setBackground(Color.blue.brighter());

setLayout(null);
bLine = new Button("Line");
bLine.setSize(100,30);
bLine.setLocation(0,40);
bLine.setForeground(Color.blue.brighter());
bLine.setFont(new Font("Arial", Font.BOLD, 16));

bOval = new Button("Circle");
bOval.setSize(100,30);
bOval.setLocation(100,40);
bOval.setForeground(Color.green.brighter());
bOval.setFont(new Font("Arial", Font.BOLD, 16));

bRect = new Button("Rectangles");
bRect.setSize(100,30);
bRect.setLocation(200,40);
bRect.setForeground(Color.red.brighter());
bRect.setFont(new Font("Arial", Font.BOLD, 16));

bRounded = new Button("Ellipse");
bRounded .setSize(100,30);
bRounded .setLocation(300,40);
bRounded.setForeground(Color.blue.brighter());
bRounded.setFont(new Font("Arial", Font.BOLD, 16));

bDraw = new Button("Free Draw");
bDraw.setSize(100,30);
bDraw.setLocation(400,40);
bDraw.setForeground(Color.yellow.brighter());
bDraw.setFont(new Font("Arial", Font.BOLD, 16));

setLayout(null);
bSelect1 = new Button("Red");
bSelect1.setSize(100,30);
bSelect1.setLocation(0,80);
bSelect1.setForeground(Color.white.brighter());
bSelect1.setBackground(Color.RED.brighter());
bSelect1.setFont(new Font("Arial", Font.BOLD, 16));

bSelect2 = new Button("Green");
bSelect2.setSize(100,30);
bSelect2.setLocation(100,80);
bSelect2.setForeground(Color.white.brighter());
bSelect2.setBackground(Color.green.brighter());
bSelect2.setFont(new Font("Arial", Font.BOLD, 16));

setLayout(null);
bSelect3 = new Button("Blue");
bSelect3.setSize(100,30);
bSelect3.setLocation(200,80);
bSelect3.setForeground(Color.white.brighter());
bSelect3.setBackground(Color.BLUE.brighter());
bSelect3.setFont(new Font("Arial", Font.BOLD, 16));

bSelect4 = new Button("Yellow");
bSelect4.setSize(100,30);
bSelect4.setLocation(300,80);
bSelect4.setForeground(Color.white.brighter());
bSelect4.setBackground(Color.YELLOW.brighter());
bSelect4.setFont(new Font("Arial", Font.BOLD, 16));

add(bLine);
add(bOval);
add(bRect);
add(bRounded);
add(bDraw);
add(bSelect1);
add(bSelect2);
add(bSelect3);
add(bSelect4);
add(bHeading);

bLine.addActionListener(this);
bOval.addActionListener(this);
bRect.addActionListener(this);
bRounded.addActionListener(this);
bDraw.addActionListener(this);
addMouseListener(this);
bSelect1.addActionListener(this);
bSelect2.addActionListener(this);
bSelect3.addActionListener(this);
bSelect4.addActionListener(this);
addMouseMotionListener(this);
}
public void mousePressed(MouseEvent e)
{
mouseUp = false;
start = new Point(e.getX(), e.getY());
}
public void mouseReleased(MouseEvent e)
{
if(line){
end = new Point(e.getX(), e.getY());
} else {
end = new Point(Math.max(e.getX(), start.x),
Math.max(e.getY(), start.y));
start = new Point(Math.min(e.getX(), start.x),
Math.min(e.getY(), start.y));
}
mouseUp = true;
repaint();
}
public void mouseDragged(MouseEvent e)
{
if(draw){
dot[dots] = new Point(e.getX(), e.getY());
dots++;
repaint();
}
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
public void paint (Graphics g)
{
if (mouseUp) {
int width = end.x - start.x;
int height = end.y - start.y;
if(line){
g.drawLine(start.x, start.y, end.x, end.y);
}
else if(oval){
g.drawOval(start.x, start.y, width, height);
}
else if(rectangle){
g.drawRect(start.x, start.y, width, height);
}
else if(rounded){
g.drawRoundRect(start.x, start.y, width, height, 10, 10);
}
else if(draw){
for(int loop_index = 0; loop_index < dots - 1;
loop_index++){
g.drawLine(dot[loop_index].x, dot[loop_index].y,
dot[loop_index + 1].x, dot[loop_index + 1].y);
}
}
}
}
public void actionPerformed(ActionEvent e)
{
setFlagsFalse();
if(e.getSource() == bDraw)draw = true;
if(e.getSource() == bLine)line = true;
if(e.getSource() == bOval)oval = true;
if(e.getSource() == bRect)rectangle = true;
if(e.getSource() == bRounded)rounded = true;
if(e.getSource() == bSelect1)
{
	
}
}

void setFlagsFalse()
{
rounded = false;
line = false;
oval = false;
rectangle = false;
draw = false;
}
}


I would like to be able to draw the image then click one of the buttons i have place and have it change the color of all the lines in the image.

This post has been edited by cmoney12051: 27 April 2009 - 07:30 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Change drawLine Color

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Change drawLine Color

Posted 27 April 2009 - 08:30 PM

it is usually in the paint() method that on the Gaphics handled g you do:
g.setColor(Color x);
Was This Post Helpful? 1
  • +
  • -

#3 cmoney12051  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 11-March 09

Re: Change drawLine Color

Posted 27 April 2009 - 08:34 PM

how can i do that by clicking one of my buttons

like this one

setLayout(null);
bSelect1 = new Button("Red");
bSelect1.setSize(100,30);
bSelect1.setLocation(0,80);
bSelect1.setForeground(Color.white.brighter());
bSelect1.setBackground(Color.RED.brighter());
bSelect1.setFont(new Font("Arial", Font.BOLD, 16));



i know it must be in the public void actionPerformed(ActionEvent e)
but how would i code that?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Change drawLine Color

Posted 27 April 2009 - 08:40 PM

View Postcmoney12051, on 27 Apr, 2009 - 07:34 PM, said:

how can i do that by clicking one of my buttons

like this one

setLayout(null);
bSelect1 = new Button("Red");
bSelect1.setSize(100,30);
bSelect1.setLocation(0,80);
bSelect1.setForeground(Color.white.brighter());
bSelect1.setBackground(Color.RED.brighter());
bSelect1.setFont(new Font("Arial", Font.BOLD, 16));



i know it must be in the public void actionPerformed(ActionEvent e)
but how would i code that?

no... sorry

public void paint(Graphics g) {
   g.setColor(Color.RED);
   // now I will draw in RED


Was This Post Helpful? 0
  • +
  • -

#5 cmoney12051  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 11-March 09

Re: Change drawLine Color

Posted 28 April 2009 - 04:28 AM

That works but how do i get that to change by me clicking a different color. i have the applet set up with 4 buttons i can click with 4 different colors, so if i click the red button the lines all turn re, if i click blue, they all turn blue, and yellow, they all turn yellow, green they all turn green.
Was This Post Helpful? 0
  • +
  • -

#6 cmoney12051  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 11-March 09

Re: Change drawLine Color

Posted 28 April 2009 - 01:41 PM

ok well now i have this

public void paint (Graphics g)
{
	

	if(red = true)
	{
		g.setColor(Color.RED);
	}
	else
	{
		g.setColor(Color.black);
	}
	
   
if (mouseUp) {
int width = end.x - start.x;
int height = end.y - start.y;
if(line){
g.drawLine(start.x, start.y, end.x, end.y);
}
else if(oval){
g.drawOval(start.x, start.y, width, height);
}
else if(rectangle){
g.drawRect(start.x, start.y, width, height);
}
else if(rounded){
g.drawRoundRect(start.x, start.y, width, height, 10, 10);
}
else if(draw){
for(int loop_index = 0; loop_index < dots - 1;
loop_index++){
g.drawLine(dot[loop_index].x, dot[loop_index].y,
dot[loop_index + 1].x, dot[loop_index + 1].y);
}

}
}
}

public void actionPerformed(ActionEvent e)
{
setFlagsFalse();
if(e.getSource() == bDraw)draw = true;
if(e.getSource() == bLine)line = true;
if(e.getSource() == bOval)oval = true;
if(e.getSource() == bRect)rectangle = true;
if(e.getSource() == bRounded)rounded = true;
if(e.getSource() == bSelect1)red = true;


}

void setFlagsFalse()
{
rounded = false;
line = false;
oval = false;
rectangle = false;
draw = false;
red = false;
blue = false;
}
}


but it is not working right. i would like to to only turn red when i click the red button, but instead it always turns red, even if i didn't click red. what should i do?

This post has been edited by cmoney12051: 28 April 2009 - 01:42 PM

Was This Post Helpful? 0
  • +
  • -

#7 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Change drawLine Color

Posted 28 April 2009 - 04:48 PM

if(red = true) Needs to be ==
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Change drawLine Color

Posted 28 April 2009 - 05:02 PM

No need for setFlagsFalse :D

public void actionPerformed(ActionEvent e)
{

draw = e.getSource() == bDraw;
line = e.getSource() == bLine;
oval = e.getSource() == bOval;
.....
}

Was This Post Helpful? 0
  • +
  • -

#9 cmoney12051  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 11-March 09

Re: Change drawLine Color

Posted 28 April 2009 - 06:16 PM

i changed the
if(red = true)
to == and it makes the lines black, and when i click the red button it does nothing. and if i remove the setFlagsFalse it makes none of the buttons work. nle the line button, so im still stuck on how to get the line color to change.

This post has been edited by cmoney12051: 28 April 2009 - 06:26 PM

Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Change drawLine Color

Posted 28 April 2009 - 06:26 PM

View Postcmoney12051, on 28 Apr, 2009 - 05:16 PM, said:

i changed the [icode]if(red = true)[/icode] to == and it makes the lines black, and when i click the red button it does nothing. and if i remove the setFlagsFalse it makes none of the buttons work. nle the line button, so im still stuck on how to get the line color to change.

did you change if red = true for a double ==

Why don't you make a color variable:

Color colorToUse;

In your actionPerformed for your buttons:

colorToUse = Color.RED;

in paint()

g.setColor(colorToUse);
Was This Post Helpful? 0
  • +
  • -

#11 cmoney12051  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 11-March 09

Re: Change drawLine Color

Posted 28 April 2009 - 06:34 PM

That worked thanks. i was unaware of the color varibable

This post has been edited by cmoney12051: 28 April 2009 - 06:40 PM

Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Change drawLine Color

Posted 28 April 2009 - 06:40 PM

Color colorToUse;

not String colorToUse... you cannot g.setColor(String) :)

in you instance variables a the top of your class so both the ActionListener and the paint() method will see it
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1