我有一个程序可以在jFnel内部的jPanel内部连接jSnel绘制游戏板。用于绘制面板的逻辑工作(并没有被更改)但是当更新GUI有点复杂时,它破了,我无法修复它。对于后人,我正在上传识别问题所需的完整相关类和功能,所以如果你想看看我如何制作六边形网格我想去吧。无论如何这里的代码给了我麻烦:
* bp是一个新的BoardPanel
绘制创建新窗口的函数:
public void draw(GroupChatClient client) throws Exception
{
frame.setVisible(false);
BoardFrame bf = new BoardFrame(in,out);
bf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
bf.setMainDisplayPane(bp);
if(bp.orientation == 'V')
{
panelSizeX=2*(int)(bp.width*Math.sqrt(3)*bp.size/2);
panelSizeY= 2*bp.yOff + 2*bp.size*bp.height;
}
else //orientation = FLAT
{
panelSizeX= 2*(bp.yOff)+ (bp.size*bp.width);
panelSizeY= 2*(int)(bp.height*Math.sqrt(3)*bp.size/2);
}
System.out.println(panelSizeX + " " + panelSizeY);
bf.getChatTextArea().setEditable(false);
bf.getSearchTextArea().setEditable(false);
DefaultCaret newCaret = (DefaultCaret)bf.getChatTextArea().getCaret();
newCaret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
bf.getChatTextArea().setLineWrap(true);
bf.getSearchTextArea().setLineWrap(true);
//bf.getChatTextField().addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent e) {
// out.println(bf.getChatTextField().getText());
// bf.getChatTextField().setText("");
// }
//});
bf.pack();
bp.setVisible(true);
bf.setVisible(true);
while(true)
{
String line = in.readLine();
if (line.startsWith("MESSAGE")) {
bf.getChatTextArea().append(line.substring(8) + "\n");
}
}
}
BoardPanel:
package groupchatclient;
import java.awt.*;
import javax.swing.*;
public class BoardPanel extends JPanel
{
int xOff = 20,yOff = 20,height = 20,width = 50,size = 30;
char orientation = 'V';
public BoardPanel()
{
setBorder(BorderFactory.createLineBorder(Color.black));
}
//@Override
//public Dimension getPreferredSize()
//{
// return new Dimension(600,400);
//}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
this.setBackground(Color.WHITE);
g.setColor(Color.BLACK);
int xOffset,yOffset;
if(Character.toUpperCase(orientation) == 'V')
{
for(int h = 0; h<height; h++)
{
if(h%2 == 0)
{
for(int w = 0; w<width; w++)
{
//find the top point
xOffset = xOff + w*(int)(Math.sqrt(3)*size);
yOffset = yOff + (h*3*size/2);
Point hexPoint = new Point(xOffset,yOffset);
//calculate the other points
VertHex hex = new VertHex(hexPoint,size);
//organize the points into a usable format
int[] xPoints = new int[6];
int[] yPoints = new int[6];
for(int i = 0; i<6; i++)
{
xPoints[i] = hex.getPoint(i).getX();
yPoints[i] = hex.getPoint(i).getY();
}
//print the hexagon
g.drawPolygon(xPoints,yPoints,6);
}
}
else
{
for(int w = 0; w<width-1; w++)
{
//find the top point
xOffset = xOff + w*(int)(Math.sqrt(3)*size) + (int)(size*Math.sqrt(3)/2);
yOffset = yOff + (h*3*size/2);
Point hexPoint = new Point(xOffset,yOffset);
//calculate the other points
VertHex hex = new VertHex(hexPoint,size);
//organize the points into a usable format
int[] xPoints = new int[6];
int[] yPoints = new int[6];
for(int i = 0; i<6; i++)
{
xPoints[i] = hex.getPoint(i).getX();
yPoints[i] = hex.getPoint(i).getY();
}
//print the hexagon
g.drawPolygon(xPoints,yPoints,6);
}
}
}
}
else //FLAT ORIENTATION
{
for(int w = 0; w<width; w++)
{
if(w%2 == 0)
{
for(int h = 0; h<height; h++)
{
//find the top left point
xOffset = xOff + (int)(w*3*size/2);
yOffset = yOff + (int)(h*Math.sqrt(3)*size);
Point hexPoint = new Point(xOffset,yOffset);
//calculate the other points
FlatHex hex = new FlatHex(hexPoint,size);
//organize the points into a usable format
int[] xPoints = new int[6];
int[] yPoints = new int[6];
for(int i = 0; i<6; i++)
{
xPoints[i] = hex.getPoint(i).getX();
yPoints[i] = hex.getPoint(i).getY();
}
//print the hexagon
g.drawPolygon(xPoints,yPoints,6);
}
}
else
{
for(int h = 0; h<height-1; h++)
{
//find the top left point
xOffset = xOff + (int)(w*3*size/2);
yOffset = yOff + (int)(h*Math.sqrt(3)*size + Math.sqrt(3)*size/2);
Point hexPoint = new Point(xOffset,yOffset);
//calculate the other points
FlatHex hex = new FlatHex(hexPoint,size);
//organize the points into a usable format
int[] xPoints = new int[6];
int[] yPoints = new int[6];
for(int i = 0; i<6; i++)
{
xPoints[i] = hex.getPoint(i).getX();
yPoints[i] = hex.getPoint(i).getY();
}
//print the hexagon
g.drawPolygon(xPoints,yPoints,6);
}
}
}
}
}
public void setOrientation(char c)
{
if(c == 'v' || c == 'V' || c == 'h' || c == 'H')
{
orientation = c;
}
}
}
BoardFrame:
package groupchatclient;
import java.awt.*;
import java.io.BufferedReader;
import java.io.PrintWriter;
import javax.swing.*;
public class BoardFrame extends JFrame
{
public BoardFrame(BufferedReader in, PrintWriter out)
{
super("Pentarius");
initComponents();
this.out = out;
this.in = in;
}
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
mainScrollPane = new javax.swing.JScrollPane();
mainDisplayPane = new javax.swing.JPanel();
filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 10), new java.awt.Dimension(0, 10), new java.awt.Dimension(32767, 10));
searchScrollPane = new javax.swing.JScrollPane();
searchTextArea = new javax.swing.JTextArea();
chatTextField = new javax.swing.JTextField();
chatScrollPane = new javax.swing.JScrollPane();
chatTextArea = new javax.swing.JTextArea();
searchTextField = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(mainDisplayPane);
mainDisplayPane.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 817, Short.MAX_VALUE)
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 622, Short.MAX_VALUE)
);
mainScrollPane.setViewportView(mainDisplayPane);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(mainScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 809, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(filler1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(6, 6, 6))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(filler1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(mainScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 613, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
searchTextArea.setColumns(20);
searchTextArea.setRows(5);
searchScrollPane.setViewportView(searchTextArea);
chatTextField.setText("");
chatTextField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
out.println(chatTextField.getText());
chatTextField.setText("");
}
});
chatTextArea.setColumns(20);
chatTextArea.setRows(5);
chatScrollPane.setViewportView(chatTextArea);
searchTextField.setText("");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(searchScrollPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(chatScrollPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(searchTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(chatTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 5, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(searchScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 252, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(searchTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(chatScrollPane)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(chatTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(PentariusFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(PentariusFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(PentariusFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(PentariusFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new PentariusFrame().setVisible(true);
}
});
}
public void setFiller(javax.swing.Box.Filler fill) {
filler1 = fill;
}
public void setPanel1(javax.swing.JPanel panel) {
jPanel1 = panel;
}
public void setMainDisplayPane(javax.swing.JPanel panel) {
mainDisplayPane = panel;
}
public void setSearchScrollPane(javax.swing.JScrollPane scroll) {
searchScrollPane = scroll;
}
public void setChatScrollPane(javax.swing.JScrollPane scroll) {
chatScrollPane = scroll;
}
public void setMainScrollPane(javax.swing.JScrollPane scroll) {
mainScrollPane = scroll;
}
public void setSearchTextArea(javax.swing.JTextArea area) {
searchTextArea = area;
}
public void setChatTextArea(javax.swing.JTextArea area) {
chatTextArea = area;
}
public void setChatTextField(javax.swing.JTextField field) {
chatTextField = field;
}
public void setSearchTextField(javax.swing.JTextField field) {
searchTextField = field;
}
public javax.swing.Box.Filler getFiller() {
return filler1;
}
public javax.swing.JPanel getMainDisplayPane() {
return mainDisplayPane;
}
public javax.swing.JPanel getJPanel1() {
return jPanel1;
}
public javax.swing.JScrollPane getSearchScrollPane() {
return searchScrollPane;
}
public javax.swing.JScrollPane getChatScrollPane() {
return chatScrollPane;
}
public javax.swing.JScrollPane getMainScrollPane() {
return mainScrollPane;
}
public javax.swing.JTextArea getSearchTextArea() {
return searchTextArea;
}
public javax.swing.JTextArea getChatTextArea() {
return chatTextArea;
}
public javax.swing.JTextField getSearchTextField() {
return searchTextField;
}
public javax.swing.JTextField getChatTextField() {
return chatTextField;
}
// Variables declaration - do not modify
private javax.swing.Box.Filler filler1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel mainDisplayPane;
private javax.swing.JScrollPane searchScrollPane;
private javax.swing.JScrollPane chatScrollPane;
private javax.swing.JScrollPane mainScrollPane;
private javax.swing.JTextArea searchTextArea;
private javax.swing.JTextArea chatTextArea;
private javax.swing.JTextField chatTextField;
private javax.swing.JTextField searchTextField;
private final PrintWriter out;
private final BufferedReader in;
// End of variables declaration
}
编辑:告诉客户绘制的逻辑
boolean gameRunning = false;
// Process all messages from server, according to the protocol.
while (!gameRunning) {
String line = in.readLine();
if (line.startsWith("SUBMITNAME")) {
out.println(name = getName());
} else if (line.startsWith("NAMEACCEPTED")) {
textField.setEditable(true);
} else if (line.startsWith("MESSAGE")) {
messageArea.append(line.substring(8) + "\n");
} else if (line.startsWith("ALL DRAW")){
gameRunning = true;
draw(this);
} else if (line.startsWith("ALL ORIENTATION")) {
if(line.contains("POINT")) {
bp.orientation = 'V';
} else {
bp.orientation = 'H';
}
} else if (line.startsWith("ALL HEIGHT")) {
bp.height = parseInt(line.substring(11));
} else if (line.startsWith("ALL WIDTH")) {
bp.width = parseInt(line.substring(10));
} else if (line.startsWith("ALL BHEIGHT")) {
bp.yOff = parseInt(line.substring(12));
} else if (line.startsWith("ALL BWIDTH")) {
bp.xOff = parseInt(line.substring(11));
} else if(line.startsWith("ALL SIZE")) {
bp.size = parseInt(line.substring(9));
} else if(line.startsWith("CMD HOST REQUEST")) {
players = parseInt(line.substring(17));
isHost = true;
out.println(".REQUEST READY");
} else if(line.startsWith("ALL REQUEST")) {
textField.setEditable(false);
int answer = getRequest();
if(answer == JOptionPane.YES_OPTION)
out.println(".YES");
else
out.println(".NO");
} else if(line.startsWith("ALL YES")) {
if(isHost) {
votes++;
if(votes==players) {
out.println(".REQUEST PASSED");
}
}
} else if(line.startsWith("ALL NO")) {
if(isHost) {
out.println(".REQUEST FAILED");
}
} else if(line.startsWith("ALL RESET")) {
textField.setEditable(true);
out.println("Vote Failed");
} else if(line.startsWith("CMD INFO")) {
messageArea.append("Height: "+bp.height+"\nWidth: "+bp.width+"\nOrientation: ");
if(bp.orientation == 'v')
messageArea.append("Pointed \nX Border: "+bp.xOff+"\nY Border: "+bp.yOff);
else
messageArea.append("Flat \nX Border: "+bp.xOff+"\nY Border: "+bp.yOff);
}
}
编辑:尝试SwingWorker(强调尝试)
public void draw(GroupChatClient client) throws Exception
{
SwingWorker worker = new SwingWorker()
{
@Override
protected Object doInBackground() throws Exception
{
frame.setVisible(false);
BoardFrame bf = new BoardFrame(in,out);
bf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
bf.setMainDisplayPane(bp);
if(bp.orientation == 'V')
{
panelSizeX=2*(int)(bp.width*Math.sqrt(3)*bp.size/2);
panelSizeY= 2*bp.yOff + 2*bp.size*bp.height;
}
else //orientation = FLAT
{
panelSizeX= 2*(bp.yOff)+ (bp.size*bp.width);
panelSizeY= 2*(int)(bp.height*Math.sqrt(3)*bp.size/2);
}
System.out.println(panelSizeX + " " + panelSizeY);
bf.getChatTextArea().setEditable(false);
bf.getSearchTextArea().setEditable(false);
DefaultCaret newCaret = (DefaultCaret)bf.getChatTextArea().getCaret();
newCaret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
bf.getChatTextArea().setLineWrap(true);
bf.getSearchTextArea().setLineWrap(true);
//bf.getChatTextField().addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent e) {
// out.println(bf.getChatTextField().getText());
// bf.getChatTextField().setText("");
// }
//});
bf.pack();
bp.setVisible(true);
bf.setVisible(true);
while(true)
{
String line = in.readLine();
if (line.startsWith("MESSAGE")) {
bf.getChatTextArea().append(line.substring(8) + "\n");
}
}
}
};
worker.execute();
}