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

Start a new topic
Add Reply





MultiQuote
| 


