Results 1 to 8 of 8
Thread: pls help!!
- 01-03-2011, 03:32 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
pls help!!
Hi there,
I am new in java, I have school problem stands like this:
Write a code for drawing two lines. By every click, a segment from a Line should be drawn which connect the current location with the previews one. The coordinates of the Lines must be stored at Line.txt. Put Buttons for clearing the display, writing the coordinates of the lines and calculating the distance between the lines.
my current code is as follows, but i am missing more than the half of the program, pls any help!!!
the code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class Domasna extends JFrame implements MouseListener{
int x,y,n,izbor;
int r=1;
public Domasna(){
setBackground(Color.RED);
setForeground(Color.BLACK);
setPreferredSize(new Dimension(600,600));
addMouseListener(this);
JButton C1B = new JButton("Clear");
C1B.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
izbor = 1;
repaint();}});
JPanel buttonPanel = new JPanel ();
buttonPanel.add(C1B);
this.add(buttonPanel,BorderLayout.NORTH);
JButton Save = new JButton("SAVE");
Save.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
izbor = 2;
repaint();}});
JPanel buttonPanel2 = new JPanel();
buttonPanel2.add(Save);
this.add(buttonPanel2,BorderLayout.EAST);
JButton Distance = new JButton("DISTANCE");
Distance.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
izbor = 3;
repaint();}});
JPanel buttonPanel3 = new JPanel();
buttonPanel3.add(Distance);
this.add(buttonPanel3,BorderLayout.WEST);
}
public void mouseEntered(MouseEvent enevt) {}
public void mouseExited(MouseEvent event) {}
public void mouseReleased(MouseEvent event) {}
public void mouseClicked(MouseEvent event) {}
public void mousePressed(MouseEvent event) {
Graphics g = getGraphics();
g.fillOval(event.getX()-r, event.getY()-r, 2*r, 2*r);
}
// public void paintComponent(Graphics g){
// super.paintComponent(g);
// }
//Main method
public static void main(String[] args){
JFrame windo = new Domasna();
windo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
windo.setSize(600,600);
windo.setBackground(Color.WHITE);
windo.setTitle("Domasna");
windo.setVisible(true);
//try{
// FileWriter fws = new FileWriter("Line.txt");
}
}
- 01-03-2011, 03:51 PM #2
What's your question? Please see the link in my signature before posting again.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 01-03-2011, 10:49 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
Kavin thanks for the fast response, btw i am asking the code to be finished :)
anyway thank u for the link i see it as useful.
- 01-03-2011, 11:18 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
Kevin, here are my exact questions:
1. how can i make line while clicking the mouse, and how can i stop this procedure?
2. how can I save the coordinates of the points which I obtain while clicking the mouse?
Regards,
Z
- 01-04-2011, 01:23 PM #5
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
-
- 01-04-2011, 11:17 PM #7
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
ok , i will :) thank u guys
- 01-22-2011, 08:12 AM #8
Member
- Join Date
- Jan 2011
- Posts
- 11
- Rep Power
- 0
cenevz.. i am new here and i've got a little suggestion on your code.. since almost all component in java can be associated with a mouselistener, i think it would be much better if you will not use the reserve word "implements" on the class which you are making so you wont be obliged to add those abstract methods needed.
you can just use this code:
<component>.addMouseListener( new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
/* for right click */
if (SwingUtilities.isRightMouseButton(e))
{
/* i just added this statement because i have read the you are asking on how to */
/* get the point where you are clicking.. :D */
Point p = e.getPoint();
}
/* for left click */
if (SwingUtilities.isRightMouseButton(e))
{
/* i just added this statement because i have read the you are asking on how to */
/* get the point where you are clicking.. :D */
Point p = e.getPoint();
}
}
}


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks