0

this is my problem, I hope you will help me somehow. I have one class that extends JPanel, and that class is creating a rectangle with a paintComponent method. When I add that class to JPanel who has gridBagLayout,first is not appearing. But, when I set Dimension.getPreferredSize() in that class, I can see the rectangle...and the problem is when I call MouseListener and see that rectangle is only moving in little square in the frame. So I think that somehow that method getPreferredSize() is controlling the place where will rectangle move and be.

Here is pic of my problem:

move3

limitet showing/moving4

Here is code:

Main:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class PrikazGUI {

JFrame frejm;
JPanel k;
JButton b1,b2;

public PrikazGUI(){
    frejm = new JFrame("Lala");
    k = new JPanel();
    k.setLayout(new GridBagLayout());
    Kvadrat l = new Kvadrat();
    JPanel a = new JPanel();
    a.add(l);
    k.add(a);

    frejm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frejm.setSize(1900, 1000);
    frejm.getContentPane().add(k);
    frejm.setVisible(true);


}


public static void main(String[] args) {
    PrikazGUI a = new PrikazGUI();

}

Second class:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

 import javax.swing.JPanel;

public class KvadratPravi extends JPanel {

int sizeH = 60;
int sizeW = 60;



@Override
protected void paintComponent(Graphics g) {
    // TODO Auto-generated method stub
    super.paintComponent(g);
        g.setColor(Color.PINK);
       g.drawRect(0, 0, sizeH, sizeW);
       g.fillRect(0, 0, sizeH, sizeW);
}

 @Override
 public Dimension getPreferredSize() {
     return new Dimension(sizeH,sizeW);
 }

 }

Third class:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JButton;
import javax.swing.JPanel;

public class Kvadrat extends JPanel {

JButton b1,b2;
JPanel panel;
KvadratPravi s1 = new KvadratPravi();
int x,y;


public Kvadrat(){
    GridBagConstraints cst = new GridBagConstraints();

    panel = new JPanel();
    panel.setLayout(new GridBagLayout());


    final KvadratPravi s = new KvadratPravi();
    cst.gridx = 0;
    cst.gridy = 0;
    s.setPreferredSize(new Dimension(40,40));
    s.setBounds(0, 0, 400, 400);
    panel.add(s,cst);

    JosJedanKvadrat j = new JosJedanKvadrat();
    j.setPreferredSize(new Dimension(40,40));
    j.setBounds(0, 0, 400, 400);
    cst.gridx = 0;
    cst.gridy = 4;

    panel.add(j,cst);

    s.setPreferredSize(new Dimension(400,400));
    s.setBounds(400, 400, 400, 1000);
    s.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
        if(!e.isMetaDown()){
        x = e.getX();
        y = e.getY();
        }
        }
        });
        s.addMouseMotionListener(new MouseMotionAdapter() {
        public void mouseDragged(MouseEvent e) {
        if(!e.isMetaDown()){
        Point p = s.getLocation();
        s.setLocation(p.x + e.getX() - x,
        p.y + e.getY() - y);
        }
        }
        });
    cst.gridx = 2;
    cst.gridy =4;
    panel.add(s1,cst);
    JPanel k = new JPanel();
    k.add(panel);
    add(k);

}



}

Fourth class:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JPanel;

public class JosJedanKvadrat extends JPanel {

int sizeH = 60;
int sizeW = 60;



@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.BLUE);
       g.drawOval(0, 0, sizeH, sizeW);


}

 @Override
 public Dimension getPreferredSize() {
     return new Dimension(sizeH,sizeW);
 }

 }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
getXgetY
  • 61
  • 1
  • 9
  • Post code please. – MrB Jan 24 '17 at 11:12
  • 1
    `getPreferredSize` provides layout tip to the layout manager, saying, "in a perfect world, I'd like to be this big". `GridBagLayout` will try and honour the preferred size of components where the space allows. To me, it sounds the setup is doing exactly what it's designed to do, just you're painting beyond the bounds of the custom component – MadProgrammer Jan 24 '17 at 11:13
  • You have to think of your panel like a canvas, you only have so much to paint in, if you paint "off" the canvas, then nothing is going to get painted – MadProgrammer Jan 24 '17 at 11:15
  • 1
    You're not going to want to move components that are under the control of a layout manager, that's just going to end in tears – MadProgrammer Jan 24 '17 at 11:23
  • @MadProgrammer but how then I control the position of objects, and leater to make them move? I am very new to this.. (: – getXgetY Jan 24 '17 at 11:29
  • You either write your own layout manager (welcome to the wonderful world of find another solution) or you use a single `JPanel`, paint ALL your stuff within in and manage it yourself. And because I'm a crazy lunatic, [here's an example of custom layout manager](http://stackoverflow.com/questions/21247833/how-to-prevent-jlabel-positions-from-resetting/21248274#21248274) – MadProgrammer Jan 24 '17 at 11:30
  • [Here's an example](http://stackoverflow.com/questions/33402446/how-to-click-and-drag-something-without-it-deselecting/33403429#33403429) which drags "shapes" on a single panel – MadProgrammer Jan 24 '17 at 11:35
  • And [another example](http://stackoverflow.com/questions/11819669/absolute-positioning-graphic-jpanel-inside-jframe-blocked-by-blank-sections/11822601#11822601) of a custom layout manager :P – MadProgrammer Jan 24 '17 at 11:41
  • Cooool ! I will try to understand that...hmm I trying to make puzzle game, and not sure that puzzle game will live without layout manager and set layout to null, like yours last post. @MadProgrammer :S :S Tnx for all examples :) – getXgetY Jan 24 '17 at 11:42
  • And [another crazy example](http://stackoverflow.com/questions/13698217/how-to-make-draggable-components-with-imageicon/13700490#13700490) of a custom layout manager ... seriously, I must get a life :P – MadProgrammer Jan 24 '17 at 11:43
  • Tnx for all @MadProgrammer :) I will try to do my best! – getXgetY Jan 24 '17 at 12:03

0 Answers0