Java Help needed: Problem with using graphics

View previous topic View next topic Go down

Java Java Help needed: Problem with using graphics

Post by BIT0122-Amit on Mon Feb 21, 2011 4:02 am

Solved by BIT0102-Mohaimin.

Code:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BoxLayout;

class LeftPanel extends JPanel {

   private BoxLayout leftPanelLayout;

   public void leftPanelFormatter() {
      setSize(100, 400);
      
   }

   public LeftPanel() {
      
      leftPanelFormatter();
   }

}

class RightPanel extends JPanel {
   public void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;
      g2.setColor(Color.white);
      g2.fillRect(0, 0, getWidth(), getHeight());
      g2.setColor(Color.black);
      g2.drawLine(0, 0, getWidth(), getHeight());
   }
}

class MainFrame extends JFrame {
   private LeftPanel leftPanel;
   private RightPanel rightPanel;

   public MainFrame() {
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      setSize(400, 400);
      setResizable(false);
      setTitle("Composite Pattern");
      setLocationRelativeTo(null);
      leftPanel = new LeftPanel();
      rightPanel = new RightPanel();

      add(leftPanel);
      add(rightPanel);
      setVisible(true);

   }
}

public class GrrClass {
   public static void main(String[] args) {
      MainFrame mainFrame = new MainFrame();
   }

}


This code shows an output like -


But I think there is something wrong with this output Neutral Why on earth is it measuring the co-ordinates system from the Frame window?
I want it to measure it from the RightPanel. Neutral

(and erm, I know. This code smells of stupid names. But.. lets concentrate on the problem here Neutral )


Last edited by BIT0122-Amit on Mon Feb 21, 2011 1:54 pm; edited 1 time in total

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Java Re: Java Help needed: Problem with using graphics

Post by BIT0102-Mohaimin on Mon Feb 21, 2011 12:30 pm

I found it out Very Happy
In the MainFrame class you wrote:
Code:

add(leftPanel);
add(rightPanel);
The default layout of JFrame is BorderLayout. BorderLayout expects a constraints as the second parameter of the add() method. There are 5 possible constraints, EAST, WEST, NORTH, SOUTH and CENTER.
It you do not mention any, the fifth one is taken by default.
So, both your JPanels are added in the center. One on other.
You should make it like this.
Code:
add(leftPanel, BorderLayout.WEST);
add(rightPanel);

Again, it will not work! Your line will be drawn fine, but you will no longer see the leftPanel. Because you need to change the class LeftPanel.
Code:
public void leftPanelFormatter() {
setSize(100, 400);
}
It should be replaced with
Code:
public void leftPanelFormatter() {
setPreferredSize(new Dimension(100, 400));
}
When you are using a LayoutManager, it does not care about what size you have set to a Component. However, it tries to respect preferredSize as much possible.

I hope it will work now Very Happy
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

Java Re: Java Help needed: Problem with using graphics

Post by BIT0122-Amit on Mon Feb 21, 2011 1:20 pm

salute Very Happy Very Happy

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Java Re: Java Help needed: Problem with using graphics

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum