School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,098 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 4,826 people online right now. Registration is fast and FREE... Join Now!



how to change applet's line drawing color

how to change applet's line drawing color Rate Topic: -----

#1 kummu4help  Icon User is offline

  • D.I.C Head
  • Icon
  • View blog
  • Group: Contributors
  • Posts: 243
  • Joined: 04-August 08


Dream Kudos: 25

Post icon  Posted 10 April 2009 - 09:07 PM

hi,
i have an applet. this applet is allows to draw free hand lines. i will have a button called "RED". if i click on button, the applet should draw red lines.
can any one help me. i am trying with following

also i have a button called "text". if i click on text, then a text area should be displayed. but the problem is, until i drag the text area is not visible.

also i am displaying a font list. i want whenever the font is selected from dropdown, the corresponding font should be used in text area.

the following code doesn't changing the font in the textarea if i change the font.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

/** A simple applet using the Java 1.0 event handling model */
public class board extends Applet {
  private int lastx, lasty;	// Remember last mouse coordinates.
  Button btnErase,btnText,btnRed,btnBlue;		 // The Erase button.
  Graphics g;   // A Graphics object for drawing.
  int width,height;
  String fonts[];
  Choice fontList;
  Color background;
  GraphicsEnvironment ge;
  TextArea txtArea;
  Font boardFont;
  /** Initialize the button and the Graphics object */
	@Override
  public void init() {
	btnErase = new Button("Erase");
	btnText = new Button("text");
	btnRed = new Button("RED");
	btnBlue = new Button("BLUE");
	txtArea = new TextArea();
	txtArea.setVisible(false);
	txtArea.setRows(5);
	txtArea.setColumns(10);
	background = this.getBackground();
	g = this.getGraphics();
	width = this.getWidth();
	height = this.getHeight();
	ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
	fontList = new Choice();
	fonts = ge.getAvailableFontFamilyNames();
	createFontList();
	fontList.addItemListener(new fontListener());
	btnText.addActionListener(new textListener());
	btnRed.addActionListener(new colorListener(this,Color.red));
	btnErase.addActionListener(new eraseListener());
	this.add(btnErase);
	this.add(btnRed);
	this.add(fontList);	
	this.add(btnText);
	this.add(txtArea);

	//this.
	this.setPreferredSize(this.getPreferredSize());
	this.requestFocus();  // Ask for keyboard focus so we get key events
  }

	public void createFontList(){
		for(int idx=0;idx<fonts.length;idx++){
			fontList.add(fonts[idx]);
		}
	}
  /** Respond to mouse clicks */
	@Override
  public boolean mouseDown(Event e, int x, int y) {
	lastx = x; lasty = y;			 // Remember where the click was
	return true;
  }


  /** Respond to mouse drags */
	@Override
  public boolean mouseDrag(Event e, int x, int y) {
	g.setColor(Color.black);
	g.drawLine(lastx, lasty, x, y);   // Draw from last position to here
	lastx = x; lasty = y;			 // And remember new last position
	return true;
  }
  /** Respond to key presses: Erase drawing when user types 'e' */
	@Override
  public boolean keyDown(Event e, int key) {
	if ((e.id == Event.KEY_PRESS) && (key == 'e')) {
	  g.setColor(this.getBackground());
	  g.fillRect(0, 0, width, height);
	  return true;
	}
	else return false;
  }
  /** Respond to Button clicks: erase drawing when user clicks button */

	class textListener implements ActionListener{
		public void actionPerformed(ActionEvent ae){
			if(txtArea.isVisible()){
				txtArea.setVisible(false);
			}else{
				txtArea.setVisible(true);
			}
		}
	}

	class eraseListener implements ActionListener{
		public void actionPerformed(ActionEvent ae){
			g.setColor(background);
			g.fillRect(0,0,width, height);
			//repaint();
		}
	}

	class fontListener implements ItemListener{
		public void itemStateChanged(ItemEvent ie){
			boardFont = new Font(fontList.getSelectedItem(),Font.PLAIN,12);
			txtArea.setFont(boardFont);
			repaint();
		}
	}

	class colorListener implements ActionListener{
		Color color;
		colorListener(Applet a,Color c){
			color = c;
			a.setForeground(color);
		}
		public void actionPerformed(ActionEvent ae){
			g.setColor(color);
		}
	}
}



thanks for any help
Was This Post Helpful? 0
  • +
  • -


#2 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • Icon
  • Group: Expert w/DIC++
  • Posts: 1,187
  • Joined: 06-March 09


Dream Kudos: 150

Posted 10 April 2009 - 09:10 PM

