checkers/ draughts game ? can this work or am i on the wrong path [closed] - chess

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
import java.awt.*;
import java.awt.event.*;
import javax.swing.Action;
import java.util.*;
import java.io.*;
import javax.swing.*;
public class testout extends JFrame{
public JLabel l1;
//declaring images
ImageIcon a= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b1.png");
ImageIcon b= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b2.png");
ImageIcon c= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b3.png");
ImageIcon d= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b4.png");
ImageIcon e= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b5.png");
ImageIcon f= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b6.png");
ImageIcon g= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b7.png");
ImageIcon h= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b8.png");
ImageIcon i= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b9.png");
ImageIcon j= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b10.png");
ImageIcon k= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b11.png");
ImageIcon l= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b12.png");
public testout(){
super("Checkers v0.1 beta");//name of window
try{
JFrame checkerboard = new JFrame();
checkerboard.setSize(400, 400);
checkerboard.setLayout(new GridLayout(8,8));
checkerboard.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
checkerboard.setResizable(false);
JLabel b1= new JLabel();
b1.setBackground(Color.black);
checkerboard.add(b1);
JLabel l1=new JLabel();
l1.setIcon(a); //sets icon for label
checkerboard.add(l1);
//mouse event
JLabel b2=new JLabel();
checkerboard.add(b2);
b2.setBackground(Color.BLACK);
JLabel l2=new JLabel();
l2.setIcon(b);
checkerboard.add(l2);
JLabel b3=new JLabel();
checkerboard.add(b3);
b3.setBackground(Color.BLACK);
JLabel l3=new JLabel();
l3.setIcon(c);
checkerboard.add(l3);
JLabel b4=new JLabel();
checkerboard.add(b4);
b4.setBackground(Color.BLACK);
JLabel l4=new JLabel();
l4.setIcon(d);
checkerboard.add(l4);
JLabel l5=new JLabel();
l5.setIcon(e);
checkerboard.add(l5);
JLabel b5=new JLabel();
checkerboard.add(b5);
b5.setBackground(Color.BLACK);
JLabel l6=new JLabel();
l6.setIcon(f);
checkerboard.add(l6);
JLabel b6=new JLabel();
checkerboard.add(b6);
b6.setBackground(Color.BLACK);
JLabel l7=new JLabel();
l7.setIcon(g);
checkerboard.add(l7);
JLabel b7=new JLabel();
checkerboard.add(b7);
b7.setBackground(Color.BLACK);
JLabel l8=new
JLabel(); l8.setIcon(h); checkerboard.add(l8); JLabel b8=new
JLabel(); checkerboard.add(b8); b8.setBackground(Color.BLACK);
JLabel b9= new JLabel(); checkerboard.add(b9);
b9.setBackground(Color.BLACK); JLabel l9=new JLabel();
l9.setIcon(i); checkerboard.add(l9);
JLabel b10=new JLabel();
checkerboard.add(b10); b10.setBackground(Color.BLACK); JLabel
l10=new JLabel(); l10.setIcon(j); checkerboard.add(l10); JLabel
b11=new JLabel(); checkerboard.add(b11);
b11.setBackground(Color.BLACK); JLabel l11=new JLabel();
l11.setIcon(k); checkerboard.add(l11);
JLabel b12=new JLabel();
checkerboard.add(b12); b12.setBackground(Color.BLACK); JLabel
l12=new JLabel(); l12.setIcon(l); checkerboard.add(l12);
JLabel
l13=new JLabel("13"); checkerboard.add(l13); JLabel b13=new
JLabel("13"); checkerboard.add(b13); b13.setBackground(Color.BLACK);
JLabel l14=new JLabel("14"); checkerboard.add(l14); JLabel b14=new
JLabel("14"); checkerboard.add(b14); b14.setBackground(Color.BLACK);
JLabel l15=new JLabel("15"); checkerboard.add(l15); JLabel b15=new
JLabel("15"); checkerboard.add(b15); b15.setBackground(Color.BLACK);
JLabel l16=new JLabel("16"); checkerboard.add(l16); JLabel b16= new
JLabel("16"); checkerboard.add(b16); b16.setBackground(Color.BLACK);
JLabel b17=new JLabel("17"); checkerboard.add(b17);
b17.setBackground(Color.BLACK); JLabel l17=new JLabel("17");
checkerboard.add(l17);
JLabel b18=new JLabel("18");
checkerboard.add(b18); b18.setBackground(Color.BLACK); JLabel
l18=new JLabel("18"); checkerboard.add(l18);
JLabel b19=new
JLabel("19"); checkerboard.add(b19); b19.setBackground(Color.BLACK);
JLabel l19=new JLabel("19"); checkerboard.add(l19);
JLabel b20=new
JLabel("20"); checkerboard.add(b20); b20.setBackground(Color.BLACK);
JLabel l20=new JLabel("20"); checkerboard.add(l20);
JLabel l21=new
JLabel("21"); checkerboard.add(l21); JLabel b21=new JLabel("21");
checkerboard.add(b21); b21.setBackground(Color.BLACK);
JLabel
l22=new JLabel("22"); checkerboard.add(l22); JLabel b22=new
JLabel("22"); checkerboard.add(b22); b22.setBackground(Color.BLACK);
JLabel l23=new JLabel("23"); checkerboard.add(l23); JLabel b23= new
JLabel("23"); checkerboard.add(b23); b23.setBackground(Color.BLACK);
JLabel l24=new JLabel("24"); checkerboard.add(l24); JLabel b24=new
JLabel("24"); checkerboard.add(b24); b24.setBackground(Color.BLACK);
JLabel b25=new JLabel("25"); checkerboard.add(b25);
b25.setBackground(Color.BLACK); JLabel l25=new JLabel("25");
checkerboard.add(l25);
JLabel b26=new JLabel("26");
checkerboard.add(b26); b26.setBackground(Color.BLACK); JLabel
l26=new JLabel("26"); checkerboard.add(l26);
JLabel b27=new
JLabel("27"); b27.setBackground(Color.BLACK); checkerboard.add(b27);
JLabel l27=new JLabel("27"); checkerboard.add(l27);
JLabel b28=new
JLabel("28"); checkerboard.add(b28); b28.setBackground(Color.BLACK);
JLabel l28=new JLabel("28"); checkerboard.add(l28);
JLabel l29=new
JLabel("29"); checkerboard.add(l29); JLabel b29=new JLabel("29");
checkerboard.add(b29); b29.setBackground(Color.BLACK);
JLabel
l30=new JLabel("30"); checkerboard.add(l30); JLabel b30= new
JLabel("30"); checkerboard.add(b30); b30.setBackground(Color.BLACK);
JLabel l31=new JLabel("31"); checkerboard.add(l31); JLabel b31=new
JLabel("31"); checkerboard.add(b31); b31.setBackground(Color.BLACK);
JLabel l32=new JLabel("32"); checkerboard.add(l32); JLabel b32=new
JLabel("32"); checkerboard.add(b32); b32.setBackground(Color.BLACK);
checkerboard.setVisible(true); //event handlers
moves move=new moves(); //listener
l1.addMouseListener(move);
l1.addMouseMotionListener(move);
l12.addMouseMotionListener(move);
l12.addMouseListener(move);
} catch(Exception w){ }
}
public class moves implements MouseListener,MouseMotionListener{
public void mouseClicked(MouseEvent z){
if(l1.contains(z.getX(),z.getY()))updateLocation(z);
}
public void mousePressed(MouseEvent z){ }
public void mouseDragged(MouseEvent z){ updateLocation(z); }
public void mouseReleased(MouseEvent z){ }
public void mouseEntered(MouseEvent z){}
public void mouseExited(MouseEvent z){}
public void mouseMoved(MouseEvent z){ }
public void updateLocation(MouseEvent z){
l1.setLocation(z.getX(), z.getY());
if(l1.contains(z.getX(),z.getY())) {
} else {
}
repaint();
}
}
}

