help with buttons

hello,

I'm new in java and i need some help with buttons and their actions.

I need to insert buttons to perform three actions (draw lines). can anybody help?

import javax.swing.*;

import java.awt.*;

import java.awt.geom.*;

import java.awt.event.*;

publicclass antrasextends JApplet{

private Rectangle2D Shape4;

public antras (){

setBackground( Color.black );

Shape4 =new Rectangle2D.Float (10.F, 10.F, 480.F, 280.F);

}

publicvoid paint (Graphics g){

Graphics2D g2D;

boolean Contained;

g2D = (Graphics2D) g;

g2D.setColor(Color.white);

g2D.fill (Shape4);

Contained = Shape4.intersectsLine (400., 10., 480., 70.);

//first action

g2D.setColor (Color.green);

g2D.drawLine (10, 100, 489, 100);

//second action

g2D.setStroke(new BasicStroke(1f, BasicStroke.CAP_BUTT,

BasicStroke.JOIN_ROUND,

1f,newfloat[]{2f},0f));

g2D.setColor (Color.green);

g2D.drawLine (10, 140, 490, 140);

//third action

g2D.setStroke(new BasicStroke(1f, BasicStroke.CAP_BUTT,

BasicStroke.JOIN_ROUND,

1f,newfloat[]{5f},0f));

g2D.setColor (Color.green);

g2D.drawLine (10, 200, 490, 200);

}

}

[2283 byte] By [podoa] at [2007-11-27 0:27:50]
# 1

First you need to declare and add a button, then a action listener to that button. I it is clicked the action listener notifies the action performed method and you can set a flag.

boolean button1pressed;

Button button1 = new Button("button 1");

b1.addActionListener(this);

public void actionPerformed(ActionEvent e){

if(e.getSource() == button1)

{

button1pressed=true;

repaint();

}

}

and inside your paint() method

if (button1pressed=true)

{

//first action

g2D.setColor (Color.green);

g2D.drawLine (10, 100, 489, 100);

}

else if (button2pressed=true)

{

//second action

}

Hard_Wireda at 2007-7-11 22:28:10 > top of Java-index,Java Essentials,New To Java...