Scrolls not appears - java

I want add JScrolpane to JPanel, but that not appears. In JLabel works fine and its very easy. I am using JPanel beacuse I'll add some image proccessing stuff to my program. There is my code:
public void draw(){
panel=new JPanel(){
protected void paintComponent(Graphics g){
Graphics g2 = g.create();
g2.drawImage(image, 0, 0, this);
g2.dispose();
}
};
panel.setBorder(BorderFactory.createEtchedBorder());
panel.setPreferredSize(new Dimension(400, 330));
s=new JScrollPane(panel);
s.setPreferredSize(new Dimension(400,285));
this.getContentPane().add(s,BorderLayout.CENTER);
add(panel);
revalidate();
repaint();
}

s=new JScrollPane(panel);
s.setPreferredSize(new Dimension(400,285));
this.getContentPane().add(s,BorderLayout.CENTER);
add(panel); // ****** ????????????
revalidate();
repaint();
}
You're adding the JPanel to the GUI not the JScrollPane, so you really shouldn't expect to see any scrollpanes if they've not been added anywhere.
Solution: Add your JScrollPane, s, that holds the JPanel to the GUI, not JPanel itself.

You dont honor the paint chain, call super.paintComponent(g) as first call in overridden paintComponent method. i.e
#Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
//draw here
}
or visual artifacts may occur.
Also note the #Override annotation which should be used with overridden methods in order to gain the advantage of compiler checking that we overrode the method correctly.
There is no need for getContentPane().add(..) simply call add on JFrame instance as add(..) along with remove(..) and setLayout(..) have been forward to the JFrames contentPane
Also not a good idea to go extending JFrame for nor reason, simply create an instance and use that i.e:
JFrame frame=new JFrame("Title here");
...
frame.add(..);
...
frame.pack();
frame.visible(true);
Also draw onto the Graphics object passed into paintComponent g dont go creating your own in paintComponent. Unless of course you're doing if for the reason #HFOE mentioned in below comment :).
No need for this parameter in drawImage(..) unless your JPanel implements on ImageObserver or the image may not be fully loaded when painting occurs. Simply use null.
And just for the cherry on top use some Graphics2D and RenderHints as seen here. This will allow for a better quality image to be drawn and text.

I got it! The main reason for me was no-layout manager and no-declarated size of this. There is my code:
public void draw(){
panel=new JPanel(new BorderLayout()){
#Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(img, 0, 0, null);
}
};
panel.setBounds(0, 0, img.getWidth(), img.getHeight());
panel.setPreferredSize(new Dimension(img.getWidth(),obraz.getHeight()));
JScrollPane scrolls=new JScrollPane(panel,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
add(scrolls);
validate();
repaint();
}
But if someone will want copy this code in the future, there is one thing to be known- a size of Jpanel isn't refreshing (it's always one size of first opened image), but I have scrolsl and I am satisfied for now ;). Thanks a lot for everyone.

Related

How add a jtextfield to a jpanel that has paint component?

I'm making a game and I want to add a JTextField to a JPanel that has Paint Component. I repaint the JPanel every 16 milliseconds.
I add() the textfield to the panel but it show up only for a single frame when i click on it.
Then I tried to repaint() the textfield but now it is flashing.
public class Screen extends JPanel {
public Screen() {
JTextField txt = new JTextField();
txt.setBounds(10, 10, 300, 50);
this.add(txt);
}
#Override
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
g2D.setColor(Color.BLACK);
g2D.fillRect(0, 0, this.getWidth(), this.getHeight());
g2D.setColor(Color.WHITE);
g2D.fillRect(0, 0, this.getWidth(), 20);
txt.repaint();
}
}
I want to show the textfield on the top of the panel
JTextField txt = new JTextField();
When you create a JTextField you should use code like:
JTextField txt = new JTextField(10);
Now the text field can calculate its own preferred size.
//txt.setBounds(10, 10, 300, 50);
Don't use setBounds() to give a component a size. Again each Swing component is responsible for determining its own preferred size. Then the layout manager will set the size/location of the component on the panel.
//public void paint(Graphics g) {
public void paintComponent(Graphics g)
{
super.paintComponent(g);
// add custom painting here
}
Don't override paint(). Custom painting is done by overriding the paintComponent() method. And the first statement in the method should be super.paintComopnent(g)
//g2D.setColor(Color.BLACK);
//g2D.fillRect(0, 0, this.getWidth(), this.getHeight());
Don't paint the background of the panel. That is the job of the panel and that is why you need to super.paintComponent(), to make sure the background is painted.
Then in the constructor of your JPanel class you simply use setBackground( Color.BLACK )
//txt.repaint();
Don't ever invoke repaint() on any component in a painting method.
Read the section from the Swing tutorial on Custom Painting for working examples to get you started. Use the demo code as the starting point for you program. Then you simply add a JTextField to the panel, so it will be a single line of code that is needed to display the text field.
It seems like you want to have a JTextField on a black panel. You don't need to set the colour of the panel every time in paint() method. Instead add this to the constructor:
public Screen() {
setOpaque(true);
setBackground(Color.BLACK);
//...
}
and remove paint() method.
Also, if you want to use absolute positioning with setBounds() method then you should set the layout to null setLayout(null) in constructor. If you use absolute positioning you will also need to specify the size of the panel explicitly. However, I would still suggest you use a layout manager that takes care of panel sizing as well.
See this post for more information about absolute positioning.

White space at the right and bottom edge of the window

