Hi, I have two problems with application I write in Java. Now it looks like that:

import java.awt.*;
import javax.swing.*;

public class Main{

    public Main() {
        JFrame frame = new JFrame();
        frame.setSize(1200,900);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        JPanel background = new JPanel(new GridBagLayout());
        background.setBackground(Color.lightGray);

        LeftPanel lp = new LeftPanel();
        background.add(lp, new GridBagConstraints(0,0,1,3,3.0,1.0,
                                                         GridBagConstraints.WEST, GridBagConstraints.NONE,
                                                         new Insets(10, 10, 10, 10), 0, 0));

        Stats stats = new Stats();
        background.add(stats, new GridBagConstraints(1,0,1,1,1.0,1.0,
                                                        GridBagConstraints.WEST, GridBagConstraints.BOTH,
                                                        new Insets(10, 10, 10, 10), 0, 0));

        Settings settings = new Settings();
        background.add(settings , new GridBagConstraints(1,1,1,1,1.0,1.0,
                                                         GridBagConstraints.WEST, GridBagConstraints.BOTH,
                                                         new Insets(10, 10, 10, 10), 0, 0));

        JButton button = new JButton();
        background.add(button, new GridBagConstraints(1,2,1,1,1.0,0.2,
                                                        GridBagConstraints.WEST, GridBagConstraints.BOTH,
                                                         new Insets(10, 10, 10, 10), 0, 0));

        frame.add(background);
    }

    public static void main(String[] args) {
        new Main();
    }
}

///////////////////////////////////////////////////////////

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;

public class LeftPanel extends JPanel {

    ArrayList<Rect> r = new ArrayList<> ();

    public LeftPanel() {
        setBackground(new Color(0,140,0));
        setMinimumSize(new Dimension(1000,800));
        setPreferredSize(new Dimension(1000,800));
        r.add(new Rect(0,0,1000,1000));
        r.add(new Rect(50,50,100,100));
    }
}

////////////////////////////////////////////////////////////

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Point2D;

public class Rect extends JComponent {
    Point2D start, end;

    public Rect(int x, int y, int x_k, int y_k) {
        start = new Point2D.Double(x, y);
        end = new Point2D.Double(x_k, y_k);
    }

    @Override
    public void paintComponents(Graphics g) {
        super.paintComponents(g);
        g.setColor(Color.BLACK);
       // g.fillRect((int)start.getX(), (int)start.getY(), 100, 1000);
        g.fillRect(10, 10, 100, 1000);
    }

}

////////////////////////////////////////////////////////////

import javax.swing.*;
import java.awt.*;

public class Settings extends JPanel {
    public Settings() {
        setBackground(Color.yellow);
    }
}

import javax.swing.*;
import java.awt.*;

public class Settings extends JPanel {
    public Settings() {
        setBackground(Color.yellow);
    }
}

////////////////////////////////////////////////////////

import javax.swing.*;
import java.awt.*;

public class Stats  extends JPanel {
    public Stats() {
        setBackground(Color.red);
    }
}

now it look like that:

My app

the first problem is, that when I set fixed size of left panel (green one) the others changed their size too, what created gap between. I'd like them to be 10 pixels apart (it were that way when green panel hadn't got fixed size, in future, at least for a while all panels will probably have fixed sizes, I will see yet), could you tell me what to do to have it as close?

Secound one I want to draw on green JPanel black rectangles (Rect class) so I use add() method to add them to panel, there is paintComponents() method inside, but I don't see them on screen. how can I draw rectangles on green JPanel (they should be only in that panel)?

Recommended Answers

All 3 Replies

line 77 (etc?) should be paintComponent, not paintComponents

pk11, kam1, same code. Whats going on. No more help until we get some explanation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.