1

I'm thinking on how to draw a circle as a ball, because whenever I used paint my frame always turn into a blank white space. Is there any way that I can combine Graphics or paint with those JLabels? I've searched the internet but I cannot find any answer so hope you can help me.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BasketBall extends JFrame implements ActionListener
{
JButton shootButton = new JButton("SHOOT");
JButton drbblButton = new JButton("DRIBBLE");
JButton holdButton = new JButton("HOLD");
JButton exitButton = new JButton("EXIT");
JLabel court = new JLabel(new ImageIcon("bcourt.jpg"));
public BasketBall()
{
Container c =getContentPane();
setLayout(null);
c.add(shootButton);
c.add(drbblButton);
c.add(holdButton);
c.add(exitButton);
c.add(court);
shootButton.setBounds(0,0,120,125);
drbblButton.setBounds(0,125,120,125);
holdButton.setBounds(0,250,120,125);
exitButton.setBounds(0,375,120,86);
court.setBounds(120,0,380,500);

setTitle("BasketBall");
c.setBackground(Color.black);
setSize(500,500);
setVisible(true);
}
//public void paint (Graphics g)
{
}
public void actionPerformed(ActionEvent e)
{
}
public static void main(String args[])
{
BasketBall ball = new BasketBall();
}
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Newbie10
  • 99
  • 1
  • 14
  • 1
    Avoid using `null` layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify. – MadProgrammer Apr 26 '15 at 05:50
  • 1
    Don't override `paint` of top level containers, instead extending from something like `JPanel` and override it's `paintComponent` method instead. [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html) and [Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/) – MadProgrammer Apr 26 '15 at 05:50
  • MadProgrammer I don't get it, can you please explain it further? sorry I'm just new to java – Newbie10 Apr 26 '15 at 05:57
  • What should I do? what about override? – Newbie10 Apr 26 '15 at 06:24
  • When you create a method like paint inside JFrame, you are overriding a method defined by one of its parent classes. Instead, you should create a custom component that extends from something like JPanel and override its paintComponent method and perform your custom painting there...assuming that's what you're trying to do – MadProgrammer Apr 26 '15 at 07:37
  • The code worked! but now my problem is that, how can I put my circle(ball) in front of the image Icon(background), I've been searching for this for a while but I can't find an answer into it. – Newbie10 Apr 26 '15 at 09:40
  • Use a JPanel, override its paintComponent method and paint your image within it, use this panel as your frames content pane and add the components to it, as [demonstrated here](http://stackoverflow.com/questions/24176008/background-image-for-a-jpanel-not-working/24176183#24176183), [here](http://stackoverflow.com/questions/22162398/how-to-set-a-background-picture-in-jpanel/22162430#22162430), [here](http://stackoverflow.com/questions/18393971/having-images-as-background-of-jpanel/18394047#18394047) – MadProgrammer Apr 26 '15 at 10:05
  • Thanks a lot MadProgrammer.. It was a great help. :) – Newbie10 Apr 26 '15 at 10:30
  • @MadProgrammer is there anyway to put my fillOval infront of bufferedimage? because I used thi piece of code that I get from the link you have shown to me BufferedImage img = ImageIO.read(new File("bcourt.jpg")); – Newbie10 Apr 27 '15 at 11:45

0 Answers0