The Artima Developer Community
Sponsored Link

Java Answers Forum
my program is a mess!!!

1 reply on 1 page. Most recent reply: Apr 7, 2004 7:16 AM by Alex Blewitt

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
john mcginlay

Posts: 2
Nickname: stub20000
Registered: Apr, 2004

my program is a mess!!! Posted: Apr 6, 2004 10:50 AM
Reply to this message Reply
Advertisement
hi, i have taken pieces from another program and put them in my program (very very badly), i cant figure this out at all, can anybody help me, PLEASE!!
<br>
my program allows users to move images from one square to another, im trying to constrict the images to one square at a time, i cant do it, any help please???<br>
<br>

<java>
import java.applet.*;<br>
import java.awt.*;<br>
import java.awt.event.*;<br>
import java.awt.image.*;<br>
<br>
public class program extends Applet implements MouseListener, MouseMotionListener {<br>
private Point counter1, counter2, counter3, counter4, counter5, mouse;<br>
private int select;<br>
private Image cabinet, bookcase, wardrobe, piano, chest;<br>
<br>

int<br>
pi,<br>
pi1,<br>
ro1 = 0,<br>
co1 = 0,<br>
ro2 = 0,<br>
co2 = 0,<br>
loaded=0,<br>
wa=0,<br>
done,<br>
move=0,<br>
number=0,<br>
inst=0,<br>
starter=0,<br>



int be[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,
18,18,18,18,18,18,18,18,18,18,18,18,18,18,18, 18 };<br>

int b[]= { 25, 55,100, 55,173, 55,246, 55,319, 55,392, 55,465, 55,
25,127,100,127, 392,127,465,127,
25,199,100,199, 392,199,465,199,

164, 126, 204, 126, 244, 126, 284, 126, 324, 126,
164, 166, 204, 166, 244, 166, 284, 166, 324, 166,
164, 206, 204, 206, 244, 206, 284, 206, 324, 206 };<br>

int ran[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };<br>

<br>


public void init() {<br>
this.addMouseMotionListener (this);<br>
this.addMouseListener (this);<br>
select = 0;<br>
cabinet = getImage (getDocumentBase(), "cabinet.jpg") ;<br>
bookcase = getImage (getDocumentBase(), "bookcase.jpg");<br>
wardrobe = getImage (getDocumentBase(), "wardrobe.jpg");<br>
piano = getImage (getDocumentBase(), "piano.jpg");<br>
chest = getImage (getDocumentBase(), "chest.jpg");<br>
counter1 = new Point(17, 24);<br>
counter2 = new Point(155, 130);<br>
counter3 = new Point(85, 130);<br>
counter4 = new Point(155, 24);<br>
counter5 = new Point(17, 130);<br>
mouse = new Point();<br>
}<br>
<br>
public void paint (Graphics g) {<br>
drawBox (g);<br>
g.drawImage (cabinet, counter1.x, counter1.y, 60, 60, this); <br>
g.drawImage (bookcase, counter2.x, counter2.y, 60, 60, this);<br>
g.drawImage (wardrobe, counter3.x, counter3.y, 60, 60, this);<br>
g.drawImage (piano, counter4.x, counter4.y, 60, 60, this);<br>
g.drawImage (chest, counter5.x, counter5.y, 60, 60, this);<br>
g.drawString ("1", 42, 113);<br>
g.drawString ("2", 113, 113);<br>
g.drawString ("3", 180, 113);<br>
g.drawString ("4", 42, 216);<br>
g.drawString ("5", 113, 216);<br>
g.drawString ("6", 180, 216);<br>
}<br>
<br>
public void paint(Graphics g) {<br>
if(wave==0 && inst==0) {<br>
offScrGr.setColor(Color.blue);<br>
offScrGr.fillRect(0, 0, 513, 248);<br>
offScrGr.setColor(Color.red);<br>
offScrGr.drawRect( 154, 116, 201, 121); <br>
<br>
for(int i=0; i<60; i+=2) { // draw squares if piece is missing<br>
if(be[i/2] == 18) offScrGr.drawRect( b, b[i+1], 20, 20);<br>
}<br>
for(int i=0; i<60; i+=2) <br>{ // draw pieces<br>
if(be[i/2] != 18) {<br>
offScrGr.drawImage(imgs[be[i/2]], b-24, b[i+1]-24, this);<br>
}<br>
}<br>
<br>


public boolean handleEvent(Event evt) {<br>

if (evt.id == Event.MOUSE_UP && inst==0 && wave==0) {<br>
ro1 = evt.y; co1 = evt.x;<br>
}<br>
<br>
if (evt.id == Event.MOUSE_DOWN && inst==0) {<br>
ro1 = evt.y; co1 = evt.x; <br>
<br>
if(wave==1) {<br>
if(co1>=222 && co1<=351 && ro1>=205 && ro1<=218 && wave==1) {<br>
getAppletContext().showDocument(currURL);<br>
}<br>
}<br>
<br>


if(wave==0) {<br>
for(int i=0; i<60; i+=2) {<br>
if(co1>=b-10 && co1<=b+30 && ro1>=b[i+1]-10 && ro1<=b[i+1]+30){<br>
if((be[i/2]<18)) {<br>
co1=b; ro1=b[i+1]; pi=be[i/2]; pi1=i/2; <br>move=1;<br>
be[30]=pi;<br>
break;<br>
}<br>
if(be[i/2]==18 && (move==1)) { <br>
be[i/2]=pi; be[pi1]=18; move=0; number++;<br>

<br>



public void mouseDragged (MouseEvent e) {<br>
mouse = e.getPoint();
//continuously change the coordinates of the selected counter<br>
if (select ==1) counter1 = mouse;<br>
if (select ==2) counter2 = mouse;<br>
if (select ==3) counter3 = mouse;<br>
if (select ==4) counter4 = mouse;<br>
if (select ==5) counter5 = mouse;<br>
repaint();<br>
}<br>
<br>
public void mouseMoved (MouseEvent e) {} //required for the interface<br>
public void mousePressed (MouseEvent e) { //select a counter using the mouse<br>
mouse = e.getPoint();<br>
if (mouse.x > counter1.x - 60 && mouse.x < counter1.x +60 && mouse.y >counter1.y - 60 && mouse.y < counter1.y +60) select = 1;<br>
if (mouse.x > counter2.x - 60 && mouse.x < counter2.x +60 && mouse.y >counter2.y - 60 && mouse.y < counter2.y +60) select = 2;<br>
if (mouse.x > counter3.x - 60 && mouse.x < counter3.x +60 && mouse.y >counter3.y - 60 && mouse.y < counter3.y +60) select = 3;<br>
if (mouse.x > counter4.x - 60 && mouse.x < counter4.x +60 && mouse.y >counter4.y - 60 && mouse.y < counter4.y +60) select = 4;<br>
if (mouse.x > counter5.x - 60 && mouse.x < counter5.x +60 && mouse.y >counter5.y - 60 && mouse.y < counter5.y +60) select = 5;<br>
}//required for the interface

public void mouseClicked (MouseEvent e){}<br>
public void mouseReleased (MouseEvent e) {}<br>
public void mouseEntered (MouseEvent e) {}<br>
public void mouseExited (MouseEvent e){}<br>
public void drawBox (Graphics g) {<br>
for (int i = 10; i <= 220; i += 105) {<br>
g.drawLine (10, i, 220, i);<br>
}<br>
<br>
for (int l = 10; l <= 220; l += 70) {<br>
g.drawLine (l, 10, l, 220);<br>}
}<br>
} </java>


Alex Blewitt

Posts: 44
Nickname: alblue
Registered: Apr, 2003

Re: my program is a mess!!! Posted: Apr 7, 2004 7:16 AM
Reply to this message Reply
Throw it away and start again. If you can't figure out what it does, then chances are neither is anyone else.

1) Figure out what you want your code to do.
2) Design it using named methods (preferably with JavaDoc). That will help you figure out what each part is doing.
3) Consider using named constants for fixed values. Numbers really don't mean anything.

Lastly, no-one is going to be of much help if you've just randomly copied and pasted code, particularly if it doesn't work. You've got two 'paint' methods, and in one of them you refer to a non-existant off-screen graphic object.

Read up on
http://java.sun.com/docs/books/tutorial/applet/overview/index.html
and
http://java.sun.com/docs/books/tutorial/applet/overview/componentMethods.html

then throw the lot away and start again.

Flat View: This topic has 1 reply on 1 page
Topic: "Mutually Exclusive Sets" error Previous Topic   Next Topic Topic: JSlider that allows a range of gamma

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use