Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Java Painter add features

  1. #1
    Junior Member
    Join Date
    May 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Painter add features

    I want to add new features for below code.
    •radio button of color
    •radio button of eraser tool

    Please teach me how to do it.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class Paint extends JFrame {
       public static void main(String[] args){
           new Paint();
       }
     
       public Paint() {
           setSize(600, 450);
           setTitle("Software Development II");
           setDefaultCloseOperation(EXIT_ON_CLOSE);
     
           MyJPanel panel= new MyJPanel();
           Container c = getContentPane();
           c.add(panel);
           setVisible(true);
       }
     
       public class MyJPanel extends JPanel implements
            ActionListener, MouseListener, MouseMotionListener {
           JSlider sliderR, sliderG, sliderB, sliderPen;
           JLabel labelR, labelG, labelB, labelPen;
           JButton button;
           int colorR, colorG, colorB;
           int rad, x = -100, y = -100;
           boolean isButtonClicked = false;
     
           public MyJPanel() {
               setLayout(null);
     
               addMouseListener(this);
               addMouseMotionListener(this);
     
               labelR = new JLabel("Red");
               labelR.setBounds(40, 10, 20, 25);
               sliderR = new JSlider(0, 255, 0);
               sliderR.setBounds(60, 10, 200, 25);
     
               labelG = new JLabel("Green");
               labelG.setBounds(40, 35, 20, 25);
               sliderG = new JSlider(0, 255, 0);
               sliderG.setBounds(60, 35, 200, 25);
     
               labelB = new JLabel("Blue");
               labelB.setBounds(40, 60, 20, 25);
               sliderB = new JSlider(0, 255, 0);
               sliderB.setBounds(60, 60, 200, 25);
     
               labelPen = new JLabel("size of pen");
               labelPen.setBounds(280, 10, 100, 25);
               sliderPen = new JSlider(2, 50, 25);
               sliderPen.setBounds(360, 10, 200, 25);
     
               button = new JButton("erase all");
               button.setBounds(410, 45, 100, 30);
               button.addActionListener(this);
     
               add(labelR);
               add(sliderR);
                add(labelG);
               add(sliderG);
                add(labelB);
                add(sliderB);
               add(labelPen);
               add(sliderPen);
               add(button);
           }
     
           public void paintComponent(Graphics g) {
               if (isButtonClicked) {
                   super.paintComponent(g);
                   isButtonClicked = false;
               }
     
               g.setColor(new Color(colorR, colorG, colorB));
               g.fillOval(x-rad/2, y-rad/2, rad, rad);
               repaint();
           }
     
           public void actionPerformed(ActionEvent e) {
               isButtonClicked = true;
               repaint();
           }
     
           public void mouseClicked(MouseEvent e) {
           }
     
           public void mousePressed(MouseEvent e) {
           }
     
           public void mouseReleased(MouseEvent e) {
               x = -100;
               y = -100;
               repaint();
           }
     
           public void mouseExited(MouseEvent e) {
           }
     
           public void mouseEntered(MouseEvent e) {
           }
     
           public void mouseMoved(MouseEvent e) {
           }
     
           public void mouseDragged(MouseEvent e) {
               colorR = sliderR.getValue();
               colorG = sliderG.getValue();
               colorB = sliderB.getValue();
               rad = sliderPen.getValue();
                x = e.getX();
               y = e.getY();
              repaint();
           }
       }
    }
    Last edited by Blackcat_20210522; May 21st, 2021 at 08:02 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,710 Times in 2,660 Posts

    Default Re: Java Painter add features

    Start by looking at the tutorial on how to build a Swing GUI:
    https://docs.oracle.com/javase/tutor...ing/index.html

    Change the code to use a layout manager vs using setBounds for all of the components.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Painter add features

    OK, I will try it.

Similar Threads

  1. Can somebody add to the other java 8 features that i have mentioned
    By bharath777 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 24th, 2014, 05:46 PM
  2. Help with ArrayList Features
    By blobby404 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 19th, 2014, 03:08 AM
  3. Java 1.7 features
    By Reddi.Java in forum Member Introductions
    Replies: 1
    Last Post: February 8th, 2013, 09:01 AM
  4. WORKING BUT SOME FEATURES NOT
    By kariolos in forum What's Wrong With My Code?
    Replies: 16
    Last Post: May 26th, 2011, 06:25 AM
  5. CVS features in Eclipse
    By bbr201 in forum Java IDEs
    Replies: 5
    Last Post: August 11th, 2010, 06:12 AM