Fill in rectangle partially (clip?¿) SOLVED
Hello, I've searched the forum and I couldn't find any answers so here's my problem:
I have to partially fill in a rectangle, i'll express the idea using O's:
OOOOO
O-O- O
OOOOO
O----O
(-= white "squares/areas")
more or less, basically I leave out certain white areas here's my code:
Code :
import javax.swing.*;
import java.awt.*;
public class Drawing{
public static void main(String[] args) {
Drawing d = new Drawing();
}
public Color(){
JFrame frame = new JFrame("faces");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new MyComponent());
frame.setSize(800,600);
frame.setVisible(true);
}
public class MyComponent extends JComponent{
public void paint(Graphics g){
int height;
int width;
g.clipRect(1,1,50,40);
g.setColor(Color.black);
g.fillRect(1,1,10,40);
g.fillRect(2,1,50,10);
//g.fillRect(6,2,50,20);
}
}
}
the two g.fillRect lines that are NOT commented out fill in the first collumn and first row respectively, however I cant manage to fill a different row (3rd)....
(and individual squares within a given row column)
Any ideas?
pd g.clipRect(1,1,50,40) making the start coords 0,0 doesnt seem to make any difference...
Re: Fill in rectangle partially (clip?¿)
What you can do is use fillRect to fill in a bounding box, and then use clearRect to remove the bits you don't want.
You can also "adjust" the drawing location by using the create() method of the Graphics.
Code :
Graphics clipped = g.create(1,1,50,40);
Re: Fill in rectangle partially (clip?¿)
Code comments:
1. Always construct, display and update Swing components on the EDT. Further reading: Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)
2. For Swing components, the method to override is paintComponent(...), not paint(...). Further reading: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
There are several valid approaches to solving your problem, none of which are exceedingly sophisticated. That you have asked the question here strongly suggests that the code you posted is not your own.
db
Re: Fill in rectangle partially (clip?¿)
The idea of me posting here is because I have just started with Java, i.e. I needed help, however I have solved my issue and the error was that I was using wrong calculations to colour in the squares, as my squares are 10 x 10 the coordinates should be multiples of 10, not (1,2) fo example. But yeah, thanks anyways ;)
Re: Fill in rectangle partially (clip?¿)
Hello Daryll sir I am really impressed by your answers can you kindly help through mails.Please reply me
Re: Fill in rectangle partially (clip?¿)
No, Vinay, I'm not interested in helping anyone through mail. That would defeat the purpose of a forum, which is to share problems and their possible solutions.
db