Early Stages, please help. Thanks
I'm currently a java student (Early Stages) and having problems with a small paint program. I dont expect a direct answer as it is part of assignment, but i am having trouble, the line does not draw now..
I have tryed the paint(Graphics g) method that does work when i draw a line, but its not what i want. i am now creating a Graphics component directly in the mouseDragged() method.
Also if you could tell me why the mouse is off when i draw the line.
import javax.swing.*;*
*import java.awt.*;
import java.awt.event.*;*
*import java.awt.Window.*;
class RevisedColourGUI extends JFrame {
int oldX;
int oldY;
int newX;
int newY;
Color colour = new Color(0,0,0);
JButton red = new JButton("Red");
JButton green = new JButton("Green");
JButton blue = new JButton("Blue");
JButton black = new JButton("Black");
JButton erase = new JButton("Erase");
JScrollBar redBar = new JScrollBar(JScrollBar.HORIZONTAL, 0,1,0,255);
JScrollBar greenBar = new JScrollBar(JScrollBar.HORIZONTAL, 0,1,0,255);
JScrollBar blueBar = new JScrollBar(JScrollBar.HORIZONTAL, 0,1,0,255);
JLabel redLabel = new JLabel("Red");
JLabel greenLabel = new JLabel("Green");
JLabel blueLabel = new JLabel("Blue");
JPanel scrollPane = new JPanel();
JPanel colourPane = new JPanel();
JPanel drawPane = new JPanel();
JPanel buttonPane = new JPanel();
JPanel labelPane = new JPanel();
JPanel southPane = new JPanel();
ButtonListener btn = new ButtonListener();
ScrollListener scrl = new ScrollListener();
PointerListener pointer = new PointerListener();
MotionListener motion = new MotionListener();
RevisedColourGUI() {
super("TestGUI");
setupGUI();
public void setupGUI() {
Container c = getContentPane();
drawPane.addMouseListener(pointer);
drawPane.addMouseMotionListener(motion);
c.setLayout(new BorderLayout());
red.addActionListener(btn);
green.addActionListener(btn);
blue.addActionListener(btn);
black.addActionListener(btn);
erase.addActionListener(btn);
buttonPane.setLayout(new GridLayout(6,1));
buttonPane.add(red);
buttonPane.add(green);
buttonPane.add(blue);
buttonPane.add(black);
buttonPane.add(erase);
buttonPane.add(colourPane);
//drawPane.setDoubleBuffered(true);
colourPane.setDoubleBuffered(true);
colourPane.setBackground(Color.BLACK);
scrollPane.setLayout(new GridLayout(3,1));
redBar.addAdjustmentListener(scrl);
greenBar.addAdjustmentListener(scrl);
blueBar.addAdjustmentListener(scrl);
redBar.setBackground(Color.RED);
greenBar.setBackground(Color.GREEN);
blueBar.setBackground(Color.BLUE);
scrollPane.add(redBar);
scrollPane.add(greenBar);
scrollPane.add(blueBar);
labelPane.setLayout(new GridLayout(3,1));
labelPane.add(redLabel);
labelPane.add(greenLabel);
labelPane.add(blueLabel);
southPane.setLayout(new BorderLayout());
southPane.add("Center", scrollPane);
southPane.add("East", labelPane);
c.add("East", buttonPane);
c.add("Center", drawPane);
c.add("South", southPane);
drawPane.setBackground(Color.WHITE);
setSize(400,450);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/*public void paint(Grapgics g) {
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == red) {
redBar.setValue(255);
greenBar.setValue(0);
blueBar.setValue(0);
else if(e.getSource() == green) {
redBar.setValue(0);
greenBar.setValue(255);
blueBar.setValue(0);
else if(e.getSource() == blue) {
redBar.setValue(0);
greenBar.setValue(0);
blueBar.setValue(255);
else if(e.getSource() == black) {
redBar.setValue(0);
greenBar.setValue(0);
blueBar.setValue(0);
else if(e.getSource() == erase) {
redBar.setValue(255);
greenBar.setValue(255);
blueBar.setValue(255);
repaint();
class ScrollListener implements AdjustmentListener {
int r = 0;
int g = 0;
int b = 0;
public void adjustmentValueChanged(AdjustmentEvent e) {
r = redBar.getValue();
g = greenBar.getValue();
b = blueBar.getValue();
colour = new Color(r,g,b);
colourPane.setBackground(colour);
class PointerListener implements MouseListener{
public void mouseClicked(MouseEvent e) {
public void mouseEntered(MouseEvent e) {
public void mouseExited(MouseEvent e) {
public void mousePressed(MouseEvent e) {
oldX = e.getX();
oldY = e.getY();
public void mouseReleased(MouseEvent e) {
class MotionListener implements MouseMotionListener {
public void mouseDragged(MouseEvent e) {
newX = e.getX();
newY = e.getY();
Graphics g = drawPane.getGraphics();
g.setColor(colour);
g.drawLine(oldX,oldY,newX,newY);
oldX = newX;
oldY = newY;
repaint();
public void mouseMoved(MouseEvent e) {
public static void main(String[] args) {
new RevisedColourGUI();
}Do i need a super.paint(g) somwhere?
Many thanks
Will
There are three problems here. First off, you're reassigning oldX and oldY in the mouseDragged() method. This method is called every time the mouse is moved while having a button down - not the behaviour we want (I assume we just want to draw a straight line between (oldX, oldY) and (newX, newY). This can be fixed by simply deleting the appropriate lines (i.e. "oldX = newX;" and "oldY = newY;") in the mouseDragged method.
Once you do this and run it, you'll find you have a second problem - the line disappears as soon as you stop moving the mouse. Again in your mouseDragged method, you're calling repaint(), which is a method in the JFrame class and actually repaints the entire frame from scratch - again, not what we want. Instead, all you need to do is erase the line by replacing repaint with a call to fillRect() that paints over the panel in the background colour, before painting the new line. By now, your mouseDragged() method should look something like:
public void mouseDragged(MouseEvent e) {
newX = e.getX();
newY = e.getY();
Graphics g = drawPane.getGraphics();
// get rid of the previous line...
g.setColor(Color.white);
g.fillRect(0, 0, drawPane.getWidth(), drawPane.getHeight());
// ...and draw the new one
g.setColor(colour);
g.drawLine(oldX,oldY,newX,newY);
}Now the third problem is that your line disappears when you press one of the colour buttons. This is because in the actionPerformed() method of your ButtonListener class, you are again repainting the entire frame. By replacing the call to repaint() with the following:
Graphics g = drawPane.getGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, drawPane.getWidth(), drawPane.getHeight());
g.setColor(colour);
g.drawLine(oldX,oldY,newX,newY);You'll have fixed the problem. Better still, put these lines of code into a private method and call it from the actionPerformed() and mouseDragged() methods.
Similar Messages
-
If i want to learn unix-HP for SAP at first stage, please help me.
Dear Friend,
If i want to learn unix-HP for SAP at first stage, please help me.
Thanks,
Regards,
sachinHi Sachin,
please go thru the following url
http://www.uga.edu/~ucns/wsg/unix/gstart/
and
http://whitepapers.techrepublic.com.com/whitepaper.aspx?&docid=10197&promo=100511
happy learning!
with BR,
Raj
<i> award points, plz </i> -
I erased an iphone on find my iphone , so i wanted to know what happened and I erased my without a sim card and now I says I cant go on it until i put in a sim card. Did i ruin the phone please help thanks.
You did not ruin the phone, you will need a sim card to activate the phone and then connect the phone to itunes and do a restore from back up.
http://support.apple.com/kb/HT1414 -
my ipod generation 5 will not come out of recovery mode. i was simply updating my software and this happened. it will not let me restore it comes up with and error. please help, thanks.
Hey erinoneill24,
Thanks for using Apple Support Communities.
Sounds like you can't update your device. You don't mention an error that it gives you. Take a look at both of these articles to troubleshoot.
iPod displays "Use iTunes to restore" message
http://support.apple.com/kb/ts1441?viewlocale=it_it
If you can't update or restore your iOS device
http://support.apple.com/kb/HT1808?viewlocale=de_DE_1
If you started your device in recovery mode by mistake, restart it to exit recovery mode. Or you can just wait—after 15 minutes the device will exit recovery mode by itself.
Have a nice day,
Mario -
AirPlay not showing on iPad anywhere. I've tried all of the rebooting functions and checked the control centre to no avail. The wifi networks are the same. Please help, thanks.
To use AirPlay, you need the following:
1. iPhone 4s (or later), iPad 2 (or later), iPad mini, or iPod touch (5th generation)
2. Apple TV (2nd or 3rd generation)
3. AirPort Express
4. Wi-Fi (802.11a/g/n) network
http://support.apple.com/kb/ht4437 -
MacBook Pro (15-inch, Late 2011) It turns on for less than 60 sec thens shutsoff by itself, it won't load to recovery hd partition, won't load to mac OS and already replaced with new MLB, but the problem still persist.. able to run ASD EFI but ASD OS wont work.please help thanks in advance
Holy crap, I got it to work.
Essentially, I just waited until DiskWarrior reached Step 9 and told it to skip the step before it had a chance to freeze. It skipped successfully, moved on, rebuilt and replaced the faulty directory files.
So far the computer is booting like normal, I can log in, and I'm currently copying all of the data to an external as I type this.
I still have no idea why the computer wouldn't mount or boot to any drive, but I guess I'll deal with that later. -
Dear Sirs,
I had just upgraded my OS to the OS X Mavericks. After doing so, I noticed the following changes:
1) My MS Outlook for Mac 2011 does not sync with my Mac book's calendar/contacts/notes.
2) My iPhone4s does not sync with my Mac book's calendar/contacts/notes anymore.
Please help, thank you.
aawwiiCorrect, USB local sync was taken away from iphones and ipads with 10.9.1 and 2. We are not allowed to speculate on rumours it might come back in 10.9.3 plus new iTunes. Either wait and see or explore Syncmate 5 beta free trial.
icloud still works.
Cant help with the Outlook problem. -
My ipad 2 cant open. Its appear a connector and a logo of itunes. Please help thanks!
You are in Recovery Mode.
Follow step 1 to 5 closely to recover your iPad.
http://support.apple.com/kb/HT1808
Note: You may have to repeat the above a few times -
How do I connect ny samsung tab2 note to hp deskjet 2540 if you could please help, thank you
how do I connect ny samsung tab2 note to hp deskjet 2540 if you could please help, thank you
Hi,
Your printer is not an ePrint printer as shown:
http://www8.hp.com/au/en/products/printers/product-detail.html?oid=5296007#!tab=specs
Please try the SECOND part of the following setup (for classic printers):
http://h10025.www1.hp.com/ewfrf/wc/document?docname=c02817255&cc=us&dlc=en&lc=en#N65
Regards.
BH
**Click the KUDOS thumb up on the left to say 'Thanks'**
Make it easier for other people to find solutions by marking a Reply 'Accept as Solution' if it solves your problem. -
HT5312 I do not remember the security answer apple ID.Please help.Thanks you!
I do not remember the security answer apple ID.Please help.Thanks you!
If you have a rescue email address (which is not the same thing as an alternate email address) set up on your account then steps 1 to 5 on the page that you posted from should let you reset them : go to https://appleid.apple.com/ and click 'Manage your Apple ID' on the right-hand side of that page and log into your account, then click on 'Password and Security' on the left-hand side of that page and on the right-hand side you should see an option to send security question reset info to your rescue email address.
If you don't have a rescue email address (you won't be able to add one until you can answer 2 of your questions) then you will need to contact iTunes Support / Apple to get the questions reset.
Contacting Apple about account security : http://support.apple.com/kb/HT5699
When they've been reset (and if you don't already have a rescue email address) you can then use the steps half-way down the page that you posted from to add a rescue email address for potential future use -
hi, my new device was set up in the store that I bought it from. How can I transfer all old text messages over to my new device? I have followed the icloud back up steps on this page. Please help, thank you.
Its an iPhone 4
Message was edited by: zzziggy85Here is how
http://support.apple.com/kb/HT2109 -
TS1696 uninstalled quick play still got no sound on films someone please help thanks
no sound for films uninstalled quick player someone please help thanks.
I'm not familiar with XP Home, you should be able to right click on the folder, and set the share permissions. You may have to turn off Simple Sharing to get access to this. Also you can set permissions on the folder as well. The share permissions I believe are Read, Write and Full Control. The NTFS should be able to set Read, Write, Modify, etc. Remember in Windows it will take the least privileged, meaning if you set the Share permissions to Read, and the NTFS to Write, you will only be able to read.
-
TS2446 Hi,My Apple ID has been disabled Please help Thank you
Hi,My Apple ID has been disabled Please help Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you
If changing your password does not solve then contact itunes support
-
I forgot my security question you can not buy please help thank you
I forgot my security question you can not buy please help thank you
The Three Best Alternatives for Security Questions and Rescue Mail
1. Send Apple an email request at: Apple - Support - iTunes Store - Contact Us.
2. Call Apple Support in your country: Customer Service: Contact Apple support.
3. Rescue email address and how to reset Apple ID security questions.
A substitute for using the security questions is to use 2-step verification:
Two-step verification FAQ Get answers to frequently asked questions about two-step verification for Apple ID. -
Please can anyone help me how to upgrade or update my mac notebook my versionis 10.5.8 I have tried several updates and or upgrade and have not been able to . Is there any new OS X availabel for my version. Please Help Thank you
Click here, install the DVD, run Software Update, open the Mac App Store, and try purchasing Mountain Lion. If you get told it's incompatible, phone the online Apple Store and order a download code for Lion.
(89308)
Maybe you are looking for
-
** SOAP to SOAP call - HTTP 404 not found - invalid content type
Hi friends, We have one scenario SOAP to SOAP call. It is synchronous interface. We are calling the WSDL. For some of the messages (not all), we are getting the error in SXMB_MONI com.sap.engine.interfaces.messaging.api.exception.MessagingException:
-
Router problem...
hey i recently bought a linksys router which works...but the internet comes and goes. But im still connected to online games and messeging applications. But i cant go on any websites. i usally plug and unplug the router and i can go on the web again
-
Tiger Classic-ified the OS9 on the other partition.
Hi, I recently bought this G4 QS and a 5-license DVD of Tiger (black disk with a silver 'X'), both second-hand. Being a newbie to OS X I also bought about 9 bookshelf inches of Tiger books. I fitted the G4 with a new 140 Gb hard drive and partitioned
-
I think people have asked about this topic, but I didn't see anything specifically relevant to my problem. I have had the desktop icons disappear, as other people have, but I can't open Finder to work out the problem either. Nor can I open any progra
-
Hi I'm using udev to automount usb devices, with the code from the wiki. Everything worked so far, but today it suddenly stopped. I have 2 usbsticks, both were working. Now I wanted to make a bootable usb stick with new firmware for my intel hard dri