This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Grokking Bitcoin and have Kalle Rosenbaum on-line!
See this thread for details.
Win a copy of Grokking Bitcoin this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

need help with my code  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.. I'm trying to do a Frame with this draw that changes it's color each time I click the button... but it's not working ... can someone help me?

Here is the code.. ( only the button shows up in the screen).

public class MyDrawPanel extends JPanel {

public void paintComponet(Graphics g) {

g.fillRect(0, 0, this.getWidth(), this.getHeight());

int red = (int)(Math.random()*255);
int green = (int)(Math.random()*255);
int blue = (int)(Math.random()*255);
Color randomColor = new Color(red,blue,green);
g.setColor(randomColor);
g.fillOval(70,70,100,100);

}

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

/**
*
* @author babi rocha
*/
public class SimpleGui3C extends JFrame implements ActionListener {

JFrame frame;

public static void main (String[] args) {
SimpleGui3C gui = new SimpleGui3C();
gui.go();
}

public void go() {
frame = new JFrame();

frame.setLayout(new GridLayout(2, 2));

JButton button = new JButton("Change colors!");
button.addActionListener(this);

MyDrawPanel drawPanel = new MyDrawPanel();

frame.getContentPane().add(BorderLayout.SOUTH, button);
frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
frame.setSize(300, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(200,200);
frame.pack();

}

public void actionPerformed(ActionEvent e) {
frame.repaint();
}
}
 
Rancher
Posts: 42974
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

paintComponet



That's your problem - the method name is missing an "n". You may want to get in the habit of using the @Override annotation to avoid this kind of mistake.
 
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You just misspelled paintComponent

Also, the packed frame isn't large enough for the circle to show up without resizing the frame. You may want to set the frame size specifically to a larger size and leave off the pack() call, or set the minimum and preferred size of the MyDrawPanel to a value that will take the size and location of the circle into account.
 
We should throw him a surprise party. It will cheer him up. We can use this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!