Example for learning Java

Can some one show me how to or refer me to a link showing how to
draw a rectangle in java such that it switches red and green color whenever clicked?

I need this to see the effort involved in java programming and whether I can go for it.

Any other similar simple example would do.
LVL 43
Saqib Husain, SyedEngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

krakatoaCommented:
Use a Graphics2D and the drawRect method, then just fill it in - or not.

Basically :

public void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.drawRect(10, 50, 70, 60);
  }

Open in new window

0
Saqib Husain, SyedEngineerAuthor Commented:
What is a Graphics2D? And if it matters I have installed Eclipse.
0
krakatoaCommented:
It’s the graphics environment / context Java uses. The online literature describes it in detail. You would need to reference that to get the full picture. You can get a graphics context from Components like JFrame.
0
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

StillUnAwareCommented:
Hi,

  as with all new content, you should start from a tutorial, the one on Java Graphics is here.

  More advanced topics are outlined here.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Saqib Husain, SyedEngineerAuthor Commented:
Hi, I have tried the tutorial given above and am running into problems.

I typed the code and got errors. I copied and pasted and it worked fine. I cannot find the difference between the two. Can you help me?

Code typed

Code Copy/Pasted
0
krakatoaCommented:
Not good optics on your cut and paste, but one class you name "box" and the other snippet uses "box3". That's all I can spot from here.
0
krakatoaCommented:
You will need something on these lines :

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.*;

public class RectColorChanger extends JPanel {

static boolean clicked;
static Graphics2D g2d;
static MouseAdapter ma;


  public void paintComponent(Graphics g) {
  
  
    g2d = (Graphics2D) g;
    
    if(clicked){
        
        
        g2d.setColor(new Color(255, 0, 0));
        g2d.fillRect(10, 50, 70, 60);
        
    }
    
    else{
        
        
        g2d.setColor(new Color(0, 255, 0));
        g2d.fillRect(10, 50, 70, 60);
        
    }

  }
  
    
  public RectColorChanger (){this.addMouseListener(ma=new Ma());}
  
    class Ma extends MouseAdapter{
  
        public void mouseReleased(MouseEvent m){
  
            clicked = !clicked;
            RectColorChanger.this.paintComponent(RectColorChanger.this.getGraphics());
        }
    }


  public static void main(String[] args) {
    
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new RectColorChanger());
    frame.setSize(360, 300);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

Open in new window

0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
In the version that you typed, on line 16 you named the method in all lowercase. But on line 11 you call a method with a similar name but with some uppercase. Java is case-sensitive.
0
Saqib Husain, SyedEngineerAuthor Commented:
This is painful.

I corrected the case. On another line which gave an error I corrected the case and the red crosses went away. But then there were black circled crosses in both lines reporting something about "..unresolved..".

There were tab characters in some places for indenting the code when I changed them to spaces the error went away. Is this normal?
0
krakatoaCommented:
If you are near the front of the learning curve with Java, I wouldn’t use an IDE. Just use an editor such as Notepad++, bringing you as close as possible to your code and the API. IDEs are stopping your progress otherwise.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.