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
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

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month9 days, 7 hours left to enroll

621 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