Donnerstag, 20. November 2008

EE3206 Java Mouse Color Panel

EE3206/EE5805 Java Programming (tut09)

There are 3 file in this exercise;

1.TestDrawableColorPanel.java (main)
2.RadioColorPanel.java
3.DrawColorPanel.java

/////////////////////////////////////////////////
//TestDrawableColorPanel.java (main)

public class TestDrawableColorPanel
{


public static void main(String[] args)
{
RadioColorPanel rcp = new RadioColorPanel();
rcp.setVisible(true);

}
}

//////////////////////////////////////////////////
//RadioColorPanel.java


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;



public class RadioColorPanel extends JFrame
{
public static final int WIDTH = 600;
public static final int HEIGHT =300;

private JPanel radioButtonPanel;
private DrawColorPanel colorPanel;

private JRadioButton blackButton;
private JRadioButton blueButton;
private JRadioButton greenButton;
private JRadioButton redButton;
private JRadioButton whiteButton;

// to group radio button and add() them
private ButtonGroup btg;


RadioColorPanel()
{
super("Test Drawable Color Panel");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLayout(new BorderLayout(5,5) );

radioButtonPanel = new JPanel();

radioButtonPanel.setLayout(new FlowLayout() );

btg= new ButtonGroup();


blackButton = new JRadioButton("Black");
blackButton.addItemListener(new radioColorButtonListener() );
radioButtonPanel.add(blackButton);
btg.add(blackButton);

blueButton = new JRadioButton("Blue");
blueButton.addItemListener(new radioColorButtonListener() );
radioButtonPanel.add(blueButton);
btg.add(blueButton);


greenButton = new JRadioButton("Green");
greenButton.addItemListener(new radioColorButtonListener() );
radioButtonPanel.add(greenButton);
btg.add(greenButton);


redButton = new JRadioButton("Red");
redButton.addItemListener(new radioColorButtonListener() );
radioButtonPanel.add(redButton);
btg.add(redButton);

whiteButton= new JRadioButton("White");
whiteButton.addItemListener(new radioColorButtonListener());
radioButtonPanel.add(whiteButton);
btg.add(whiteButton);


//ButtonGroup is not a subclass of Component. So a Buttongroup
// object cannot be added to a container



add(radioButtonPanel, BorderLayout.NORTH);

colorPanel = new DrawColorPanel();
colorPanel.setBorder(new TitledBorder("Color Area by ( Martin Leung )") );
add(colorPanel, BorderLayout.CENTER);

}

private class radioColorButtonListener implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
//make sure the source is a JRadioButton
if (e.getSource() instanceof JRadioButton)
{
if(blackButton.isSelected())
colorPanel.drawInBlack();
else if (blueButton.isSelected() )
colorPanel.drawInBlue();
else if (greenButton.isSelected() )
colorPanel.drawInGreen();
else if (redButton.isSelected() )
colorPanel.drawInRed();
else if (whiteButton.isSelected() )
colorPanel.drawInWhite();

}
colorPanel.setBackground(Color.LIGHT_GRAY);
}
}

}

///////////////////////////////////////////////
DrawColorPanel.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class DrawColorPanel extends JPanel
implements MouseListener, MouseMotionListener
{

private Point lineStart = new Point(0,0);
private Point mousePoint = new Point();

private boolean blackColor =false;
private boolean blueColor = false;
private boolean greenColor = false;
private boolean redColor = false;
private boolean whiteColor = false;

public DrawColorPanel()
{
super();
addMouseMotionListener(this);
addMouseListener(this);

}

public void drawInBlack()
{
blackColor =true;
blueColor = false;
greenColor = false;
redColor = false;
whiteColor = false;
}

public void drawInBlue()
{

blackColor =false;
blueColor = true;
greenColor = false;
redColor = false;
whiteColor = false;
}

public void drawInGreen()
{

blackColor =false;
blueColor = false;
greenColor = true;
redColor = false;
whiteColor = false;

}

public void drawInRed()
{

blackColor =false;
blueColor = false;
greenColor = false;
redColor = true;
whiteColor = false;

}

public void drawInWhite()
{
blackColor =false;
blueColor = false;
greenColor = false;
redColor = false;
whiteColor = true;
}

// mouse handling interface

public void mouseClicked(MouseEvent e)
{
// get its (x,y) coord
// mousePoint = e.getPoint();
// repaint();
}

public void mouseEntered(MouseEvent e)
{
//
}

public void mouseExited(MouseEvent e)
{

}

public void mouseReleased(MouseEvent e)
{
//

}
public void mousePressed (MouseEvent e)
{
//
lineStart.move(e.getX(), e.getY());
// repaint();
}

public void mouseDragged(MouseEvent e)
{

Graphics g= getGraphics();


if (blackColor == true)
g.setColor(Color.BLACK);
else if (blueColor ==true)
g.setColor(Color.BLUE);
else if (greenColor == true)
g.setColor(Color.GREEN);
else if (redColor == true)
g.setColor(Color.RED);
else if (whiteColor == true)
g.setColor(Color.WHITE);

mousePoint = e.getPoint();

// the order of the 3 lines is very important...
g.drawLine(lineStart.x, lineStart.y, e.getX(), e.getY());
g.dispose();
lineStart.move(e.getX(), e.getY()) ;
// repaint();

}

public void mouseMoved(MouseEvent e)
{
//
}

public void paintComponent(Graphics g)
{
super.paintComponent(g);


//g.drawLine(lineStart.x, lineStart.y, mousePoint.x, mousePoint.y);
//g.dispose();

}


}




3 Kommentare:

il nostro centro hat gesagt…

I enjoy this program explore on account of what you do, still by reason of the people that Probably when I am along with you.

il nostro centro hat gesagt…

I enjoy this program explore on account of what you do, still by reason of the people that Probably when I am along with you.

mobile prices hat gesagt…

Thanks for sharing this opinion. Regards for you.Good job!