July 30, 2003
Hej.
Jag försöker programmera ett brädspel (Thud) mha netbeans i java.
Jag tycker att spelplanen ser rätt så bra ut just nu, och jag har lyckats att fånga musklickningar precis som jag vill.
Det är rätt så mycket kod, men i stort sett så är det en tab-panel, och på en av sidorna finns det en bild av spelplanen som kan fånga knapptryckningar.
Vad jag inte lyckats göra är placera ut spelpjäser.
Så jag behöver lite tips och ideer om hur jag ska lyckas placera ut en spelpjäs, på en given koordinat på bilden som laddas i början av "public Thud()"
Om jag bara lyckas placera ut en bild på en koordinat (x,y) så är jag övertygad om att resten går utan problem.
/*
* Thud.java
*
* Created on den 8 augusti 2008, 15:37
*/
package ophesthud;
import javax.swing.*;
import java.awt.*;
/**
*
* @author Ophe
*/
public class Thud extends JFrame {
// Rad 1 heter rad 0
// 1 = Dvärg
// 2 = Troll
// 3 = Sten
public int[][] pieces = {
{0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 2, 3, 2, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}
};
/** Creates new form Thud */
public Thud() {
initComponents();
Board.setIcon(new ImageIcon("board.jpg"));
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
Tabs = new javax.swing.JTabbedPane();
TabBoard = new javax.swing.JPanel();
ScoreBar = new javax.swing.JProgressBar();
jScrollPane1 = new javax.swing.JScrollPane();
MovesList = new javax.swing.JTextArea();
BoardPanel = new javax.swing.JPanel();
Board = new javax.swing.JLabel();
TabChat = new javax.swing.JPanel();
TabRules = new javax.swing.JPanel();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea2 = new javax.swing.JTextArea();
TabAbout = new javax.swing.JPanel();
jScrollPane3 = new javax.swing.JScrollPane();
jTextArea3 = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Ophe's Thud");
setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
setMinimumSize(new java.awt.Dimension(200, 200));
getContentPane().setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 0, 0));
Tabs.setBackground(new java.awt.Color(153, 255, 255));
Tabs.setMinimumSize(new java.awt.Dimension(50, 50));
TabBoard.setBackground(new java.awt.Color(153, 255, 255));
TabBoard.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
TabBoard.setForeground(new java.awt.Color(153, 255, 255));
TabBoard.setLayout(new java.awt.BorderLayout());
ScoreBar.setMaximum(32);
ScoreBar.setMinimum(-32);
ScoreBar.setToolTipText("Score");
ScoreBar.setFocusable(false);
ScoreBar.setIndeterminate(true);
ScoreBar.setString("50");
TabBoard.add(ScoreBar, java.awt.BorderLayout.PAGE_END);
jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane1.setMaximumSize(new java.awt.Dimension(32767, 600));
MovesList.setColumns(20);
MovesList.setRows(5);
jScrollPane1.setViewportView(MovesList);
TabBoard.add(jScrollPane1, java.awt.BorderLayout.LINE_END);
BoardPanel.setLayout(new java.awt.GridLayout(1, 1));
Board.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
Board.setMaximumSize(new java.awt.Dimension(800, 800));
Board.setMinimumSize(new java.awt.Dimension(600, 600));
Board.setPreferredSize(new java.awt.Dimension(600, 600));
Board.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
BoardMouseClicked(evt);
}
});
BoardPanel.add(Board);
TabBoard.add(BoardPanel, java.awt.BorderLayout.CENTER);
Tabs.addTab("Board", TabBoard);
javax.swing.GroupLayout TabChatLayout = new javax.swing.GroupLayout(TabChat);
TabChat.setLayout(TabChatLayout);
TabChatLayout.setHorizontalGroup(
TabChatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 806, Short.MAX_VALUE)
);
TabChatLayout.setVerticalGroup(
TabChatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 620, Short.MAX_VALUE)
);
Tabs.addTab("Chat", TabChat);
jScrollPane2.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
jScrollPane2.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
jTextArea2.setColumns(20);
jTextArea2.setRows(5);
jScrollPane2.setViewportView(jTextArea2);
javax.swing.GroupLayout TabRulesLayout = new javax.swing.GroupLayout(TabRules);
TabRules.setLayout(TabRulesLayout);
TabRulesLayout.setHorizontalGroup(
TabRulesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(TabRulesLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 786, Short.MAX_VALUE)
.addContainerGap())
);
TabRulesLayout.setVerticalGroup(
TabRulesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(TabRulesLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 598, Short.MAX_VALUE)
.addContainerGap())
);
Tabs.addTab("Rules", TabRules);
jScrollPane3.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
jScrollPane3.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
jTextArea3.setColumns(20);
jTextArea3.setRows(5);
jScrollPane3.setViewportView(jTextArea3);
javax.swing.GroupLayout TabAboutLayout = new javax.swing.GroupLayout(TabAbout);
TabAbout.setLayout(TabAboutLayout);
TabAboutLayout.setHorizontalGroup(
TabAboutLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(TabAboutLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 786, Short.MAX_VALUE)
.addContainerGap())
);
TabAboutLayout.setVerticalGroup(
TabAboutLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(TabAboutLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 598, Short.MAX_VALUE)
.addContainerGap())
);
Tabs.addTab("About", TabAbout);
getContentPane().add(Tabs);
getAccessibleContext().setAccessibleDescription("Ophe's Thud");
pack();
}// </editor-fold>
private void BoardMouseClicked(java.awt.event.MouseEvent evt) {
int x = 1 + evt.getX() / 40;
int y = 1 + evt.getY() / 40;
MovesList.setText("(" + x + "," + y + ") : "+pieces[x-1][y-1]);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Thud().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel Board;
private javax.swing.JPanel BoardPanel;
private javax.swing.JTextArea MovesList;
private javax.swing.JProgressBar ScoreBar;
private javax.swing.JPanel TabAbout;
private javax.swing.JPanel TabBoard;
private javax.swing.JPanel TabChat;
private javax.swing.JPanel TabRules;
private javax.swing.JTabbedPane Tabs;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JTextArea jTextArea2;
private javax.swing.JTextArea jTextArea3;
// End of variables declaration
}
July 30, 2003
Jag har lyckats rita lite, men jag har ännu inte lyckats få fram en bild.
Mitt problem är dock att det jag har ritat försvinner efter ungefär 1 sekund. Klickar jag med musen så dyker det upp igen, men det försvinner igen rätt så fort. Jag vill att vad jag har ritat ska stanna kvar!
/*
* Thud.java
*
* Created on den 8 augusti 2008, 15:37
*/
package ophesthud;
import javax.swing.*;
import java.awt.*;
import java.applet.Applet;
import java.io.*;
import javax.imageio.*;
/**
*
* @author Ophe
*/
public class Thud extends JFrame {
public int mouseX;
public int mouseY;
// public Image DwarfIcon=new Image("4040dwarf.jpg");
// public ImageIcon TrollIcon=new ImageIcon("4040troll.jpg");
// Rad 1 heter rad 0
// 1 = Dvärg
// 2 = Troll
// 3 = Sten
public int[][] pieces = {
{0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 2, 3, 2, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}
};
/** Creates new form Thud */
public Thud() {
initComponents();
// Board.setIcon(new ImageIcon("board.jpg"));
// int i;
// int j;
// for ( i=0; i<15; i++ ) {
// for ( j=1; j<6; j++ ) {
// if (pieces[i][j]==1){
// mouseX=i;
// mouseY=j;
// System.out.println(i);
// Board.repaint();
// }
// }
// }
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
Tabs = new javax.swing.JTabbedPane();
TabBoard = new javax.swing.JPanel();
ScoreBar = new javax.swing.JProgressBar();
jScrollPane1 = new javax.swing.JScrollPane();
MovesList = new javax.swing.JTextArea();
BoardPanel = new javax.swing.JPanel();
Board = new javax.swing.JLabel();
TabChat = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
TabRules = new javax.swing.JPanel();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea2 = new javax.swing.JTextArea();
TabAbout = new javax.swing.JPanel();
jScrollPane3 = new javax.swing.JScrollPane();
jTextArea3 = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Ophe's Thud");
setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
setMinimumSize(new java.awt.Dimension(200, 200));
getContentPane().setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 0, 0));
Tabs.setBackground(new java.awt.Color(153, 255, 255));
Tabs.setMinimumSize(new java.awt.Dimension(50, 50));
TabBoard.setBackground(new java.awt.Color(153, 255, 255));
TabBoard.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
TabBoard.setForeground(new java.awt.Color(153, 255, 255));
TabBoard.setLayout(new java.awt.BorderLayout());
ScoreBar.setMaximum(32);
ScoreBar.setMinimum(-32);
ScoreBar.setToolTipText("Score");
ScoreBar.setFocusable(false);
ScoreBar.setIndeterminate(true);
ScoreBar.setString("50");
TabBoard.add(ScoreBar, java.awt.BorderLayout.PAGE_END);
jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane1.setMaximumSize(new java.awt.Dimension(32767, 600));
MovesList.setColumns(20);
MovesList.setRows(5);
jScrollPane1.setViewportView(MovesList);
TabBoard.add(jScrollPane1, java.awt.BorderLayout.LINE_END);
BoardPanel.setLayout(new java.awt.GridLayout(1, 1));
Board.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
Board.setMaximumSize(new java.awt.Dimension(800, 800));
Board.setMinimumSize(new java.awt.Dimension(600, 600));
Board.setPreferredSize(new java.awt.Dimension(600, 600));
Board.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
BoardMouseClicked(evt);
}
});
BoardPanel.add(Board);
TabBoard.add(BoardPanel, java.awt.BorderLayout.CENTER);
Tabs.addTab("Board", TabBoard);
jLabel1.setText("jLabel1");
javax.swing.GroupLayout TabChatLayout = new javax.swing.GroupLayout(TabChat);
TabChat.setLayout(TabChatLayout);
TabChatLayout.setHorizontalGroup(
TabChatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(TabChatLayout.createSequentialGroup()
.addGap(203, 203, 203)
.addComponent(jLabel1)
.addContainerGap(569, Short.MAX_VALUE))
);
TabChatLayout.setVerticalGroup(
TabChatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(TabChatLayout.createSequentialGroup()
.addGap(116, 116, 116)
.addComponent(jLabel1)
.addContainerGap(493, Short.MAX_VALUE))
);
Tabs.addTab("Chat", TabChat);
jScrollPane2.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
jScrollPane2.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
jTextArea2.setColumns(20);
jTextArea2.setRows(5);
jScrollPane2.setViewportView(jTextArea2);
javax.swing.GroupLayout TabRulesLayout = new javax.swing.GroupLayout(TabRules);
TabRules.setLayout(TabRulesLayout);
TabRulesLayout.setHorizontalGroup(
TabRulesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(TabRulesLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 786, Short.MAX_VALUE)
.addContainerGap())
);
TabRulesLayout.setVerticalGroup(
TabRulesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(TabRulesLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 601, Short.MAX_VALUE)
.addContainerGap())
);
Tabs.addTab("Rules", TabRules);
jScrollPane3.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
jScrollPane3.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
jTextArea3.setColumns(20);
jTextArea3.setRows(5);
jScrollPane3.setViewportView(jTextArea3);
javax.swing.GroupLayout TabAboutLayout = new javax.swing.GroupLayout(TabAbout);
TabAbout.setLayout(TabAboutLayout);
TabAboutLayout.setHorizontalGroup(
TabAboutLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(TabAboutLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 786, Short.MAX_VALUE)
.addContainerGap())
);
TabAboutLayout.setVerticalGroup(
TabAboutLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(TabAboutLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 601, Short.MAX_VALUE)
.addContainerGap())
);
Tabs.addTab("About", TabAbout);
getContentPane().add(Tabs);
getAccessibleContext().setAccessibleDescription("Ophe's Thud");
pack();
}// </editor-fold>
private void BoardMouseClicked(java.awt.event.MouseEvent evt) {
int x = 1 + evt.getX() / 40;
int y = 1 + evt.getY() / 40;
mouseX=x*40-10;
mouseY=y*40+35;
MovesList.setText("(" + x + "," + y + ") : "+pieces[x-1][y-1]);
repaint();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Thud().setVisible(true);
}
});
}
public void paint( Graphics g ) {
int i;
int j;
for ( i=0; i<15; i++ ) {
for (j=0; j<15;j++){
if (pieces[i][j]==1){
g.setColor(Color.blue);
g.fillOval(i*40+15, j*40+65, 30, 30);
} else if (pieces[i][j]==2){
g.setColor(Color.red);
g.fillOval(i*40+15, j*40+65, 30, 30);
}
}
}
// g.drawImage(DwarfIcon, mouseX, mouseY, 40, 40, this);
}
public void update ( Graphics g ) {
paint(g);
}
// Variables declaration - do not modify
private javax.swing.JLabel Board;
private javax.swing.JPanel BoardPanel;
private javax.swing.JTextArea MovesList;
private javax.swing.JProgressBar ScoreBar;
private javax.swing.JPanel TabAbout;
private javax.swing.JPanel TabBoard;
private javax.swing.JPanel TabChat;
private javax.swing.JPanel TabRules;
private javax.swing.JTabbedPane Tabs;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JTextArea jTextArea2;
private javax.swing.JTextArea jTextArea3;
// End of variables declaration
}
2 Guest(s)