aspose file tools*
The moose likes Beginning Java and the fly likes Help 3 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help 3" Watch "Help 3" New topic
Author

Help 3

Jose Carrillo
Greenhorn

Joined: Nov 13, 2003
Posts: 17
hello well I'm creating an applet that has 5 buttons drawline, drawrect, drawoval, filloval, and fillrect. and it compiles and everything but the applet does not appear. here is my code:
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[3] = new JButton("Fill");
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;
case 3: g.fillOval(x, y, w, h);
break;
case 4: g.fillRect(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 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 somebody help me
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Jose,
You have a couple of problems in your code.
1. You must create a larger array for buttons. Try 5 items instead of 3.
2. You are only adding 3 of the created buttons to the panel. Therefore you will only see 3 buttons.
After those changes are made, the applet seems to change the button backgrounds correctly. Nothing gets drawn yet, because you must call repaint if you want the applet to change.
Regards,
Manfred.
Jose Carrillo
Greenhorn

Joined: Nov 13, 2003
Posts: 17
and how I do those changes??
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
Jose, as a rule, the Javaranch regulars and bartenders will not do your homework for you. Manfred has given you some excellent suggestions as to what is wrong with your code. It is now YOUR job to figure out how to implement them.
Kyle


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help 3