?
Solved

paint/repaint question?

Posted on 2003-02-26
1
Medium Priority
?
217 Views
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);
        input1.setText("0");
        input2 = new JTextField(3);
        input2.setText("0");
        input3 = new JTextField(3);
        input3.setText("0");
        JPanel panel = new JPanel();
        JPanel panelsq = new JPanel();
        panel.setLayout(new FlowLayout());
        panel.add(input1);
        panel.add(input2);
        panel.add(input3);
        panel.add(colour);
       
        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
        super.paint(g);
        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);
        System.out.println(r);
        System.out.println(gr);
        System.out.println(b);
        g.fillRect(10,10,50,50);
        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
        myApplet.repaint();
    }

}


Any suggestions greatfully received
Thanks
Mark
0
Comment
Question by:scarykitten123
[X]
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

by:
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()
0

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