paint/repaint question?

Posted on 2003-02-26
Medium Priority
Last Modified: 2010-03-31
I'm a java newbie and i'm using a JApplet to create a simple applet with two panels. One has a filled rectangle on it and the other has 3 text fields for Red, Green, and Blue . This panel also has a button which when pushed needs to repaint the sqaure in the colour typed in the boxes. When the button is pushed the square is re-painted but not in the new colours.

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

public class Exercise2 extends JApplet
{   JTextField input1, input2, input3;
    String clr_red, clr_green, clr_blue;
    public void init()      
        //intialises buttons, textfields etc.
        JButton colour = new JButton("Colour");
        colour.addActionListener(new ButtonHandler(this));
        input1 = new JTextField(3);
        input2 = new JTextField(3);
        input3 = new JTextField(3);
        JPanel panel = new JPanel();
        JPanel panelsq = new JPanel();
        panel.setLayout(new FlowLayout());
        Container c = getContentPane();
        c.add(panel, BorderLayout.SOUTH);
        c.add(panelsq, BorderLayout.CENTER);

    public void paint(Graphics g)
        //Paints a square in a given colour
        clr_red = input1.getText().toString();
        clr_green = input2.getText().toString();
        clr_blue = input3.getText().toString();
        int r = Integer.parseInt(clr_red);
        int gr = Integer.parseInt(clr_green);
        int b = Integer.parseInt(clr_blue);
        g.setColor(new Color(r,gr,b));

class ButtonHandler implements ActionListener
    Exercise2 myApplet;
    Graphics g;
    public ButtonHandler(Exercise2 app)
        myApplet = app;
    public void actionPerformed(ActionEvent e)
        // Repaints the sqaure when the button is pressed


Any suggestions greatfully received
Question by:scarykitten123
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
LVL 86

Accepted Solution

CEHJ earned 150 total points
ID: 8025186
You're setting the color after you do the fill instead of before. You should also set an instance variable to the new colour in your button handler, i.e. don't have all that parsing code in paint()

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month8 days, 18 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question