Maybe the most important programming principle for you to learn is Don't Repeat Yourself (DRY). Notice how repetitive your code is.
In the first block, you declare 12 variables (named a, b, c, etc.), that only vary on the member names and the file names. You should replace repeated data structures with collections like arrays and repeated code with loops. For example:
private ImageIcon[] icons;
// Then, in your constructor
for (int i = 0; i < 12; i++) {
icons[i] = new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b" + (i + 1).toString() + ".png");
}
Notice how 12 lines of code can be replaced with 4. Consider replacing the rest of your repetitive code and data with loops and collections. Otherwise, you may find the code too big to manage and too time-intensive to change.

Related

wxWidgets. Creating a new page within wxAuiNotebook on command

How do I add a page to wxAUINoteBook? I wish to be able to click: File->New, and then have a new tab pop in. I understand how to do this using wxAuiMDIChildFrame within a parent frame but the child frame is a class on its own and a new page is not. Here is what I have so far.
#include "main.h"
MyFrame::MyFrame()
: wxFrame(nullptr, wxID_ANY, "AUItest", wxPoint(50, 50), wxSize(800, 450), wxDEFAULT_FRAME_STYLE)
{
wxMenuBar* menubar = new wxMenuBar;
wxMenu* menufile = new wxMenu;
menufile->Append(1001, "New");
menufile->Append(1002, "Open");
menufile->Append(1003, "Save");
menufile->Append(1004, "Exit");
menubar->Append(menufile, "File");
SetMenuBar(menubar);
wxAuiNotebook* notebook = new wxAuiNotebook(this, -1, wxPoint(-1, -1),
wxSize(-1, -1), wxNB_TOP | wxAUI_NB_CLOSE_ON_ACTIVE_TAB);
wxPanel* panel1 = new wxPanel(notebook, wxID_ANY, wxDefaultPosition, wxDefaultSize);
wxPanel* panel2 = new wxPanel(notebook, wxID_ANY, wxDefaultPosition, wxDefaultSize);
wxPanel* panel3 = new wxPanel(notebook, wxID_ANY, wxDefaultPosition, wxDefaultSize);
notebook->AddPage(panel1, wxT("Sheet1"));
notebook->AddPage(panel2, wxT("Sheet2"));
notebook->AddPage(panel3, wxT("Sheet3"));
Center();
}
MyFrame::~MyFrame()
{
}
wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(1001, MyFrame::OnMenuNew)
EVT_MENU(1002, MyFrame::OnMenuOpen)
EVT_MENU(1003, MyFrame::OnMenuSave)
EVT_MENU(1004, MyFrame::OnMenuExit)
wxEND_EVENT_TABLE()
void MyFrame::OnMenuNew(wxCommandEvent& event)
{
//How do I add a page on command?
}
void MyFrame::OnMenuOpen(wxCommandEvent& event)
{
}
void MyFrame::OnMenuSave(wxCommandEvent& event)
{
}
void MyFrame::OnMenuExit(wxCommandEvent& event)
{
Close(true);
event.Skip();
}
Thanks for the help,
I'm still learning.

