package whiteBoard;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class whiteB extends JFrame implements MouseMotionListener{
public whiteB() {
initUI();
}
private javax.swing.JButton circleButton;
private javax.swing.JButton enterButton;
private javax.swing.JButton eraseButton;
private javax.swing.JButton rectangleButton;
private javax.swing.JLabel tempLable;
private javax.swing.JTextField textField;
private javax.swing.JTextArea textArea;
private javax.swing.JTextArea extraTextArea;
public int x;
public int y;
int recx,recy,recwidth,recheight;
boolean recActive;
public final void initUI() {
int recx=0,recy=0,recwidth=160,recheight=120;
addMouseMotionListener(this);
JPanel panel = new JPanel();
getContentPane().add(panel);
rectangleButton = new JButton();
circleButton = new JButton();
eraseButton = new JButton();
textField = new JTextField(20);
enterButton = new JButton();
textArea = new JTextArea();
extraTextArea = new JTextArea();
textArea.setEditable(false);
panel.add(textArea);
textArea.setSize(recwidth,recheight);
panel.add(extraTextArea);
extraTextArea.setBounds(0,130, 150,40);
panel.setLayout(null);
rectangleButton = new JButton("Rectangle");
rectangleButton.setBounds(160, 5, 100, 40);
rectangleButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
rectangleButtonActionPerformed(event); }
});
circleButton = new JButton("Circle");
circleButton.setBounds(160, 40, 100, 40);
circleButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
circleButtonActionPerformed(event);
}
});
eraseButton = new JButton("Erase");
eraseButton.setBounds(160, 75, 100, 40);
eraseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
eraseButtonActionPerformed(event);
}
});
textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
textAreaActionPerformed(event);
}
});
enterButton = new JButton("ENTER");
enterButton.setBounds(160, 130, 100, 40);
enterButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
enterButtonActionPerformed(event);
}
});
panel.add(rectangleButton);
panel.add(circleButton);
panel.add(eraseButton);
panel.add(enterButton);
setTitle("White Board");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
/*public void paint(Graphics g){
g.fillRect(recx,recy,recwidth,recheight);
g.drawString("("+x+","+y+")",x,y);
}*/
public void mouseMoved(MouseEvent event){
x = event.getX();
y = event.getY();
if (x >= recx&& x < recx+recwidth && y >= recy && y < recy+recheight)
recActive = true;
else
recActive = false;
repaint();
}
public void mouseExited(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseDragged(MouseEvent e)
{
}
private void rectangleButtonActionPerformed(ActionEvent event) {
textArea.setText(" Rectangle");
}
private void circleButtonActionPerformed(ActionEvent event) {
textArea.setText(" Circle");
}
private void eraseButtonActionPerformed(ActionEvent event) {
textArea.setText(" Erase");
}
private void textAreaActionPerformed(ActionEvent event) {
}
private void enterButtonActionPerformed(ActionEvent event) {
textArea.setText(extraTextArea.getText());
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
whiteB ex = new whiteB();
ex.setVisible(true);
}
});
}
}
35 replies to this topic
#1
Posted 15 November 2011 - 09:46 PM
I need help to fix my current Whiteboard program. You can compile it and run it, but does not currently do everything I want it to. My whiteboard currently has 3 function, Circle, Rectangle, and Erase. When Circle, Rectangle, or Erase is click it will print the word "Circle", "Rectangle" or "Erase on a 'Text area'. This currently works. I also need to implement mouseListener, when it enter,exit, click, dragged and display the coordinates or it. This is the part I need help with. I already handed this assignment in but I need it to work 100% before moving on because my next assignment that is already given makes me to actually draw the Circle if circle is clicked on and so on. There is a part of the code that is commented out that should implement the mouse course part which is not correct.
|
|
|
#2
Posted 15 November 2011 - 11:49 PM
public void paint(Graphics g){
super.paint(g);
g.fillRect(recx,recy,recwidth,recheight);
g.drawString("("+x+","+y+")",x,y);
}
Calling super.paint(g) there should get you further already. But then you must start thinking on whether it's correct to register the enter / move / drag of the mouse on the JFrame, or on for example the textArea. If you need the JFrame you'll need to add a mouselistener to the JFrame but also to every component added to it.
Test the code with only the added super.paint(g) and you should see why.
Also, you can make 1 action ActionListener for your 3 buttons (maybe not later on when the 3 buttons actually do something different):
ActionListener buttonListener = new ActionListener() {
public void actionPerformed(ActionEvent event) {
textArea.setText(" " + event.getActionCommand());
}
};
rectangleButton = new JButton("Rectangle");
rectangleButton.setBounds(160, 5, 100, 40);
rectangleButton.addActionListener(buttonListener);
circleButton = new JButton("Circle");
circleButton.setBounds(160, 40, 100, 40);
circleButton.addActionListener(buttonListener);
eraseButton = new JButton("Erase");
eraseButton.setBounds(160, 75, 100, 40);
eraseButton.addActionListener(buttonListener);
The "getActionCommand" of a JButton is the text of it by default.
#3
Posted 16 November 2011 - 09:42 AM
I added the super.paint(g) and notice the changes. Also added:
so now it displayed the coordinates, problem I'm having is that it displayed the coordinates everywhere on the Whiteboard instead of just the textArea.
what am I doing wrong here?
textArea.addMouseMotionListener(this); addMouseMotionListener(this);
so now it displayed the coordinates, problem I'm having is that it displayed the coordinates everywhere on the Whiteboard instead of just the textArea.
public void mouseMoved(MouseEvent event){
x = event.getX();
y = event.getY();
System.out.println(x+","+y);
if (x > recx&& x < recx+recwidth && y > recy && y < recy+recheight){
recActive = false;
}
else{
recActive = true;
repaint();
}
what am I doing wrong here?
#4
Posted 17 November 2011 - 12:18 AM
Don't do
You may consider doing
addMouseMotionListener(this);If you only want to have the coordinates tracked / showing on the textarea.
You may consider doing
x = event.getXOnScreen() - super.getLocationOnScreen().x; y = event.getYOnScreen() - super.getLocationOnScreen().y;To have it show the coordinates right at the mouse pointer.
#5
Posted 17 November 2011 - 01:27 AM
I can seem to get the scrollPane to work. I currently have the coordinates displaying in the textArea but it only displays coordinates of the bottom half of the textArea(i don't know why? what should I change or do here for this?). So I currently need scrollPane so when I enter the text area I can scroll and track the list of commands. example:
mouse entered at: (100,105)
mouse entered at: (97,107)
mouse entered at: (95,109)
mouse entered at: (93,109)
mouse entered at: (95,110)
mouse entered at: (99,110)
Once is fill the screen I want to be able to scroll and still the rest of the coordinates because once it fills up it stop showing the coordinates.
heres my code:
mouse entered at: (100,105)
mouse entered at: (97,107)
mouse entered at: (95,109)
mouse entered at: (93,109)
mouse entered at: (95,110)
mouse entered at: (99,110)
Once is fill the screen I want to be able to scroll and still the rest of the coordinates because once it fills up it stop showing the coordinates.
heres my code:
textArea = new JTextArea(); JScrollPane scrollPane = new JScrollPane(textArea); textArea.addMouseMotionListener(this); panel.add(scrollPane);
#6
Posted 17 November 2011 - 01:46 AM
Unfortunately it should work like that.
Make sure you did not set a size to your textArea, you can set the size of the scrollpane, not the textarea.
If that doesn't help I'm afraid it may have to do with you using no layout managers, and just coordinates and sizes to get everything to show...
Make sure you did not set a size to your textArea, you can set the size of the scrollpane, not the textarea.
If that doesn't help I'm afraid it may have to do with you using no layout managers, and just coordinates and sizes to get everything to show...
#7
Posted 17 November 2011 - 04:14 AM
I'm not sure if this will help but you could probably extract from it. Below is part of a chat program i am building in java. This is the scrollable JtextArea:
I got the scroll to work using a thread at stack overflow. Hope this helps.
chatText = new JTextArea(15, 30);
chatText.setLineWrap(true);
chatText.setEditable(false);
chatText.setForeground(Color.blue);
Font userfont = new Font(Font.SANS_SERIF, Font.PLAIN, 14);
chatText.setFont(userfont);
final JScrollPane chatTextScrollPane = new JScrollPane(chatText,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
chatTextScrollPane.getVerticalScrollBar().addAdjustmentListener( new AdjustmentListener()
{
BoundedRangeModel brm = chatTextScrollPane.getVerticalScrollBar().getModel();
boolean wasAtBottom = true;
@Override
public void adjustmentValueChanged(AdjustmentEvent e)
{
if (!brm.getValueIsAdjusting()) {
if (wasAtBottom)
brm.setValue(brm.getMaximum());
} else
wasAtBottom = ((brm.getValue() + brm.getExtent()) == brm.getMaximum());
}
});
panel.add(chatTextScrollPane, BorderLayout.SOUTH);
I got the scroll to work using a thread at stack overflow. Hope this helps.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:
#8
Posted 17 November 2011 - 04:18 AM
What's the listener for? Automaticallly scrolling to the bottom when content is added?
Edit: I actually refactored your whole original code to work with layoutmanager and not set positions using coordinates etc,
and those 4 lines are all it takes for me.
Edit: I actually refactored your whole original code to work with layoutmanager and not set positions using coordinates etc,
and those 4 lines are all it takes for me.
#9
Posted 17 November 2011 - 04:24 AM
Yes. Without it, the scroll bar just stayed at the top and shrunk as text was added. neshkid123 mess around with the auto scroll and see if that works for you.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:
#10
Posted 17 November 2011 - 04:28 AM
I thought his problem was getting the scrollpane to even show, not scrolling down.
(His textArea appears, added with scrollpane, but upon adding text "out of range" no scrollbar is added to sroll, and text is added out of sight...That's how I interpreted it.)
Maybe a screenshot of the problem could enlighten us ^^
(His textArea appears, added with scrollpane, but upon adding text "out of range" no scrollbar is added to sroll, and text is added out of sight...That's how I interpreted it.)
Maybe a screenshot of the problem could enlighten us ^^
#11
Posted 17 November 2011 - 04:32 AM
Oh. I taught get it to work meant, to scroll. running the code now.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:
#12
Posted 17 November 2011 - 04:58 AM
Okay, I just went back to your way of building the layout, and I had to add
scrollPane.setBounds(0,0,160,120); OR scrollPane.setSize(160,120);to get it to show.
Edited by wim DC, 17 November 2011 - 06:16 AM.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









