Java Help needed: Problem with using graphics
2 posters
Page 1 of 1
Java Help needed: Problem with using graphics
Solved by BIT0102-Mohaimin.
This code shows an output like -
But I think there is something wrong with this output Why on earth is it measuring the co-ordinates system from the Frame window?
I want it to measure it from the RightPanel.
(and erm, I know. This code smells of stupid names. But.. lets concentrate on the problem here )
- 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 Why on earth is it measuring the co-ordinates system from the Frame window?
I want it to measure it from the RightPanel.
(and erm, I know. This code smells of stupid names. But.. lets concentrate on the problem here )
Last edited by BIT0122-Amit on Mon Feb 21, 2011 1:54 pm; edited 1 time in total
Re: Java Help needed: Problem with using graphics
I found it out
In the MainFrame class you wrote:
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.
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.
I hope it will work now
In the MainFrame class you wrote:
- Code:
add(leftPanel);
add(rightPanel);
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);
}
- Code:
public void leftPanelFormatter() {
setPreferredSize(new Dimension(100, 400));
}
I hope it will work now
BIT0102-Mohaimin- Programmer
- Course(s) :
- BIT
Blood Group : B+
Posts : 415
Points : 715
Similar topics
» Facing problem while programming mediator problem using Java
» graphics.h problem in TC
» Need suggestions on scaling an image and making it transparent
» Problem creating executable jar file in java
» Opinion needed : which one is looking better ?
» graphics.h problem in TC
» Need suggestions on scaling an image and making it transparent
» Problem creating executable jar file in java
» Opinion needed : which one is looking better ?
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|
Tue Sep 29, 2015 2:45 pm by Sophiawood
» Cisco EHWIC SFP/GE WAN Card
Mon Sep 07, 2015 11:08 am by Sophiawood
» Huawei S1700-28GFR-4P-AC Price
Thu Jun 25, 2015 2:31 pm by Sophiawood
» teach yourself C++ / Herbert Schildt Solutions
Wed Jun 03, 2015 1:52 pm by Abdullah Al Noman
» teach yourself c by herbert schildt pdf
Wed May 13, 2015 11:01 pm by Raquib Ridwan
» ASA 5506X With Firepower ASA5506-K9
Fri Apr 10, 2015 4:31 pm by Sophiawood
» New Trends in Deal Business
Tue Feb 03, 2015 9:38 pm by nersoa
» PoE Power Allocation for WS-C2960S-24PS-L
Wed Nov 05, 2014 11:12 am by Sophiawood
» How to cure back pain
Fri Oct 31, 2014 7:15 pm by Bergen Guildford