Correct way to serialize a JPanel?

I am sending my class which extends JPanel through an ObjectOutputStream to the client, so that their GUI will 'load'.
public class ClickOptionPanel extends JPanel implements Serializable {
private Font font = new Font("Arial", Font.BOLD, 13);
private JLabel clickerTitle;
private JSlider clickerMaxSlider, clickerMinSlider;
private JTextField clickerMinField, clickerMaxField;
private JCheckBox clickInsideMinecraft, autoBlock;
public ClickOptionPanel() {
setLayout(new BorderLayout());
add(getClickerTitlePanel(), BorderLayout.NORTH);
add(getClickerSliderPanel(), BorderLayout.CENTER);
add(getCheckBoxPanel(), BorderLayout.SOUTH);
}
private JPanel getClickerTitlePanel() {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEtchedBorder());
clickerTitle = new JLabel("Clicker Options");
clickerTitle.setFont(font);
panel.add(clickerTitle);
return panel;
}
private JPanel getClickerSliderPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
clickerMaxSlider = new JSlider(0, 200, 125);
clickerMaxSlider.setPreferredSize(new Dimension(300, 45));
clickerMinSlider = new JSlider(0, 200, 75);
clickerMinSlider.setPreferredSize(new Dimension(300, 45));
panel.add(clickerMaxSlider, BorderLayout.NORTH);
panel.add(clickerMinSlider, BorderLayout.CENTER);
panel.add(getClickFieldPanel(), BorderLayout.SOUTH);
return panel;
}
private JPanel getClickFieldPanel() {
JPanel panel = new JPanel();
clickerMaxField = new JTextField(10);
clickerMaxField.setFont(font);
clickerMaxField.setHorizontalAlignment(SwingConstants.CENTER);
clickerMinField = new JTextField(10);
clickerMinField.setFont(font);
clickerMinField.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(clickerMinField);
panel.add(clickerMaxField);
return panel;
}
private JPanel getCheckBoxPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setBorder(BorderFactory.createEtchedBorder());
clickInsideMinecraft = new JCheckBox("Only click inside of Minecraft: ");
clickInsideMinecraft.setHorizontalTextPosition(SwingConstants.LEFT);
autoBlock = new JCheckBox("Enable auto-block: ");
autoBlock.setHorizontalTextPosition(SwingConstants.LEFT);
panel.add(clickInsideMinecraft, BorderLayout.NORTH);
panel.add(autoBlock, BorderLayout.SOUTH);
return panel;
}
There is the JPanel which I am sending. I create a new instance of it before it is sent.
The JPanel is turning up blank - why is this?
I was doing everything right - it was just that on the client side I forgot to set the ois.readObject object to the panel!

