import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import javax.swing.*;
public class Etch_A_Sketch extends JFrame implements MouseListener, MouseMotionListener, ActionListener // NOTE multiple interfaces
{
public void paintComponent(Graphics g)
{ super.paintComponents(g);
g.setColor(Color.blue);
g.drawRect(10, 10, 80, 30);
g.drawRoundRect(100, 10, 80, 30, 15, 15);
int thickness = 4;
for (int i = 0; i <= thickness; i++)
g.draw3DRect(200 - i, 10 - i,
80 + 2 * i, 30 + 2 * i, true);
for (int i = 0; i < thickness; i++)
g.draw3DRect(200 - i, 50 - i,
80 + 2 * i, 30 + 2 * i, false);
g.drawOval(10, 100, 80, 30);
}
Point start, end;
boolean mouseUp = false;
JFrame window;
Container content;
int mouseX,mouseY,oldX,oldY;
JLabel coords;
JButton colorButton;
JComboBox shapes;
Color newColor;
JRadioButton button1, button2;
private boolean draw,oval, rectangle, line;
private boolean fill = false;
int x1,x2,y1,y2;
Graphics gg;
public Etch_A_Sketch()
{
JFrame window = new JFrame("Classic Etch a Sketch");
content = window.getContentPane();
content.setLayout( new FlowLayout() );
coords = new JLabel();
coords.setFont(new Font("TimesRoman", Font.ITALIC + Font.BOLD, 32));
JPanel dPanel = new JPanel();
//JCOLORCHOOSER
colorButton = new JButton("Choose a Color");
colorButton.addActionListener(this);
dPanel.add(colorButton);
//JCOMBOBOX
String[] shapesList = {"Default", "Line", "Rectange", "Oval" };
shapes = new JComboBox(shapesList);
dPanel.add(shapes);
shapes.addActionListener(this);
shapes.setSelectedIndex(0);
//JRADIO
button1 = new JRadioButton("fill ");
button2 = new JRadioButton("empty");
ButtonGroup fillingsGroup = new ButtonGroup();
fillingsGroup.add(button1);
fillingsGroup.add(button2);
button2.setSelected(true);
JPanel bPanel = new JPanel();
dPanel.add(button1);
dPanel.add(button2);
button1.addActionListener(this);
button2.addActionListener(this);
//LAYOUT
content.add(bPanel, BorderLayout.SOUTH);
content.add(dPanel, BorderLayout.SOUTH);
content.add( coords);
content.addMouseListener(this); // "this" is the class that impliments that listener
content.addMouseMotionListener(this);
// "this" is the class that impliments that listener
window.setSize(640,480);
window.setVisible(true);
}
// ..............................................................
// IMPLEMENTING MOUSELISTENER REQUIRES YOU TO WRITE (OVER-RIDE) THESE METHODS
public void mouseClicked( MouseEvent me)
{
mouseX = me.getX();
mouseY = me.getY();
reportCoords("Mouse clicked at: " +
mouseX + "," + mouseY );
}
public void mousePressed( MouseEvent me)
{
mouseX = me.getX();
mouseY = me.getY();
reportCoords("Mouse Pressed at: " +
mouseX + "," + mouseY );
//repaint();
x1= mouseX;
y1 =mouseY;
System.out.println("x1: " +x1 +"y1: " +y1);
}
public void mouseReleased( MouseEvent me)
{
mouseX = me.getX();
mouseY = me.getY();
reportCoords("Mouse released at: " +
mouseX + "," + mouseY );
//repaint();
x2 = mouseX;
y2 = mouseY;
System.out.println("x2: " +x2 +"y2: " +y2);
}
public void mouseExited( MouseEvent me)
{
mouseX = me.getX();
mouseY = me.getY();
reportCoords("Mouse exited at: " +
mouseX + "," + mouseY );
//repaint();
}
public void mouseEntered( MouseEvent me)
{
mouseX = me.getX();
mouseY = me.getY();
reportCoords("Mouse Entered at: " +
mouseX + "," + mouseY );
//repaint();
}
// ...............................................................
// IMPLEMENTING MOUSEMOTIONLISTENER REQUIRES YOU WRITE (OVER-RIDE) THESE METHODS
public void mouseDragged( MouseEvent me)
{
mouseX = me.getX();
mouseY = me.getY();
if (oldX ==0 )
{
oldX=mouseX;
oldY=mouseY;
return;
}
gg = content.getGraphics();
gg.setColor(Color.YELLOW);
if(draw)
gg.drawLine( oldX,oldY, mouseX, mouseY );
gg.drawRect(100,100,100,100);
paint(gg);
oldX = mouseX;
oldY = mouseY;
reportCoords("Mouse Dragged at: " +
mouseX + "," + mouseY );
//repaint();
}
public void mouseMoved( MouseEvent me)
{
mouseX = me.getX();
mouseY = me.getY();
reportCoords("Mouse Moved at: " +
mouseX + "," + mouseY );
//repaint();
}
public static int difference(int a, int b){
return Math.abs(a-b);
}
// ..............................................................
public static void main( String[] args)
{
new Etch_A_Sketch();
}
/////////////////////////////////////////////////////////////
public void paint(Graphics g) {
g.setColor(Color.RED);
int width; int height;
width = difference(x1,x2);
height = difference(y1,y2);
if(draw)
g.drawLine( oldX,oldY, mouseX, mouseY );
else if(oval){
g.drawOval(mouseX,mouseY, width,height);
if(fill)
g.fillOval(mouseX,mouseY, width,height);
}
else if(rectangle){
g.drawRect(mouseX,mouseY, width, height);
if(fill)
g.fillRect(mouseX,mouseY, width,height);
}
}
// a helper utility
private void reportCoords( String msg )
{
coords.setText( msg );
}
public void actionPerformed(ActionEvent e) {
// Args are parent component, title, initial color
if(e.getSource()==colorButton){
Color bgColor = JColorChooser.showDialog(this, "Choose a color to draw", getBackground());
getBackground();
if (bgColor != null)
getContentPane().setBackground(bgColor);
newColor = bgColor;
System.out.println("bgColor:" +bgColor);
System.out.println("newColor:" +newColor);
}
int Selection;
Selection = shapes.getSelectedIndex();
if(Selection == 0){
System.out.println("Default: at " + Selection); setFlagsFalse(); draw = true;
}
else if(Selection == 1){
System.out.println("Line: at " + Selection); setFlagsFalse(); draw = true;
}
else if(Selection == 2){
System.out.println("Rec: at " + Selection); setFlagsFalse(); rectangle = true;
}
else if(Selection == 3){
System.out.println("Oval: at " + Selection); setFlagsFalse(); oval = true;
}
if(e.getSource() == button1){
System.out.println("button1" + fill);
fill = true;
}
else if(e.getSource() == button2){
System.out.println("button2" + fill);
fill = false;
}
}
void setFlagsFalse()
{
line = false;
oval = false;
rectangle = false;
draw = false;
}
}//EOF