Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

HELP

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
hi well my question is I have to create an applet with 3 button, one to draw a line, other to draw a rect, and other to draw an oval. Also an additional button that fill the rect and ovals how can I do that because I have not idea I have try everything and nothing works. please help me 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[4];
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<4; 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<4; 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.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<4; i++)
{
buttons[i].setBackground(Color.lightGray);
if (e.getSource() == buttons[i])
{
shape = i;
buttons[i].setBackground(Color.red);
}
}
}
}
thanks
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Originally posted by Jose Carrillo:
I have try everything and nothing works.


Heck, you haven't even tried the suggestion from A. Wolf in this thread.. This is your 6th post in two days asking for someone to do your homework. You will get better results if you ask a specific question (i.e. how do I draw a square using the mouse on a JPanel) than just posting your code and hoping someone finishes it for you.
 
    Bookmark Topic Watch Topic
  • New Topic