After I created a window, which should be completely black, there are white colors at the right and bottom edges. What is it that I may be doing wrong ?
this is the constructor where I do initialisation of window :-
public Panel()
{
Thread t = new Thread(this);
addKeyListener(this);
setFocusable(true);
this.setPreferredSize(new Dimension(gameWidth, gameHeight));
JFrame f = new JFrame("AlienBusters");
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
t.start();
}
this is the paint method where I made the window black :-
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(0, 0, gameWidth, gameHeight);
}
Five things...
Calling setResizable after pack, not a good idea, setResizable can change the frame border size, which affects the available content size...
Using KeyListener, seriously, see How to Use Key Bindings and save youreself the head ache...
Relying on magic numbers instead of imperical values, g.fillRect(0, 0, gameWidth, gameHeight); should be g.fillRect(0, 0, getWidth(), getHeight()); or better, simply use setBackground(Color.BLACK) and get it for free via super.paintComponent...and paintComponent should be protected ;)
Calling setPreferredSize. This means that the size could be changed, which, probably, isn't what you really want. Instead, override getPreferredSize instead. See Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? for more details
Constructing a frame within the constructor of a component, the component shouldn't care how it's going to be displayed, it should just focus on it's job.

Draw a circle that doesn't "break" when resized in Java

How can I draw a circle that is resizable when the window is maximized or minimized? The code I have isn't really even draw the circle like I would like it to either. Any help with this would be greatly appreciated as I have never worked with graphics in Java before and the Oracle site is only helping me so much. Thanks.
public class GUI extends JFrame {
public GUI() {
JPanel p1 = new JPanel();
}
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawOval(0, 0, 50, 50);
}
public static void main(String[] args) {
GUI frame = new GUI();
frame.setTitle("Circle Generator");
frame.setSize(400, 300);
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
To make the painting dynamic you need to get the current width/height of the panel.
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
//g2d.drawOval(0, 0, 50, 50);
g2d.drawOval(0, 0, getWidth(), getHeight());
}
You also need the super.paintComponent(g) to clear the background of the panel.
Edit:
Actually, I took a closer look at your code and it does nothing. A JFrame does not have a paintComponent() method so your code will never be executed. Also, you create a panel but then don't do anything with it.
Start by reading the section from the Swing tutorial on Custom Painting for more information and working examples.
Then you can modify the example from the tutorial to draw your oval, using the suggestion give above.

Paint Component Method not working for JFrame

I'm trying to paint an image on the screen after trying to use JLabel's and am now trying the paintComponent method. I tried inserting breakpoints after seeing no results and the method doesn't get called, and nothing appears. What should I do? Here is my important code-
`
public void createWindow(){
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setSize(xSize, ySize);
frame.setLocation(0, 0);
frame.addComponentListener(this);
//frame.add(im);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(placeholder, 0, 0, getWidth(), getHeight(), null);
g.drawString("Hello", 100, 100);
}
Also I'm using a JFrame instead of JPanel or component if that makes a difference.
JFrame does not have a paintComponent method. You should avoid painting directly to a frame and instead use a JPanel and override its paintComponent method
You should also make use of the #Override annotation, which will raise a compiler exception if the parent class does not have the method you are trying to override...

Issues with ImageIcon and images on buttons - JFrame

this has been driving me crazy all day so I figured I'd post it here and see if someone else can work it out. First off I tried to add a background image, the default ImageIcon was not working so I went with overriding the paint method instead. #
public class ImageJPanel extends JPanel {
private static final long serialVersionUID = -1846386687175836809L;
Image image = null;
public ImageJPanel(){
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
ImageJPanel.this.repaint();
}
});
}
//Set the image.
public ImageJPanel(Image i) {
image=i;
setOpaque(false);
}
//Overide the paint component.
public void paint(Graphics g) {
if (image!=null) g.drawImage(image, 0, 0, null);
super.paint(g);
}
}
Once I used that it worked fine, however now I want to add images to my buttons but it is not working. Here is how my buttons work:
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Images images = new Images();
JPanel j = new ImageJPanel(images.menuBackground);
j.setLayout(null);
JButton Button_1= new JButton("Button_1",new ImageIcon("images/gui/Button.png"));
Insets insets = j.getInsets();
Dimension size = Button_1.getPreferredSize();
Button_1.setBounds(300 + insets.left, 150+ insets.top, size.width, size.height);
Singleplayer.setSize(200, 50);
j.add(Button_1);
frame.add(j);
frame.setSize(800,600);
frame.setResizable(false);
Button_1.addMouseListener(singleplayerPressed);
frame.setVisible(true);
All my images are .png, could that affect it?
Let's start with this:
public void paint(Graphics g) {
if (image!=null) g.drawImage(image, 0, 0, null);
super.paint(g);
}
This is the wrong approach. Firstly, you really don't want to override the paint method UNLESS you absolutely know that this is the correct approach to your problem (and without knowing more, I'd suggest it isn't).
Secondly, you paint your image on the component, then promptly paint over the top of it...(super.paint(g); can have the ability to paint over your work, I know the panel is opaque, but this is still a very bad approach).
Use paintComponent instead
protected void paintComponent(Graphics g) {
super.paint(g);
if (image!=null) g.drawImage(image, 0, 0, null);
}
PNG images are fine, they are supported by Swing out of the box.
Make sure that your program can see the image. Is it been loaded from the file source or is it a resource within you JAR?
Try this:
System.out.println(new File("images/gui/Button.png").exits());
If your program can see the file, it will return true other wise the program can not see the file and that is your problem.
Try this:
ImageIcon image = new ImageIcon(this.getClass()
.getResource("images/gui/Button.png"));
Side note, you should override paintComponent() not paint(). Also make sure to call super implementation before doing all your painting. For more details see Lesson: Performing Custom Painting tutorial.

Categories

Resources