java - How to paint a gridline as a background on JPanel -
I have a problem I used gridbaghlayout in my JFrame is a component JPanel.
I wanted to draw a gridline in the form of a background for my zenith, for example, in the program given below, it wants to create 3 vertical and 3 horizontal lines, although it is only 2 vertical and 2 Displays the horizontal line. The last line was not shown.
Another problem was that, it seems that the size of the zippell was bigger than I had decided. I saw it from the length of the line which is the least white background.
Public class droplet JComponent {public dimension getPreferredSize () (new dimension (600, 600)) increases; } Public int getY () {Return 0; } Public int getX () {return 0; } @ Override Public Love Paint Coment (Graphics G) {g.setPaint (Color.GRAY); For (int i = 0; i & lt; = getSize (). Width; i + = 300) {g2.drawline (i, 0, i, getSize () height); } (Int i = 0; i & lt; = getSize (). Height; i + = 300) {g2.drawline (0, i, getSize (). Width, i); }}}
Edit:
The first problem solved (gridlines were displayed on JDNL). The second issue troubled me as you can see on the attached image, considering the length of the grid (marked as the red box), JPNL size is more than 600. How can I solve it so that the gridline background is good without any extra white space outside the gridline?
If your zenith size is 600, its available coordinates can go from 0 to 599 only . 600 is not going to be ready.
In addition, any limitations and / or insets will reduce the available space.
Update: I had some time, so I wrote your application. Hope you find some useful hints
The public class provides JC JFrame {Private Static last int DRAWING_SIZE = 600; Private stable final full SUBDIVISIONS = 2; Personal Static FINAL END SUBDIVISION_SIZE = DRAWING_SIZE / SUBDIVISIONS; Public Jesse () {Set Size (800, 800); SetLayout (new gridoutout ()); Gridbag Consultants GBC = New Gridbag Consultants (); Gbc.weightx = 1.0; Gbc.weighty = 1.0; Gbc.gridx = 0; Gbc.gridy = 0; JLab drawingboard = new JLABEL ("drawing board"); Gbc.anchor = GridBagConstraints.SOUTH; DrawingBoard.setFont (new font ("serif", font .ba, 28)); Add (drawing board, GBC); JP Penal Panel = New Zeppel () {@ Override Public Wide Paint Comonant (Graphics G) {Superpact Commonant (G); Graphics 2DG2 = (Graphics 2D) G; G2.setPaint (Color.GRAY); For (int i = 1; i & lt; subclass; i ++) {int x = i * SUBDIVISION_SIZE; G2.drawline (x, 0, x, getSize (.) Height); } (Int i = 1; i & lt; SUBDIVISIONS; i ++) {int y = i * SUBDIVISION_SIZE; G2.drawline (0, y, getSize (). Width, y); }}}; Panel.setPreferredSize (new dimension (DRAWING_SIZE, DRAWING_SIZE)); Panel.setBackground (Color.WHITE); Panel.setBorder (BorderFactory.createLineBorder (Color.BLACK)); Gbc.gridy ++; Gbc.anchor = GridBagConstraints.CENTER; Add (panel, GBC); Jebton Save Drafting = New Pocket ("Driving Drawing"); Gbc.gridy ++; Gbc.anchor = GridBagConstraints.NORTH; Add (save, gbc); } Public static zero chief (string [] args) {(new jc ()) Set isabel (true); }}
Some details:
- I have used a limit for external boundaries, this will save us problems with "599"
- I assumed that you wanted a sub-split grid inside, so I added some flexible for that flexibility and configuration.
- I did not see your
paintComponents ()
'call callsuperpointcompanies ()
. it must be! - Whatever I think, I use the minimum coding to specify gridbag barriers. Less coding = less worms :)
- Instead of a jackknon (according to your code), instead of a zippin (according to what you wrote) sub-class. The difference is that JComponent is not able to attract its background, so that gray ends.
- Perhaps the most important thing is, I measured my GUI with Kurole: the drawing component is actually in both ways of 600 size! :)
Comments
Post a Comment