aspose file tools*
The moose likes Beginning Java and the fly likes HELP 5 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HELP 5" Watch "HELP 5" New topic
Author

HELP 5

Jose Carrillo
Greenhorn

Joined: Nov 13, 2003
Posts: 17
Hello I'm creating an applet that draw a line, rect, and oval. and I have create a another button that fill the rect and ovals. also I have to create 3 array that storage 10 shapes in that way the shapes will not be erase if I go to other page or something. here is my code so far:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class mouseDraw extends JApplet implements MouseListener, MouseMotionListener, ActionListener
{
private int mouseX, mouseY, mouseY1, mouseX1;
private JButton[] buttons = new JButton[3];
private JPanel controlPanel, drawPanel;
private int shape;
public void init()
{
buttons[0] = new JButton("Line");
buttons[1] = new JButton("Rectangle");
buttons[2] = new JButton("Oval");
buttons[0].setBackground(Color.red);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
controlPanel.setBackground(Color.orange);
drawPanel = new JPanel();
drawPanel.setBackground(new Color(0xffffcc));
for (int i=0; i<3; i++)
controlPanel.add(buttons[i]);
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
pane.add(controlPanel, BorderLayout.NORTH);
pane.add(drawPanel, BorderLayout.CENTER);
for (int i=0; i<3; i++)
buttons[i].addActionListener(this);
addMouseListener(this);
addMouseMotionListener(this);
}
public void paint(Graphics g)
{
super.paint(g);
int x = Math.min(mouseX1, mouseX);
int y = Math.min(mouseY1, mouseY);
g.setColor(Color.blue);
int w = (int)Math.abs(mouseX1 - mouseX);
int h = (int)Math.abs(mouseY1 - mouseY);
switch (shape)
{
case 0: g.drawLine(mouseX1, mouseY1, mouseX, mouseY);
break;
case 1: g.drawRect(x, y, w, h);
break;
case 2: g.drawOval(x, y, w, h);
break;
}
}
public void mouseMoved(MouseEvent e)
{
mouseX = e.getX();
mouseY = e.getY();
showStatus("Mouse is moved to (" + mouseX + mouseY + ")");
}
public void mouseDragged(MouseEvent e)
{
mouseX = e.getX();
mouseY = e.getY();
showStatus("Mouse is moved to (" + mouseX + mouseY + ")");
repaint();
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}

public void mouseExited(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
mouseX1 = mouseX;
mouseY1 = mouseY;
}
public void mouseClicked(MouseEvent e)
{
}
public void actionPerformed(ActionEvent e)
{
for (int i=0; i<3; i++)
{
buttons[i].setBackground(Color.lightGray);
if (e.getSource() == buttons[i])
{
shape = i;
buttons[i].setBackground(Color.red);
}
}
}
}
please Help me thanks
A. Wolf
Ranch Hand

Joined: Sep 28, 2003
Posts: 57
I just pulled out my trusty java book, and found a similar example. In the example they used two arrays of type Point(). one for all the beginnings and one for all the ends of lines, and a counter to keep track of which point is being drawn at the moment, and incremented. in the paint method its a simple for loop drawing all the previously drawn lines.

edit-
you have two "mouseExited" methods in that code.
public void mouseExited(MouseEvent e)
{}
[ November 23, 2003: Message edited by: A. Wolf ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: HELP 5