Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- @SuppressWarnings("serial")
- public class hillstromnlab6 extends JFrame{
- boolean Ffill = false;
- boolean Cred = false;
- boolean Cyellow = false;
- boolean Cblue = false;
- boolean Ssquare = false;
- boolean SRsquare = false;
- boolean Soval = false;
- class ControlPanel extends JPanel{
- JLabel label = new JLabel("Open");
- JToolBar toolbar = new JToolBar("Open/Fill");
- JRadioButton square = new JRadioButton("Square", true);
- JRadioButton oval = new JRadioButton("Oval", false);
- JRadioButton circle = new JRadioButton("RSqr", false);
- ButtonGroup Rgroup = new ButtonGroup();
- JCheckBox red = new JCheckBox("red", true);
- JCheckBox yellow= new JCheckBox("yellow", false);
- JCheckBox blue= new JCheckBox("blue", false);
- ButtonGroup Cgroup = new ButtonGroup();
- JToggleButton fill = new JToggleButton(new ImageIcon("open_shape.gif"));
- public ControlPanel()
- {
- Rgroup.add(circle);
- Rgroup.add(square);
- Rgroup.add(oval);
- red.setBackground(Color.red);
- yellow.setBackground(Color.yellow);
- blue.setBackground(Color.blue);
- Cgroup.add(blue);
- Cgroup.add(red);
- Cgroup.add(yellow);
- toolbar.add(fill);
- toolbar.add(label);
- this.add(square);
- this.add(circle);
- this.add(oval);
- this.add(red);
- this.add(yellow);
- this.add(blue);
- this.add(toolbar);
- Ssquare = true;
- red.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent evt){
- if(red.isSelected())
- Cred = true;Cblue = false;Cyellow = false;
- }
- });
- yellow.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent evt){
- if(yellow.isSelected())
- Cyellow = true;Cred = false;Cblue = false;
- }
- });
- blue.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent evt){
- if(blue.isSelected())
- Cblue = true;Cred =false;Cyellow = false;
- }
- });
- square.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent evt){
- if(square.isSelected())
- Ssquare = true;SRsquare = false;Soval = false;
- }
- });
- circle.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent evt){
- if(circle.isSelected())
- SRsquare = true;Ssquare = false;Soval = false;
- }
- });
- oval.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent evt){
- if(oval.isSelected())
- Soval = true;Ssquare = false;SRsquare = false;
- }
- });
- fill.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent evt){
- if(fill.isSelected()){
- Ffill = true;
- fill.setSelectedIcon(new ImageIcon("fill_shape.gif"));
- label.setText("FILL");
- }
- else{
- Ffill = false;
- label.setText("OPEN");
- }
- }
- });
- }
- }//end controlpanel
- class MyCanvas extends JPanel{
- Listener myListener;
- //Constructor
- public MyCanvas(){
- myListener = new Listener(this);
- addMouseListener(myListener);
- addMouseMotionListener(myListener);
- }
- public void paintComponent(Graphics g){
- if(myListener.drag)
- {
- g.setColor(Color.green);
- g.setXORMode(Color.CYAN);
- rubberBand(g, myListener.startpt.x, myListener.startpt.y, myListener.temppt.x, myListener.temppt.x);
- rubberBand(g, myListener.startpt.x, myListener.startpt.y, myListener.endpt.x, myListener.endpt.y);
- //System.out.println("Here2");
- }
- else if(myListener.released)
- {
- Color color = getAColor();
- g.setColor(color);
- rubberBand(g, myListener.startpt.x,myListener.startpt.y, myListener.endpt.x,myListener.endpt.x);
- }
- }
- public void rubberBand(Graphics g, int startX, int startY, int endX, int endY)
- {
- int x, y, w, h;
- //g.setColor(Color.green);
- x = Math.min(myListener.startpt.x, myListener.endpt.x);
- y = Math.min(myListener.startpt.y, myListener.endpt.y);
- w = Math.abs(myListener.startpt.x - myListener.endpt.x);
- h = Math.abs(myListener.startpt.y - myListener.endpt.y);
- //System.out.println(myListener.startpt.y + " "+ myListener.endpt.y );
- //System.out.println(x + " " + y + " " + w + " " + h);
- //g.setXORMode(Color.BLACK);
- //g.setPaintMode();
- g.drawRect(x,y, w, h);
- }
- public Color getAColor()
- {
- if(Cblue)
- return Color.blue;
- else if(Cyellow)
- return Color.yellow;
- else
- return Color.red;
- }
- }
- public static void main(String[] args){
- new hillstromnlab6();
- }
- public hillstromnlab6(){
- super("Lab6");
- setBounds(100, 100, 600, 600);
- MyCanvas thecanvas = new MyCanvas();
- Container cp = getContentPane();
- cp.setBackground(Color.LIGHT_GRAY);
- ControlPanel dock = new ControlPanel();
- cp.add(dock, BorderLayout.NORTH);
- cp.add(thecanvas);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setVisible(true);
- }
- }
- /*
- if(Ffill)//fill is enabled
- {
- if(Ssquare)
- g.fillRect(x, y, w, h);
- if(Soval)
- g.fillOval(x, y, w, h);
- if(SRsquare)
- g.fillRoundRect(x, y, w, h, 20, 20);
- }
- else
- {
- if(Ssquare)
- g.drawRect(x, y, w, h);
- if(Soval)
- g.drawOval(x, y, w, h);
- if(SRsquare)
- g.drawRoundRect(x, y, w, h, 20, 20);
- }
- */
Add Comment
Please, Sign In to add comment