Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 306,825 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,756 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

kummu4help

10 Apr, 2009 - 09:07 PM
Post #1

D.I.C Head
Group Icon

Joined: 4 Aug, 2008
Posts: 234



Thanked: 4 times
Dream Kudos: 25
My Contributions
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.
CODE

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

User is offlineProfile CardPM
+Quote Post


Fuzzyness

RE: How To Change Applet's Line Drawing Color

10 Apr, 2009 - 09:10 PM
Post #2

Comp Sci Student
Group Icon

Joined: 6 Mar, 2009
Posts: 1,183



Thanked: 181 times
Dream Kudos: 150
My Contributions
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.

User is offlineProfile CardPM
+Quote Post

kummu4help

RE: How To Change Applet's Line Drawing Color

10 Apr, 2009 - 10:10 PM
Post #3

D.I.C Head
Group Icon

Joined: 4 Aug, 2008
Posts: 234



Thanked: 4 times
Dream Kudos: 25
My Contributions
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)
CODE

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);
        }
    }
}


User is offlineProfile CardPM
+Quote Post

pbl

RE: How To Change Applet's Line Drawing Color

10 Apr, 2009 - 10:18 PM
Post #4

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,962



Thanked: 1187 times
Dream Kudos: 450
My Contributions
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
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/20/09 10:38PM

Live Java Help!

Be Social

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

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month