Results 1 to 2 of 2

Thread: change colors

  1. #1
    calblue is offline Member
    Join Date
    Dec 2007
    Posts
    1
    Rep Power
    0

    Default change colors

    Hi,
    I need to modify following program inorder to incoroporate colors by creating toolbar.Toolbar should have 6 radio buttons Red,Black,Magenta,Blue,Green & Yellow. When new color is seleted , drawing should occur in new color.

    Thank you in advance



    package guidemo1;

    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JFrame;

    public class Painter
    extends Applet{

    private int xValue=-1, yValue=-10;


    public void paint(Graphics g){

    g.drawString("Drag the mouse to draw",10,20);
    g.fillOval(xValue,yValue,4,4);


    }

    public void update(Graphics g){
    paint(g);

    }

    public boolean mouseDrag(Event evtObj,int x,int y){

    xValue=x;
    yValue=y;
    repaint();
    return true;
    }

    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    You posted version 1.0 code. This is version 1.5 code:
    Java Code:
    //  <applet code="PainterApplet" width="400" height="400"></applet>
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.MouseInputAdapter;
    
    public class PainterApplet extends JApplet implements ActionListener {
        PainterPanel painterPanel = new PainterPanel();
    
       public void init() {
           painterPanel.addMouseListener(mia);
           painterPanel.addMouseMotionListener(mia);
           getContentPane().add(painterPanel);
           getContentPane().add(getButtonPanel(), "Last");
        }
    
        private JPanel getButtonPanel() {
            Color[] colors = { Color.red, Color.green.darker(), Color.blue };
            JPanel panel = new JPanel(new GridLayout(1,0));
            panel.setPreferredSize(new Dimension(100,30));
            for(int j = 0; j < colors.length; j++) {
                JButton button = new JButton();
                button.setBackground(colors[j]);
                button.addActionListener(this);
                panel.add(button);
            }
            return panel;
        }
    
        public void actionPerformed(ActionEvent e) {
            JButton button = (JButton)e.getSource();
            painterPanel.setColor(button.getBackground());
        }
    
        private class PainterPanel extends JPanel {
            private int x = 0, y = 0, w = 0, h = 0;
            Color color = Color.black;
    
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawString("Drag the mouse to draw",10,20);
                g.setColor(color);
                g.fillOval(x, y, w, h);
            }
    
            public void setColor(Color color) {
                this.color = color;
                repaint();
            }
    
            public void setOval(Point start, Point end) {
                x = (end.x > start.x) ? start.x : end.x;
                y = (end.y > start.y) ? start.y : end.y;
                w = end.x - start.x;
                h = end.y - start.y;
                if(w < 0) w *= -1;
                if(h < 0) h *= -1;
                repaint();
            }
        }
    
        private MouseInputAdapter mia = new MouseInputAdapter() {
            Point start;
    
            public void mousePressed(MouseEvent e) {
                start = e.getPoint();
            }
    
            public void mouseDragged(MouseEvent e) {
                painterPanel.setOval(start, e.getPoint());
            }
        };
    }

Similar Threads

  1. How to change JDK?
    By mew in forum Eclipse
    Replies: 5
    Last Post: 05-21-2010, 05:21 AM
  2. How ro change the locale of JVM
    By ravi.ks007@hotmail.com in forum Advanced Java
    Replies: 2
    Last Post: 12-26-2007, 06:17 AM
  3. Is it possible to change the '\n' into ' ' ...
    By johnny7white in forum New To Java
    Replies: 1
    Last Post: 11-15-2007, 02:32 PM
  4. how to set multiple colors in a JtextArea
    By valery in forum AWT / Swing
    Replies: 2
    Last Post: 08-01-2007, 05:12 PM
  5. How to change the resolution ?
    By samson in forum Java 2D
    Replies: 1
    Last Post: 07-17-2007, 11:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •