Reputation: 11
I am trying to generate a user interface and while my creation worked fine as a JFrame, it is not displaying my line based graphics in it's JPanel incarnation. If someone could point out what I am doing wrong here it would be very appreciated.
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.*;
public class Bowling_GUI_v9 extends JPanel {
public JLabel[] label1;
public JTextField[] text1;
public JComboBox[] combo1;
public JPanel displayPanel = new JPanel();
private int[] horz_coords = new int[] {8,60,580, 53,140,420,100,60,580,245,140,580,280,140,580,315,140,580,350,60,580,400,60,580};
private int[] vert_coords = new int[] {60,140,220,260,300,340,380,420,460,500,540,580};
public Bowling_GUI_v9() {
displayPanel.setLayout(null);
//displayPanel.setSize(800, 600);
displayPanel.setSize(new Dimension(800,600));
createUserInterface();
}
public void drawLines(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
//super.paintComponent(g);
int a = vert_coords.length;
for(int i=0; i<a; i++){
g2d.drawLine(1, vert_coords[i], 400, vert_coords[i]);
}
a = horz_coords.length;
for(int i=0; i<a; i=i+3){
g2d.drawLine(horz_coords[i],horz_coords[i+1],horz_coords[i],horz_coords[i+2]);
}
}
public void paint(Graphics g) {
//super.paint(g);
paint(g);
drawLines(g);
}
private void createUserInterface(){
//Container contentPane = getContentPane();
//contentPane.setLayout(null);
label1 = new JLabel[51];
text1 = new JTextField[17];
combo1 = new JComboBox[5];
int text1counter = 0;
int label1counter = 0;
int combo1counter = 0;
int popCounter = 0;
int counter = 2;
int h_pos =0;
int v_pos =1;
int flag = 0;
int wide = 30;
String[] populate = new String[] {"Team Name", "Ave", "HCP", "Name", "Game 1", "Game 2", "Game 3", "Total", "Won", "Scratch Total", "Lost", "Team HCP", "", "HCP Total Pin", "", "WINS"};
int[] identifier = new int[] {1,2,2,1,1,1,1,1,1,1,2,2,4,3,3,3,2,8,1,2,1,2,2,2,2,9};
label1[label1counter] = new JLabel();
label1[label1counter].setText(populate[popCounter]);
label1[label1counter].setBounds(8,60,80,10);
displayPanel.add(label1[label1counter]);
label1counter++;
popCounter++;
text1[text1counter] = new JTextField();
text1[text1counter].setText("Enter Team Name Here");
text1[text1counter].setBounds(101,60,200,20);
displayPanel.add(text1[text1counter]);
text1counter++;
text1[text1counter] = new JTextField();
text1[text1counter].setText("Lane");
text1[text1counter].setBounds(351,60,40,20);
displayPanel.add(text1[text1counter]);
text1counter++;
for(int a=0; a<10; a++){
for(int b=0; b<7; b++){
counter++;
switch(identifier[counter]){
case 1:
if (b == 2){ wide = 90; }
else if (b == 6 && a == 0){ wide = 55; }
else { wide = 30; }
label1[label1counter] = new JLabel();
label1[label1counter].setText(populate[popCounter]);
label1[label1counter].setBounds(horz_coords[h_pos],vert_coords[v_pos]- 20,wide,20);
displayPanel.add(label1[label1counter]);
h_pos = h_pos + 3;
label1counter++;
popCounter++;
break;
case 2:
label1[label1counter] = new JLabel();
label1[label1counter].setText("0");
label1[label1counter].setBounds(horz_coords[h_pos],vert_coords[v_pos] -20, 30, 20);
displayPanel.add(label1[label1counter]);
h_pos = h_pos + 3;
label1counter++;
break;
case 3:
text1[text1counter] = new JTextField();
text1[text1counter].setText("0");
text1[text1counter].setBounds(horz_coords[h_pos],vert_coords[v_pos] - 20,30,20);
displayPanel.add(text1[text1counter]);
text1counter++;
h_pos = h_pos + 3;
break;
case 4:
combo1[combo1counter] = new JComboBox();
combo1[combo1counter].setBounds(horz_coords[h_pos],vert_coords[v_pos] - 20,30,20);
displayPanel.add(combo1[combo1counter]);
combo1counter++;
h_pos = h_pos + 3;
break;
case 8:
b--;
flag++;
counter = 9;
break;
case 9:
b--;
flag++;
counter = 17;
break;
}
}
v_pos++;
h_pos=0;
if(flag == 4){
flag = 1;
counter ++;
}
}
label1[38].setText("");
label1[45].setText("");
}
}
Upvotes: 1
Views: 339
Reputation: 324098
Don't override paint(). Custom painting is done by overriding paintComponent(...) and then you invoke super.paintComponent(...) as the first statement.
You need to override getPreferredSize()
to return the size of your panel so layout managers can do their job.
Don't use a null layout and don't use setSize(). Use an appropriate layout manager and the layout manager will determine the size/location of any component you add to the panel.
You don't show where you actually add the panel to a frame.
Read the section from the Swing tutorial on Custom Painting for more information and working examples. In other words, start with a small example and make sure you understand the entire process before doing complex painting.
The tutorial also has a section on Layout Managers
that you should be reading to you use them properly.
Upvotes: 2