How to remove one indirectly referenced image from a PDF and keep all others?

I would like to parse a PDF and find the logo via known attributes and when I find a match, remove that image and then copy everything else.
I am using the code below to replace an image with a blank white image to remove a logo from PDFs that are to be printed on letterhead. It replaces the image with a white image of the same size. Is there a way to modify this to actually remove the image (and thus save some space, etc.?).
private static void Main(string[] args)
{
ManipulatePdf(#"C:\in.pdf", #"C:\out.pdf");
Console.WriteLine("Finished - press a key");
Console.ReadKey();
}
public static void ManipulatePdf(String src, String dest)
{
Console.WriteLine("Start");
PdfReader reader = new PdfReader(src);
// first read all references and find the one we wish to work on.
PdfDictionary page = reader.GetPageN(1); // all resources are available to every page (?)
PdfDictionary resources = page.GetAsDict(PdfName.RESOURCES);
PdfDictionary xobjects = resources.GetAsDict(PdfName.XOBJECT);
page = reader.GetPageN(1);
resources = page.GetAsDict(PdfName.RESOURCES);
xobjects = resources.GetAsDict(PdfName.XOBJECT);
foreach (PdfName pdfName in xobjects.Keys)
{
PRStream stream = (PRStream) xobjects.GetAsStream(pdfName);
if (stream.Length > 100000)
{
PdfImage image = new PdfImage(MakeBlankImg(), "", null);
Console.WriteLine("Calling replace stream");
ReplaceStream(stream, image);
}
}
PdfStamper stamper = new PdfStamper(reader, new FileStream(dest, FileMode.Create));
stamper.Close();
reader.Close();
}
public static iTextSharp.text.Image MakeBlankImg()
{
Console.WriteLine("Making small blank image");
byte[] array;
using (MemoryStream ms = new MemoryStream())
{
//var drawingImage = image.GetDrawingImage();
using (Bitmap newBi = new Bitmap(1, 1))
{
using (Graphics g = Graphics.FromImage(newBi))
{
g.Clear(Color.White);
g.Flush();
}
newBi.Save(ms, ImageFormat.Jpeg);
}
array = ms.ToArray();
}
Console.WriteLine("Image array is " + array.Length + " bytes.");
return iTextSharp.text.Image.GetInstance(array);
}
public static void ReplaceStream(PRStream orig, PdfStream stream)
{
orig.Clear();
MemoryStream ms = new MemoryStream();
stream.WriteContent(ms);
orig.SetData(ms.ToArray(), false);
Console.WriteLine("Iterating keys");
foreach (KeyValuePair<PdfName, PdfObject> keyValuePair in stream)
{
Console.WriteLine("Key: " + keyValuePair.Key.ToString());
orig.Put(keyValuePair.Key, stream.Get(keyValuePair.Key));
}
}
}

