Related
I'm trying to make a pong game by using Swing. I have a menu screen where you can change your preferences such as music, game speed and you can choose multiplayer options etc. But when I made a choice, their values don't change. I have my mainmenu class and computer class.
For ex: I want to change the difficulty which means changing the AI's speed but I wasn't be able to do it. I am looking forward for an answer, maybe it's so simple but I can't see it.
This is my code:
public class MenuPanel
{
ScoreBoard sc = new ScoreBoard();
private JFrame mainFrame;
private JFrame optionsFrame;
private JPanel menuPanel;
private JPanel optionsPanel;
private JFrame gameEndFrame;
private JPanel gameEndPanel;
JCheckBox twoPlayer;
// creating the swing components and containers,
// there are two frames to swap between them,
// we tried to use cardLayout but it didn't work for us.
public MenuPanel() {
mainFrame = new JFrame("Welcome To Pong Game");
mainFrame.setSize(700,700);
mainFrame.setLayout(new CardLayout());
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
menuPanel = new JPanel(null);
menuPanel.setSize(700,700);
menuPanel.setVisible(true);
mainFrame.add(menuPanel);
optionsFrame = new JFrame("Settings");
optionsFrame.setSize(700, 700);
optionsFrame.setLayout(new CardLayout());
optionsFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel optionsPanel = new JPanel(null) ;
optionsPanel.setSize(700,700);
optionsPanel.setVisible(true);
optionsFrame.add(optionsPanel);
// mainPanel components
ImageIcon ic1 = new ImageIcon("C:\\Users\\Onur17\\Downloads\\Actions-player-play-icon.png");
ImageIcon ic2 = new ImageIcon("C:\\Users\\Onur17\\Downloads\\Settings-L-icon.png");
ImageIcon ic3 = new ImageIcon("C:\\Users\\Onur17\\Downloads\\cup-icon.png");
ImageIcon ic4 = new ImageIcon("C:\\Users\\Onur17\\Downloads\\Button-Close-icon.png");
ImageIcon ic5 = new ImageIcon("C:\\Users\\Onur17\\Downloads\\ice-2025937_960_720.jpg");
ImageIcon ic6 = new ImageIcon("C:\\Users\\Onur17\\Downloads\\tenis1.png");
JLabel mainLabel = new JLabel();
Font font = new Font("Papyrus", Font.BOLD,26);
mainLabel.setFont(font);
mainLabel.setForeground(Color.RED);
mainLabel.setText("PONG GAME");
JButton startButton = new JButton("Start Game");
JButton optionsButton = new JButton("Options");
JButton leaderButton = new JButton("Leaderboard");
JButton exitButton = new JButton("Exit");
JButton icb1 = new JButton(ic1);
JButton icb2 = new JButton(ic2);
JButton icb3 = new JButton(ic3);
JButton icb4 = new JButton(ic4);
JButton icb6 = new JButton(ic6);
// at first, we tried to keep our buttons and labels in panels
// but then we didn't add an image to label as the background
// so we created a JLabel to set its image as the background, we may remove it later.
JLabel mn = new JLabel(ic5);
mn.setBackground(Color.BLUE);
mn.setBounds(1, 1, 700, 700);
Font font3 = new Font("Calibri", Font.PLAIN, 20);
twoPlayer = new JCheckBox();
twoPlayer.setFont(font3);
twoPlayer.setForeground(Color.DARK_GRAY);
twoPlayer.setText(" MultiPlayer");
twoPlayer.setBounds(200, 350, 220, 40);
menuPanel.add(mn);
mn.add(mainLabel);
mn.add(startButton);
mn.add(optionsButton);
mn.add(leaderButton);
mn.add(exitButton);
mn.add(icb1);
mn.add(icb2);
mn.add(icb3);
mn.add(icb4);
mn.add(icb6);
mn.add(twoPlayer);
mainFrame.setVisible(true);
// the components added by their coordinates to make them look formal
mainLabel.setBounds(210, 100, 220, 40);
startButton.setBounds(200, 150, 220, 40);
optionsButton.setBounds(200, 200, 220, 40);
leaderButton.setBounds(200, 250, 220, 40);
exitButton.setBounds(200, 300, 220, 40);
icb1.setBounds(150, 150, 40, 40);
icb2.setBounds(150, 200, 40, 40);
icb3.setBounds(150, 250, 40, 40);
icb4.setBounds(150, 300, 40, 40);
icb6.setBounds(150,350,40,40);
startButton.setBorder(BorderFactory.createRaisedBevelBorder());
optionsButton.setBorder(BorderFactory.createRaisedBevelBorder());
leaderButton.setBorder(BorderFactory.createRaisedBevelBorder());
exitButton.setBorder(BorderFactory.createRaisedBevelBorder());
// optionsPanel components
JButton doneButton = new JButton("Done");
doneButton.setBounds(150,330,220,40);
doneButton.setBorder(BorderFactory.createRaisedBevelBorder());
Font font1 = new Font("Calibri", Font.PLAIN,24);
Font font2 = new Font("Calibri", Font.BOLD,19);
JLabel st = new JLabel();
st.setFont(font1);
st.setForeground(Color.DARK_GRAY);
st.setText("-OPTIONS-");
JLabel difficulty = new JLabel("Difficulty: ");
difficulty.setFont(font2);
difficulty.setForeground(Color.DARK_GRAY);
JLabel music = new JLabel("Sound: ");
music.setFont(font2);
music.setForeground(Color.DARK_GRAY);
JLabel gSpeed = new JLabel("Game Speed: ");
gSpeed.setFont(font2);
gSpeed.setForeground(Color.DARK_GRAY);
JLabel screen = new JLabel(ic5);
screen.setBackground(Color.BLUE);
screen.setBounds(1, 1, 700, 700);
JRadioButton rb1 = new JRadioButton("Easy");
JRadioButton rb2 = new JRadioButton("Normal");
JRadioButton rb3 = new JRadioButton("Hard");
JRadioButton rb4 = new JRadioButton("On");
JRadioButton rb5 = new JRadioButton("Off");
JRadioButton rb6 = new JRadioButton("x");
JRadioButton rb7 = new JRadioButton("2x");
JRadioButton rb8 = new JRadioButton("3x");
ButtonGroup bg1 = new ButtonGroup();
ButtonGroup bg2 = new ButtonGroup();
ButtonGroup bg3 = new ButtonGroup();
bg1.add(rb1);
bg1.add(rb2);
bg1.add(rb3);
bg2.add(rb4);
bg2.add(rb5);
bg3.add(rb6);
bg3.add(rb7);
bg3.add(rb8);
optionsPanel.add(screen);
screen.add(difficulty);
screen.add(st);
screen.add(gSpeed);
screen.add(rb1);
screen.add(rb2);
screen.add(rb3);
screen.add(rb4);
screen.add(rb5);
screen.add(rb6);
screen.add(rb7);
screen.add(rb8);
screen.add(music);
screen.add(doneButton);
st.setBounds(200,100,220,40);
difficulty.setBounds(100,150,90,40);
music.setBounds(100,200,90,40);
gSpeed.setBounds(100, 250, 120, 40);
rb1.setBounds(200,150,60,40);
rb2.setBounds(260,150,80,40);
rb3.setBounds(340, 150, 60, 40);
rb4.setBounds(200, 200, 50, 40);
rb5.setBounds(250,200,50,40);
rb6.setBounds(220, 250, 50, 40);
rb7.setBounds(270, 250, 50, 40);
rb8.setBounds(320, 250, 50, 40);
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Pong pong = new Pong();
sound("Marimba-music");
mainFrame.dispose();
if(twoPlayer.isSelected()) {
pong.c.isTwoPlayer = true;
}
else {
pong.c.isTwoPlayer = false;
}
}
});
optionsButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mainFrame.setVisible(false);
optionsFrame.setVisible(true);
}
});
doneButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
optionsFrame.dispose();
Pong pong = new Pong();
if(rb1.isSelected())
{
pong.c.setUp(-10);;
pong.c.setDown(10);
}
else if(rb2.isSelected())
{
pong.c.setUp(-11);
pong.c.setDown(11);
}
else if(rb3.isSelected())
{
pong.c.setUp(-30);
pong.c.setDown(30);
}
if(rb4.isSelected())
{
sound("Marimba-music");
}
else if(rb5.isSelected())
{
soundStop("Marimba-music");
}
if(rb6.isSelected())
{
GamePanel g = new GamePanel();
g.x = 50;
}
else if(rb7.isSelected())
{
GamePanel g = new GamePanel();
g.x = 75;
}
else if(rb8.isSelected())
{
GamePanel g = new GamePanel();
g.x = 100;
System.out.println(g.x);
}
}
});
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
mainFrame.dispose();
}
});
}
public void sound(String nm)
{
AudioStream BGM;
try {
InputStream test = new FileInputStream("./" +nm+".wav");
BGM = new AudioStream(test);
AudioPlayer.player.start(BGM);
}
catch(IOException error) {
JOptionPane.showMessageDialog(null, error.getMessage());
}
}
public void soundStop(String nm)
{
AudioStream BGM;
try {
InputStream test = new FileInputStream("./" +nm+".wav");
BGM = new AudioStream(test);
AudioPlayer.player.stop(BGM);
}
catch(IOException error) {
JOptionPane.showMessageDialog(null, error.getMessage());
}
}
}
My Computer.class:
public class Computer
{
private GamePanel field;
private int y = Pong.WINDOW_HEIGHT / 2;
private int yCh = 0;
private int up = -10;
private int down = 10;
private int width = 20;
private int height = 90;
boolean isTwoPlayer = false;
public Computer() {
}
public void update(Ball b) {
if(y + height > Pong.WINDOW_HEIGHT)
{
y = Pong.WINDOW_HEIGHT - height;
}
else if(y < 0)
{
y = 0;
}
if(!isTwoPlayer) {
if(b.getY() < y+height && y >= 0)
{
yCh = up;
}
else if(b.getY() > y && y + height <= Pong.WINDOW_HEIGHT)
{
yCh = down;
}
y = y + yCh;
}
else {
y = y + yCh;
}
}
public void setYPosition(int speed) {
yCh = speed;
}
public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.fillRoundRect(450, y, width, height, 10, 10);
}
public int getX() {
return 450;
}
public int getY() {
return y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public int getUp() {
return up;
}
public int getDown() {
return down;
}
public void setUp(int up) {
this.up = up;
}
public void setDown(int down) {
this.down = down;
}
I don't know how your whole program is structured. But in this piece of code:
doneButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
optionsFrame.dispose();
Pong pong = new Pong();
You create a new instance of Pong class and set the values of the new created instance.
This instance is not used anywhere else. You may need to use a instance of Pong that is already in use, or make other classes use the new created instance.
Cannot put my game into the GUI without there being issues. When starting the GUI, it shows the game. It glitches out and only fixes after pressing on of the buttons. However the buttons are hidden unless you put your mouse over it. Here is the code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.*;
public class BBSays extends JFrame implements ActionListener, MouseListener
{
private BufferedImage image;
public static BBSays bbsays;
public Renderer renderer;
public static final int WIDTH = 800, HEIGHT = 800;
public int flashed = 0, glowTime, dark, ticks, indexPattern;
public boolean creatingPattern = true;
public ArrayList<Integer> pattern;
public Random random;
private boolean gameOver;
private JPanel game;
JFrame frame = new JFrame("BB8 Says");
private JPanel menu;
private JPanel credits;
ImageIcon bbegif = new ImageIcon("tumblr_o0c57n9gfv1tha1vgo1_r3_250.gif");
public BBSays()
{
Timer timer = new Timer(20, this);
renderer = new Renderer();
frame.setSize(WIDTH +7, HEIGHT +30);
frame.setVisible(true);
frame.addMouseListener(this);
frame.add(renderer);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
start();
timer.start();
menu = new JPanel();
credits = new JPanel();
game = new JPanel();
menu.setBackground(Color.yellow);
credits.setBackground(Color.yellow);
game.setBackground(Color.yellow);
JButton button = new JButton("Start");
JButton button2 = new JButton("Exit");
JButton button4 = new JButton("Start");
JLabel greet = new JLabel(" Welcome to BB8 Says");
JLabel jif = new JLabel(bbegif);
JLabel jif2 = new JLabel(bbegif);
JLabel saus = new JLabel("BB8 Image: https://49.media.tumblr.com/7ba3be87bff2efc009e9cfa889d46b4e/tumblr_o0c57n9gfv1tha1vgo1_r3_250.gif");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setContentPane(game);
frame.invalidate();
frame.validate();
};
});
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
};
});
menu.setLayout(new GridLayout(2,2));
menu.add(jif2);
menu.add(greet);
menu.add(jif);
menu.add(button);
menu.add(button6);
menu.add(button2);
frame.setVisible(true);
}
private class MenuAction implements ActionListener {
private JPanel panel;
private MenuAction(JPanel pnl) {
this.panel = pnl;
}
#Override
public void actionPerformed(ActionEvent e) {
changePanel(panel);
}
}
private void changePanel(JPanel panel) {
getContentPane().removeAll();
getContentPane().add(panel, BorderLayout.CENTER);
getContentPane().doLayout();
update(getGraphics());
}
public void start()
{
random = new Random();
pattern = new ArrayList<Integer>();
indexPattern = 0;
dark = 2;
flashed = 0;
ticks = 0;
}
public static void main(String[] args)
{
bbsays = new BBSays();
}
#Override
public void actionPerformed(ActionEvent e)
{
ticks++;
if (ticks % 20 == 0)
{
flashed = 0;
if (dark >= 0)
{
dark--;
}
}
if (creatingPattern)
{
if (dark <= 0)
{
if (indexPattern >= pattern.size())
{
flashed = random.nextInt(40) % 4 + 1;
pattern.add(flashed);
indexPattern = 0;
creatingPattern = false;
}
else
{
flashed = pattern.get(indexPattern);
indexPattern++;
}
dark = 2;
}
}
else if (indexPattern == pattern.size())
{
creatingPattern = true;
indexPattern = 0;
dark = 2;
}
renderer.repaint();
}
public void paint(Graphics2D g)
{
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.yellow);
g.fillRect(0, 0, WIDTH, HEIGHT);
if (flashed == 1)
{
g.setColor(Color.blue);
}
else
{
g.setColor(Color.blue.darker());
}
g.fillRect(0, 0, WIDTH/2, HEIGHT/2);
if (flashed == 2)
{
g.setColor(Color.green);
}
else
{
g.setColor(Color.green.darker());
}
g.fillRect(WIDTH/2, 0, WIDTH/2, HEIGHT/2);
if (flashed == 3)
{
g.setColor(Color.orange);
}
else
{
g.setColor(Color.orange.darker());
}
g.fillRect(0, HEIGHT/2, WIDTH/2, HEIGHT/2);
if (flashed == 4)
{
g.setColor(Color.gray);
}
else
{
g.setColor(Color.gray.darker());
}
g.fillRect(WIDTH/2, HEIGHT/2, WIDTH/2, HEIGHT/2);
g.setColor(Color.BLACK);
g.fillRoundRect(220, 220, 350, 350, 300, 300);
g.fillRect(WIDTH/2 - WIDTH/14, 0, WIDTH/7, HEIGHT);
g.fillRect(0, WIDTH/2 - WIDTH/12, WIDTH, HEIGHT/7);
g.setColor(Color.yellow);
g.setStroke(new BasicStroke(200));
g.drawOval(-100, -100, WIDTH+200, HEIGHT+200);
g.setColor(Color.black);
g.setStroke(new BasicStroke(5));
g.drawOval(0, 0, WIDTH, HEIGHT);
if (gameOver)
{
g.setColor(Color.WHITE);
g.setFont(new Font("Comic Sans", 1, 80));
g.drawString("You let", WIDTH / 2 - 140, HEIGHT / 2 - 70);
g.drawString("down BB8 :(", WIDTH / 2 - 220, HEIGHT / 2 );
g.drawString("Try again!", WIDTH / 2 - 195, HEIGHT / 2 + 80);
}
else
{
g.setColor(Color.WHITE);
g.setFont(new Font("Ariel", 1, 144));
g.drawString(indexPattern + "/" + pattern.size(), WIDTH / 2 - 100, HEIGHT / 2 + 42);
}
}
#Override
public void mousePressed(MouseEvent e)
{
int x = e.getX(), y = e.getY();
if (!creatingPattern && !gameOver)
{
if (x>0 && x<WIDTH/2 && y>0 && y<HEIGHT/2)
{
flashed = 1;
ticks = 1;
}
else if (x>WIDTH/2 && x<WIDTH && y>0 && y<HEIGHT/2)
{
flashed = 2;
ticks = 1;
}
else if (x>0 && x<WIDTH/2 && y>HEIGHT/2 && y<HEIGHT)
{
flashed = 3;
ticks = 1;
}
else if (x>WIDTH/2 && x<WIDTH && y>HEIGHT/2 && y<HEIGHT)
{
flashed = 4;
ticks = 1;
}
if (flashed != 0)
{
if (pattern.get(indexPattern)==flashed)
{
indexPattern++;
}
else
{
gameOver = true;
}
}
else
{
start();
gameOver = true;
}
}
else if (gameOver)
{
start();
gameOver = false;
}
}
Here is the code for the renderer class:
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class Renderer extends JPanel
{
#Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if (BBSays.bbsays != null)
{
BBSays.bbsays.paint((Graphics2D) g);
}
}
}
I think that the issue is here as I use the super. method and want to put the graphics on the game JPanel in the main code. I have tried many ways of doing this but I am not able to put the game on a JPanel.
If you can help it is greatly appreciated.
In my code i generate randoms integer between 0 and 60 and i draw lines based on these.
I just want my lines fit the ordinate vertical line without touching my randoms integer... I guess it's kind of a mathematics problem but i'm really stuck here!
Here's my code first:
Windows.java:
public class Window extends JFrame{
Panel pan = new Panel();
JPanel container, north,south, west;
public JButton ip,print,cancel,start,ok;
JTextArea timeStep;
JLabel legend;
double time=0;
double temperature=0.0;
Timer chrono;
public static void main(String[] args) {
new Window();
}
public Window()
{
System.out.println("je suis là");
this.setSize(1000,400);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setTitle("Assignment2 - CPU temperature");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
container = new JPanel(new BorderLayout());
north = new JPanel();
north.setLayout(new BorderLayout());
ip = new JButton ("New");
north.add(ip, BorderLayout.WEST);
print = new JButton ("Print");
north.add(print,BorderLayout.EAST);
JPanel centerPanel = new JPanel();
centerPanel.add(new JLabel("Time Step (in s): "));
timeStep = new JTextArea("0.1",1,5);
centerPanel.add(timeStep);
start = new JButton("OK");
ListenForButton lForButton = new ListenForButton();
start.addActionListener(lForButton);
ip.addActionListener(lForButton);
print.addActionListener(lForButton);
centerPanel.add(start);
north.add(centerPanel, BorderLayout.CENTER);
west = new JPanel();
JLabel temp = new JLabel("°C");
west.add(temp);
container.add(north, BorderLayout.NORTH);
container.add(west,BorderLayout.WEST);
container.add(pan, BorderLayout.CENTER);
this.setContentPane(container);
this.setVisible(true);
}
private class ListenForButton implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==start)
{
time=Double.parseDouble(timeStep.getText());
System.out.println(time);
chrono = new Timer((int)(1000*time),pan);
chrono.start();
}
if(e.getSource()==ip)
{
JPanel options = new JPanel();
JLabel address = new JLabel("IP Address:");
JTextField address_t = new JTextField(15);
JLabel port = new JLabel("Port:");
JTextField port_t = new JTextField(5);
options.add(address);
options.add(address_t);
options.add(port);
options.add(port_t);
int result = JOptionPane.showConfirmDialog(null, options, "Please Enter an IP Address and the port wanted", JOptionPane.OK_CANCEL_OPTION);
if(result==JOptionPane.OK_OPTION)
{
System.out.println(address_t.getText());
System.out.println(port_t.getText());
}
}
if(e.getSource()==print)
{
chrono.stop();
}
}
}
}
Panel.java:
public class Panel extends JPanel implements ActionListener {
int rand;
int lastrand=0;
ArrayList<Integer> randL = new ArrayList<>();
ArrayList<Integer> tL = new ArrayList<>();
int lastT = 0;
Color red = new Color(255,0,0);
Color green = new Color(0,200,0);
Color blue = new Color (0,0,200);
Color yellow = new Color (200,200,0);
int max=0;
int min=0;
int i,k,inc = 0,j;
int total,degr,moyenne;
public Panel()
{
super();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(new BasicStroke(1.8f));
g2.drawLine(20, 20, 20, this.getHeight()-50);
g2.drawLine(20, this.getHeight()-50, this.getWidth()-50, this.getHeight()-50);
g2.drawLine(20, 20, 15, 35);
g2.drawLine(20, 20, 25, 35);
g2.drawLine(this.getWidth()-50, this.getHeight()-50, this.getWidth()-65, this.getHeight()-45);
g2.drawLine(this.getWidth()-50, this.getHeight()-50, this.getWidth()-65, this.getHeight()-55);
g.drawString("10", 0, this.getHeight()-85);
g.drawString("20", 0, this.getHeight()-125);
g.drawString("30", 0, this.getHeight()-165);
g.drawString("40", 0, this.getHeight()-205);
g.drawString("50", 0, this.getHeight()-245);
g2.drawString("Maximum: ", 20, this.getHeight()-20);
g2.drawString(Integer.toString(max), 80, this.getHeight()-20);
g2.drawString("Minimum: ", 140, this.getHeight()-20);
g2.drawString(Integer.toString(min), 200, this.getHeight()-20);
g2.drawString("Average: ", 260, this.getHeight()-20);
g2.drawString(Integer.toString(moyenne), 320, this.getHeight()-20);
g2.setColor(red);
g2.drawLine(500, this.getHeight()-25, 540, this.getHeight()-25);
g2.setColor(new Color(0,0,0));
g2.drawString(": Maximum", 560, this.getHeight()-20);
g2.setColor(blue);
g2.drawLine(640, this.getHeight()-25, 680, this.getHeight()-25);
g2.setColor(new Color(0,0,0));
g2.drawString(": Minimum", 700, this.getHeight()-20);
g2.setColor(green);
g2.drawLine(780, this.getHeight()-25, 820, this.getHeight()-25);
g2.setColor(new Color(0,0,0));
g2.drawString(": Average", 840, this.getHeight()-20);
if(!randL.isEmpty()){
g2.setColor(red);
g2.drawLine(15, this.getHeight()-50-max, this.getWidth()-50,this.getHeight()-50-max);
g2.setColor(blue);
g2.drawLine(15, this.getHeight()-50-min, this.getWidth()-50,this.getHeight()-50-min);
g2.setColor(green);
g2.drawLine(15, this.getHeight()-50-moyenne, this.getWidth()-50,this.getHeight()-50-moyenne);
}
for(i = 0; i<tL.size(); i++){
int temp = randL.get(i);
int t = tL.get(i);
g2.setColor(new Color(0,0,0));
g2.drawLine(20+t, this.getHeight()-50-temp, 20+t, this.getHeight()-50);
// Ellipse2D circle = new Ellipse2D.Double();
//circle.setFrameFromCenter(20+t, this.getHeight()-50, 20+t+2, this.getHeight()-52);
}
for(j=0;j<5;j++)
{
inc=inc+40;
g2.setColor(new Color(0,0,0));
g2.drawLine(18, this.getHeight()-50-inc, 22, this.getHeight()-50-inc);
}
inc=0;
}
#Override
public void actionPerformed(ActionEvent e) {
rand = (int)(Math.random() * (60));
lastT += 80;
randL.add(rand);
tL.add(lastT);
Object obj = Collections.max(randL);
max = (int) obj;
Object obj2 = Collections.min(randL);
min = (int) obj2;
if(!randL.isEmpty()) {
degr = randL.get(k);
total += degr;
moyenne=total/randL.size();
}
k++;
if(randL.size()>=12)
{
randL.removeAll(randL);
tL.removeAll(tL);
lastT = 0;
k=0;
degr=0;
total=0;
moyenne=0;
}
repaint();
}
}
And here it what i gives me :
Sorry it's a real mess!
Any thoughts ?
Thanks.
You need to stop working with absolute/magical values, and start using the actual values of the component (width/height).
The basic problem is a simple calculation which takes the current value divides it by the maximum value and multiples it by the available width of the allowable area
int length = (value / max) * width;
value / max generates a percentage value of 0-1, which you then use to calculate the percentage of the available width of the area it will want to use.
The following example places a constraint (or margin) on the available viewable area, meaning all the lines need to be painted within that area and not use the entire viewable area of the component
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class DrawLine {
public static void main(String[] args) {
new DrawLine();
}
public DrawLine() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
int margin = 20;
int width = getWidth() - (margin * 2);
int height = getHeight() - (margin * 2);
int x = margin;
for (int index = 0; index < 4; index++) {
int y = margin + (int)(((index / 3d) * height));
int length = (int)(((index + 1) / 4d) * width);
g2d.drawLine(x, y, x + length, y);
}
g2d.dispose();
}
}
}
I have read multiple topics about why repaint does not call the paintComponent, yet I have not been able to combine those answers with my code to get it working. I have to do an assignment for school where in a GUI I give the X and Y coordinate for N trucks to be drawn, first one on X and Y and the rest randomly.
public class TruckMaker extends JPanel {
public int y1;
public int x1;
public static Double circle, circle2, circle3;
public static java.awt.geom.Rectangle2D.Double rectangle, rectangle2;
Random random = new Random();
public TruckMaker() {
circle = new Ellipse2D.Double();
circle2 = new Ellipse2D.Double();
circle3 = new Ellipse2D.Double();
rectangle = new Rectangle2D.Double();
rectangle2 = new Rectangle2D.Double();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLUE);
g2.fill(circle);
g2.fill(rectangle);
g2.fill(rectangle2);
g2.fill(circle2);
g2.fill(circle3);
System.out.println("Paint component is being called");
}
public void getTrucks(int x, int y, int n) {
boolean firstTimer = true;
while (n > 0) {
if (firstTimer) {
x1 = x;
y1 = y;
rectangle = new Rectangle2D.Double(x1, y1, 50, 50);
circle = new Ellipse2D.Double(x1 + 25, (y1 + 65), 25, 25);
rectangle2 = new Rectangle2D.Double(x1 + 65, y1 - 25, 150, 75);
circle2 = new Ellipse2D.Double(x1 + 140, (y1 + 65), 25, 25);
circle3 = new Ellipse2D.Double(x1 + 175, (y1 + 65), 25, 25);
n--;
firstTimer = false;
} else {
x1 = (random.nextInt(750) + 1);
y1 = y;
rectangle = new Rectangle2D.Double(x1, y1, 50, 50);
circle = new Ellipse2D.Double(x1 + 25, (y1 + 65), 25, 25);
rectangle2 = new Rectangle2D.Double(x1 + 65, y1 - 25, 150, 75);
circle2 = new Ellipse2D.Double(x1 + 140, (y1 + 65), 25, 25);
circle3 = new Ellipse2D.Double(x1 + 175, (y1 + 65), 25, 25);
n--;
}
}
repaint();
}
}
and here is where I call the TruckMaker
public class TruckMain {
private TruckMaker truck;
private TruckMain truckmain;
private final static int FRAME_WIDTH = 750;
private final static int FRAME_HEIGHT = 750;
public final static JButton startButton = new JButton("Start");
private final static BorderLayout border = new BorderLayout();
public final static JLabel x = new JLabel("x: ");
public final static JLabel y = new JLabel("y: ");
public final static JLabel n = new JLabel("n: ");
private static JPanel controlPanel = new JPanel();
private static JTextField textField1 = new JTextField(5);
private static JTextField textField2 = new JTextField(5);
private static JTextField textField3 = new JTextField(5);
public static void main(String[] args){
TruckMaker truck = new TruckMaker();
TruckMain truckMain = new TruckMain();
JFrame frame = new JFrame();
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.add(truck);
frame.setLayout(border);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
controlPanel.add(x);
controlPanel.add(textField1);
controlPanel.add(y);
controlPanel.add(textField2);
controlPanel.add(n);
controlPanel.add(textField3);
controlPanel.add(startButton);
startButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
int x1 = Integer.parseInt(textField1.getText());
int y1 = Integer.parseInt(textField2.getText());
int n1 = Integer.parseInt(textField3.getText());
truck.getTrucks(x1,y1,n1);
}
});
frame.add(controlPanel,BorderLayout.SOUTH);
}
}
You repaint method seems to work fine; the problem is in your main method and how you construct your frame. It seems like your TruckMaker component is not visible at all, so repaint has nothing to do. Maybe this is just a mistake in your posted code, since I can not even see your controls panel, but instead of adding the components directly to frame, you have to add them to the frame's contentPane:
frame.getContentPane().add(truck, BorderLayout.CENTER);
frame.getContentPane().add(controlPanel,BorderLayout.SOUTH);
After this fix, both the control panel and the TruckMaker component are visible and the paintComponent method is being called.
There's also an (unrelated) issue with the types of circle, circle2, etc. I guess this should be
public static Ellipse2D.Double circle, circle2, circle3;
Please am developing a chess game using Java. I am having problems trying to display captured chess pieces, this pieces are held in a bufferedImage. when i run and print the variable of the bufferedImage it isn't null and doesn't display the image on the JPanel.
In addition my list of captured chess piece stored in a "List capturedPiece" and is working fine, also the paths to the images are correct. The only problem is that the image stored on the bufferedImage doesn't displaying.
Here is my code
package chessgame.gui;
public class BrownPlayerPanel extends JPanel {
private Rectangle yellowPawnRect, yellowKnightRect, yellowBishopRect,
yellowRookRect, yellowQueenRect;
private Rectangle brownPawnRect, brownKnightRect, brownBishopRect,
brownRookRect, brownQueenRect;
private List<Piece> capturedPieces;
BufferedImage figurineLayer, counterLayer;
boolean firstPaint = true;
private ImageFactory fact;
BufferedImage piecetest;
public BrownPlayerPanel() {
initComponents();
fact = new ImageFactory();
}
public void setCapturedPieces(List<Piece> piece) {
capturedPieces = piece;
//System.out.println(capturedPieces);
if (counterLayer != null) {
clearBufferedImage(counterLayer);
}
if (figurineLayer != null) {
clearBufferedImage(figurineLayer);
}
updateLayers();
repaint();
}
private void initRects(int width, int height) {
int gridWidth = width / 5;
int gridHeight = height / 2;
yellowPawnRect = new Rectangle(0, 0, gridWidth, gridHeight);
yellowKnightRect = new Rectangle(gridWidth, 0, gridWidth, gridHeight);
yellowBishopRect = new Rectangle(gridWidth * 2, 0, gridWidth, gridHeight);
yellowRookRect = new Rectangle(gridWidth * 3, 0, gridWidth, gridHeight);
yellowQueenRect = new Rectangle(gridWidth * 4, 0, gridWidth, gridHeight);
brownPawnRect = new Rectangle(0, gridHeight, gridWidth, gridHeight);
brownKnightRect = new Rectangle(gridWidth, gridHeight, gridWidth,gridHeight);
brownBishopRect = new Rectangle(gridWidth * 2, gridHeight, gridWidth,gridHeight);
brownRookRect = new Rectangle(gridWidth * 3, gridHeight, gridWidth,gridHeight);
brownQueenRect = new Rectangle(gridWidth * 4, gridHeight, gridWidth,gridHeight);
}
public void clearBufferedImage(BufferedImage image) {
Graphics2D g2d = image.createGraphics();
g2d.setComposite(AlphaComposite.Src);
g2d.setColor(new Color(0, 0, 0, 0));
g2d.fillRect(0, 0, image.getWidth(), image.getHeight());
g2d.dispose();
}
private void updateLayers() {
int yPC = 0, yNC = 0, yBC = 0, yRC = 0, yQC = 0;
int bPC = 0, bNC = 0, bBC = 0, bRC = 0, bQC = 0;
if (firstPaint) {
int width = this.getWidth();
int height = this.getHeight();
figurineLayer = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
counterLayer = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
capturedPieces = new ArrayList<Piece>();
initRects(width, height);
firstPaint = false;
}
int width = this.getWidth();
int height = this.getHeight();
int gridWidth = width / 5;
for (Piece piece : capturedPieces) {
if (piece.getColor() == Piece.YELLOW_COLOR) {
if (piece.getType() == Piece.TYPE_PAWN) {
drawImageIntoLayer(figurineLayer, getFigurineImage(Piece.YELLOW_COLOR, Piece.TYPE_PAWN),yellowPawnRect);
yPC++;
}
if (piece.getType() == Piece.TYPE_KNIGHT) {
drawImageIntoLayer(figurineLayer, getFigurineImage(Piece.YELLOW_COLOR,Piece.TYPE_KNIGHT),yellowKnightRect);
yNC++;
}
if (piece.getType() == Piece.TYPE_BISHOP) {
drawImageIntoLayer(figurineLayer, getFigurineImage(Piece.YELLOW_COLOR,Piece.TYPE_BISHOP),yellowBishopRect);
yBC++;
}
if (piece.getType() == Piece.TYPE_ROOK) {
drawImageIntoLayer(figurineLayer, getFigurineImage(Piece.YELLOW_COLOR, Piece.TYPE_ROOK), yellowRookRect);
yRC++;
}
if (piece.getType() == Piece.TYPE_QUEEN) {
drawImageIntoLayer(figurineLayer, getFigurineImage(Piece.YELLOW_COLOR,Piece.TYPE_QUEEN), yellowQueenRect);
yQC++;
}
}
if (piece.getColor() == Piece.BROWN_COLOR) {
if (piece.getType() == Piece.TYPE_PAWN) {
drawImageIntoLayer(figurineLayer, getFigurineImage(Piece.BROWN_COLOR, Piece.TYPE_PAWN),brownPawnRect);
System.out.println(getFigurineImage(Piece.BROWN_COLOR, Piece.TYPE_PAWN).getWidth());
bPC++;
}
if (piece.getType() == Piece.TYPE_KNIGHT) {
drawImageIntoLayer(figurineLayer, getFigurineImage(Piece.BROWN_COLOR,Piece.TYPE_KNIGHT),brownKnightRect);
bNC++;
}
if (piece.getType() == Piece.TYPE_BISHOP) {
drawImageIntoLayer(figurineLayer, getFigurineImage(Piece.BROWN_COLOR,Piece.TYPE_BISHOP),brownBishopRect);
bBC++;
}
if (piece.getType() == Piece.TYPE_ROOK) {
drawImageIntoLayer(figurineLayer, getFigurineImage(Piece.BROWN_COLOR, Piece.TYPE_ROOK),brownRookRect);
bRC++;
}
if (piece.getType() == Piece.TYPE_QUEEN) {
drawImageIntoLayer(figurineLayer, getFigurineImage(Piece.BROWN_COLOR,Piece.TYPE_QUEEN), brownQueenRect);
bQC++;
}
}
}
if (yPC > 1) {
drawCounterIntoLayer(counterLayer, yPC, yellowPawnRect);
}
if (yNC > 1) {
drawCounterIntoLayer(counterLayer, yNC, yellowKnightRect);
}
if (yBC > 1) {
drawCounterIntoLayer(counterLayer, yBC, yellowBishopRect);
}
if (yRC > 1) {
drawCounterIntoLayer(counterLayer, yRC, yellowRookRect);
}
if (yQC > 1) {
drawCounterIntoLayer(counterLayer, yQC, yellowQueenRect);
}
if (bPC > 1) {
drawCounterIntoLayer(counterLayer, bPC, brownPawnRect);
}
if (bNC > 1) {
drawCounterIntoLayer(counterLayer, bNC, brownKnightRect);
}
if (bBC > 1) {
drawCounterIntoLayer(counterLayer, bBC, brownBishopRect);
}
if (bRC > 1) {
drawCounterIntoLayer(counterLayer, bRC, brownRookRect);
}
if (bQC > 1) {
drawCounterIntoLayer(counterLayer, bQC, brownQueenRect);
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(figurineLayer, 10,10, this);
g2d.drawImage(counterLayer, null, this);
}
private void drawCounterIntoLayer(BufferedImage canvas, int count,Rectangle whereToDraw) {
Graphics2D g2d = canvas.createGraphics();
g2d.setFont(new Font("Arial", Font.BOLD, 12));
g2d.setColor(Color.black);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
FontMetrics textMetrics = g2d.getFontMetrics();
String countString = Integer.toString(count);
int xCoord = whereToDraw.x + whereToDraw.width - textMetrics.stringWidth(countString);
int yCoord = whereToDraw.y + whereToDraw.height;
g2d.drawString(countString, xCoord, yCoord);
g2d.dispose();
}
private void drawImageIntoLayer(BufferedImage canvas, BufferedImage item,Rectangle r) {
Graphics2D g2d = canvas.createGraphics();
g2d.drawImage(item, r.x, r.y, r.width, r.height, this);
g2d.dispose();
}
private void initComponents() {
setName("Form");
setBounds(0, 0, 250, 146);
setBorder(new LineBorder(new Color(139, 69, 19)));
GroupLayout groupLayout = new GroupLayout(this);
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGap(0, 698, Short.MAX_VALUE)
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGap(0, 448, Short.MAX_VALUE)
);
setLayout(groupLayout);
setBackground(Color.black);
}
private BufferedImage getFigurineImage(int color, int type) {
BufferedImage ImageToDraw = null;
try {
String filename = "";
filename += (color == Piece.YELLOW_COLOR ? "Yellow" : "Brown");
switch (type) {
case Piece.TYPE_BISHOP:
filename += "b";
break;
case Piece.TYPE_KING:
filename += "k";
break;
case Piece.TYPE_KNIGHT:
filename += "n";
break;
case Piece.TYPE_PAWN:
filename += "p";
break;
case Piece.TYPE_QUEEN:
filename += "q";
break;
case Piece.TYPE_ROOK:
filename += "r";
break;
}
filename += ".png";
URL urlPiece = getClass().getResource("/chessgame/res/" + filename);
ImageToDraw = ImageIO.read(urlPiece);
} catch (IOException io) {
}
return ImageToDraw;
}
}
ChessWindow Class:
public class ChessWindow extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private BoardGUI boardgui;
private Game game;
JPanel panel_1;
public JLabel lblNewLabel;
private XmlRpcServer server;
private static final int PLAYER_OPTION_SWING = 1;
private static final int PLAYER_OPTION_AI = 2;
private static final int PLAYER_OPTION_NETWORK = 3;
String gameIdOnServer, gamePassword;
public ChessWindow(int yellowPlayerOption, int brownPlayerOption,Game game, String gameIdOnServer, String gamePassword) {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 776, 583);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu mnFile = new JMenu("File");
menuBar.add(mnFile);
JMenuItem mntmNewGame = new JMenuItem("New Game");
mnFile.add(mntmNewGame);
JSeparator separator = new JSeparator();
mnFile.add(separator);
JSeparator separator_1 = new JSeparator();
mnFile.add(separator_1);
JSeparator separator_2 = new JSeparator();
mnFile.add(separator_2);
JMenuItem mntmAccount = new JMenuItem("Account");
mnFile.add(mntmAccount);
JSeparator separator_10 = new JSeparator();
mnFile.add(separator_10);
JMenuItem mntmExit = new JMenuItem("Exit");
mnFile.add(mntmExit);
JMenu mnTactics = new JMenu("Tactics");
menuBar.add(mnTactics);
JMenuItem mntmTactics = new JMenuItem("Tactics 1");
mnTactics.add(mntmTactics);
JMenuItem mntmTactics_1 = new JMenuItem("Tactics 2");
mnTactics.add(mntmTactics_1);
JMenu mnServer = new JMenu("Server");
menuBar.add(mnServer);
JMenuItem mntmServerStatus = new JMenuItem("Server Status");
mntmServerStatus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//String gameId = "Game ID:" + server.getGameIdOnServer();
JOptionPane.showMessageDialog(boardgui,
server.gameIdOnServer,
"A plain message",
JOptionPane.PLAIN_MESSAGE);
}
});
mnServer.add(mntmServerStatus);
JMenuItem mntmServerLog = new JMenuItem("Server Log");
mntmServerLog.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JavaConsole console = new JavaConsole();
console.frame.setVisible(true);
console.frame.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
}
});
mnServer.add(mntmServerLog);
JMenuItem mntmViewOnlinePlayers = new JMenuItem("View Online Players");
mnServer.add(mntmViewOnlinePlayers);
JMenu mnStatistics = new JMenu("Statistics");
menuBar.add(mnStatistics);
JMenuItem mntmHumanStatsAgainst = new JMenuItem("Human Stats Against Computer");
mnStatistics.add(mntmHumanStatsAgainst);
JSeparator separator_8 = new JSeparator();
mnStatistics.add(separator_8);
JMenuItem mntmComputerStatsAgainst = new JMenuItem("Computer Stats Against Itself");
mnStatistics.add(mntmComputerStatsAgainst);
JSeparator separator_9 = new JSeparator();
mnStatistics.add(separator_9);
JMenuItem mntmHumanStatsAgains = new JMenuItem("Human Stats Agains Human");
mnStatistics.add(mntmHumanStatsAgains);
JMenuItem mntmAlgorithmInformation = new JMenuItem("Algorithm Information");
mnStatistics.add(mntmAlgorithmInformation);
JMenu mnOptions = new JMenu("Options");
menuBar.add(mnOptions);
JMenu mnTest = new JMenu("Test");
mnOptions.add(mnTest);
JMenuItem mntmPiecePosition = new JMenuItem("Piece Position");
mnTest.add(mntmPiecePosition);
JMenuItem mntmPieceMoves = new JMenuItem("Piece Moves");
mnTest.add(mntmPieceMoves);
JSeparator separator_11 = new JSeparator();
mnOptions.add(separator_11);
JMenuItem mntmConsole = new JMenuItem("Console");
mntmConsole.addActionListener(new ConsoleController());
mnOptions.add(mntmConsole);
JMenu mnHelp = new JMenu("Help");
menuBar.add(mnHelp);
JMenuItem mntmRulesOfChess = new JMenuItem("Rules of Chess");
mnHelp.add(mntmRulesOfChess);
JMenuItem mntmAbout = new JMenuItem("About");
mnHelp.add(mntmAbout);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.NORTH);
panel.setBounds(0, 0, 50, 50);
lblNewLabel = new JLabel("text");
panel.add(lblNewLabel);
JSplitPane splitPane = new JSplitPane();
splitPane.setResizeWeight(0.1);
contentPane.add(splitPane, BorderLayout.CENTER);
game = new Game();
boardgui = new BoardGUI(game);
splitPane.setLeftComponent(boardgui);
GameConfig(yellowPlayerOption, brownPlayerOption, game, gameIdOnServer, gamePassword, boardgui);
StatusPanel panel_1 = new StatusPanel();
contentPane.add(panel_1, BorderLayout.SOUTH);
boardgui.lblGameState = new JLabel();
panel_1.add(boardgui.lblGameState);
JPanel panel_2 = new JPanel();
splitPane.setRightComponent(panel_2);
panel_2.setBounds(0, 0, 50, 300);
panel_2.setLayout(null);
BrownPlayerPanel brown = new BrownPlayerPanel();
panel_2.add(brown);
MovesHistoryPanel moveDisplay = new MovesHistoryPanel();
moveDisplay.setBorder(new LineBorder(new Color(139, 69, 19)));
panel_2.add(moveDisplay);
YellowPlayerPanel yellow = new YellowPlayerPanel();
yellow.setBorder(new LineBorder(new Color(139, 69, 19)));
panel_2.add(yellow);
}
}
Given these things you stated are true:
when i run and print the variable of the bufferedImage it isn't null
In addition my list of captured chess piece stored in a "List capturedPiece" and is working fine
also the paths to the images are correct.
A common problem will custom painting that the panel that the painting is being done on has not default preferred size (0x0). A panel only get increased preferred size based on more components being added to it. In the case of custom painting on a panel, you need to give the panel an explicit preferred size by overriding getPreferredSize()
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
Without this, it can be hit or miss, whether or not you will see the contents of the panel. The determining factory is the layout manager of the parent container. Some layout managers will respect the preferred size (in which case, you won't see the contents) and some that won't respect preferred size (in which case, the panel will stretch to fit the container - where you may see the contents). Generally when custom painting, you always want to override the method. Have a look at this post to get an idea of which layout managers respect preferred sizes and which ones don't
If that doesn't work, I suggest you post an MCVE that we can run and test. Leave out all the unnecessary code that doesn't pertain to this problem. I'd sat just create a separate program that has nothing to do with your program, just a couple simple images being drawn to a panel, and add it to a frame. Something simple recreates the problem.
UPDATE
Don't use null layouts! Use layout managers.. Your layout a null for the container you want to add the panel in question to, but you can't do that without setting the bounds. But forget that. Like I said, don't use null layouts. Learn to use the layout managers in the link. Let them do the position and sizing for you.