Well, for the color you can just do g.setColor(new Color(int, int, int)); It is based off of the rgb scale, so go to paint or something and find a chart or something.
Was This Post Helpful? 0
  • +
  • -

#3 kummu4help  Icon User is offline

  • D.I.C Head
  • Icon
  • View blog
  • Group: Contributors
  • Posts: 243
  • Joined: 04-August 08


Dream Kudos: 25

Posted 10 April 2009 - 10:10 PM

thanks i am able to solve my drawing problem. now my code looks like this. i removed g.setColor(Color.black) statement from mouseDrag function. now i am able to draw lines in color that i want.

now i want to increase the line width. also i want to add erase functionality that we normally see in paint in windows. (different sizes of erasers)
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

/** A simple applet using the Java 1.0 event handling model */
public class board extends Applet {
  private int lastx, lasty;	// Remember last mouse coordinates.
  Button btnErase,btnText,btnRed,btnBlue;		 // The Erase button.
  Graphics g;   // A Graphics object for drawing.
  int width,height;
  String fonts[];
  Choice fontList;
  Color background;
  GraphicsEnvironment ge;
  TextArea txtArea;
  Font boardFont;
  /** Initialize the button and the Graphics object */
	@Override
  public void init() {
	btnErase = new Button("Erase");
	btnText = new Button("text");
	btnRed = new Button("RED");
	btnBlue = new Button("BLUE");
	txtArea = new TextArea();
	txtArea.setVisible(false);
	txtArea.setRows(5);
	txtArea.setColumns(10);
	background = this.getBackground();
	g = this.getGraphics();
	width = this.getWidth();
	height = this.getHeight();
	ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
	fontList = new Choice();
	fonts = ge.getAvailableFontFamilyNames();
	createFontList();
	fontList.addItemListener(new fontListener());
	btnText.addActionListener(new textListener());
	btnRed.addActionListener(new colorListener(this,Color.red));
	btnErase.addActionListener(new eraseListener());
	this.add(btnErase);
	this.add(btnRed);
	this.add(fontList);	
	this.add(btnText);
	this.add(txtArea);

	//this.
	this.setPreferredSize(this.getPreferredSize());
	this.requestFocus();  // Ask for keyboard focus so we get key events
  }

	public void createFontList(){
		for(int idx=0;idx<fonts.length;idx++){
			fontList.add(fonts[idx]);
		}
	}
  /** Respond to mouse clicks */
	@Override
  public boolean mouseDown(Event e, int x, int y) {
	lastx = x; lasty = y;			 // Remember where the click was
	return true;
  }


  /** Respond to mouse drags */
	@Override
  public boolean mouseDrag(Event e, int x, int y) {
   
	g.drawLine(lastx, lasty, x, y);   // Draw from last position to here
	lastx = x; lasty = y;			 // And remember new last position
	return true;
  }
  /** Respond to key presses: Erase drawing when user types 'e' */
	@Override
  public boolean keyDown(Event e, int key) {
	if ((e.id == Event.KEY_PRESS) && (key == 'e')) {
	  g.setColor(this.getBackground());
	  g.fillRect(0, 0, width, height);
	  return true;
	}
	else return false;
  }
  /** Respond to Button clicks: erase drawing when user clicks button */

	class textListener implements ActionListener{
		public void actionPerformed(ActionEvent ae){
			if(txtArea.isVisible()){
				txtArea.setVisible(false);
			}else{
				txtArea.setVisible(true);
			}
		}
	}

	class eraseListener implements ActionListener{
		public void actionPerformed(ActionEvent ae){
			g.setColor(background);
			g.fillRect(0,0,width, height);
			//repaint();
		}
	}

	class fontListener implements ItemListener{
		public void itemStateChanged(ItemEvent ie){
			boardFont = new Font(fontList.getSelectedItem(),Font.PLAIN,12);
			txtArea.setFont(boardFont);
			repaint();
		}
	}

	class colorListener implements ActionListener{
		Color color;
		colorListener(Color c){
			color = c;

		}
		public void actionPerformed(ActionEvent ae){
			g.setColor(color);
		}
	}
}



Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • Java Lover
  • Icon
  • Group: Mentors
  • Posts: 11,168
  • Joined: 06-March 08


Dream Kudos: 475

Posted 10 April 2009 - 10:18 PM

never never never do that

g = this.getGraphics();

Graphic g is a graphic context that might change/evolve with time

The only way to have the "actual" graphic context is to call repaint() that will call your paint() method with the actual (the thread that paints) Graphic context

Have instance variable to hold your "state" what you have to draw, which color, ....
save this state
call repaint()
use the Graphics context that is passed to your paint() method
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month