saving a text file using JFileChooser

I am writing a small program where are user enters some information into text fields and then has the option to save that information using JFileChooser. I have not been able to figure out how to get it to save. What am I doing wrong? Thank you in advance.
public class Program implements ActionListener{
JMenuItem loadMenuItem, saveMenuItem, quitMenuItem;
JFrame frame;
JPanel mainPanel, inputPanel, listPanel;
JLabel firstNameLabel, lastNameLabel, rentLabel, listLabel;
JTextField firstNameTextField, lastNameTextField, rentTextField;
JButton addButton;
JList list;
DefaultListModel<TenantInfo> listModel;
JScrollPane scrollPane;
public ArrayList<TenantInfo>tenantInfo;
private void saveFile() {
// TODO Auto-generated method stub
JFileChooser jfc = new JFileChooser();
int userSelected = jfc.showSaveDialog(null);
if(userSelected == JFileChooser.APPROVE_OPTION){
try{
FileOutputStream fos = new FileOutputStream(jfc.getSelectedFile());
ObjectOutputStream oos = new ObjectOutputStream(fos);
for(TenantInfo t : tenantInfo){
oos.writeObject(t);
}
oos.writeObject(tenantInfo.get(0));
oos.close();
fos.close();
}catch(Exception ex){
ex.printStackTrace();
}
}

How can I show JDialog generated with JFormDesigner

I generated a JDialog w/ JFormDesigner, but I don't know how to display it. Here is the code:
public class FinishPopup extends JDialog {
public FinishPopup(Frame owner) {
super(owner);
initComponents();
}
public FinishPopup(Dialog owner) {
super(owner);
initComponents();
}
public void initComponents() {
dialogPane = new JPanel();
contentPanel = new JPanel();
label1 = new JLabel();
label2 = new JLabel();
buttonBar = new JPanel();
okButton = new JButton();
label3 = new JLabel();
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
{
{
label1.setText("Money: ");
label1.setFont(new Font("Tahoma", Font.PLAIN, 12));
label2.setText("Time");
label2.setFont(new Font("Tahoma", Font.PLAIN, 12));
GroupLayout contentPanelLayout = new GroupLayout(contentPanel);
contentPanel.setLayout(contentPanelLayout);
contentPanelLayout.setHorizontalGroup(
contentPanelLayout.createParallelGroup()
.addGroup(contentPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(label2)
.addGap(90, 90, 90)
.addComponent(label1)
.addContainerGap(105, Short.MAX_VALUE))
);
contentPanelLayout.setVerticalGroup(
contentPanelLayout.createParallelGroup()
.addGroup(contentPanelLayout.createSequentialGroup()
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(contentPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(label2)
.addComponent(label1))
.addContainerGap())
);
}
dialogPane.add(contentPanel, BorderLayout.CENTER);
{
buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
buttonBar.setLayout(new GridBagLayout());
((GridBagLayout)buttonBar.getLayout()).columnWidths = new int[] {0, 80};
((GridBagLayout)buttonBar.getLayout()).columnWeights = new double[] {1.0, 0.0};
okButton.setText("OK");
buttonBar.add(okButton, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0), 0, 0));
}
dialogPane.add(buttonBar, BorderLayout.SOUTH);
label3.setText("Finished");
label3.setFont(new Font("Tahoma", Font.BOLD, 14));
dialogPane.add(label3, BorderLayout.NORTH);
}
contentPane.add(dialogPane, BorderLayout.CENTER);
pack();
setLocationRelativeTo(getOwner());
}
private JPanel dialogPane;
private JPanel contentPanel;
private JLabel label1;
private JLabel label2;
private JPanel buttonBar;
private JButton okButton;
private JLabel label3;
}
How do I create and show it? thanks.
You need to first create an instance of the class and then call setVisible to make it visible
// owner is reference to the parent frame or dialog. May be null, but you'll
// need to cast it
FinishPopup popup = new FinishPopup(owner);
popup.setVisible(true);
You may also find setLocationRelativeTo helpful