#1
Ok, so i have some code here, and i hope you can help me....what i need to do is find a way that i can draw a circle when the circle button is selected, and a square when square is selected...and what ive got now is being able to draw a square no matter what is selected....hopefully someone knows java here *crosses fingers*


import javax.swing.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.border.*;
/**
* Demonstrate how to draw a box
*/
public class PaintProgram extends JApplet
{
private int currentX = 0; // Mouse cursor's X position
private int currentY = 0; // Mouse cursor's Y position
private int width = 0; // The rectangle's width
private int height = 0; // The rectangle's height

/**
* Hook it up with the listener
*/
public void init() {
// Add a mouse listener and a mouse motion listener.
addMouseListener(new MyMouseListener());
addMouseMotionListener(new MyMouseMotionListener());

JPanel top = new JPanel();
JRadioButton circle = new JRadioButton("circle");
JRadioButton square = new JRadioButton("square");
top.add(circle);
top.add(square);
DrawPanel bottom = new DrawPanel();


setLayout(new GridLayout(2,1));
add(top);
add(bottom);



}


/**
* Responds to mouse events
*/
private class MyMouseListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
// Get the coordinates of the mouse cursor.
currentX = e.getX();
currentY = e.getY();
}
}

/**
* Responds to mouse motion events
*/
private class MyMouseMotionListener extends MouseMotionAdapter {
public void mouseDragged(MouseEvent e) {
// Calculate the size of the rectangle.
width = e.getX() - currentX;
height = e.getY() - currentY;

// Repaint the window.
repaint();
}
}


class DrawPanel extends JPanel {


public void paint(Graphics g) {
// Call the superclass's paint method.
super.paint(g);




}
}
}
:stickpoke

schiggity schiggity schwaaaaaam
#4
I haven't worked with the graphic aspects of java yet. I'm just now getting into arrays. Sorry, bud.
#5
You need to create an ActionListener class that includes an action that corresponds to if circle is selected else if square is selected, and in each of the possible selections, use the appropriate methods to draw a circle or a square using the information in the MouseListener and MouseMotionListener classes.

Always use the Java API for help.

By the way, you imported one too many times for a couple sets up on top.
mmmk.
Last edited by sithian476 at Nov 28, 2007,