Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Trying to Change the custom Color of a Rectangle made through DragMode and change type of Rectangle

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Trying to Change the custom Color of a Rectangle made through DragMode and change type of Rectangle

    Hello and good morning to all you kind sirs and madams

    What I'm trying to do is basically draw a Rectangle outline through left-click and then dragging the mouse to the size one wants.

    After one has created this rectangle, the user can change the color of the rectangle through clicking the 3 sliders found on the top in the program. Upon the change in color, the rectangle should change from "g.drawRect" to "g.fillRect" and use the desired color.


    I have the basic outline of the program done, but need help with the last part (the color and changing of the Rectangle type after changing the color)


    Thanks for the help in advance!
    P.S. If possible, please use the same imported things that I use and try to keep it as simple as possible (Still learning Java)
    P.P.S. Is there like a "then" command in Java? Like: after you've done x, do y. That would make this heaps easier.


    import java.awt.event.*;
    import java.awt.*;
    import java.applet.*;
     
    //Diese Applikation öffnet ein Fenster,
    //in welchem durch einen gehaltenen linken Mouse Klick
    //ein Reckteck gezeichnet wird
     
     
    public class WhatIsTheProblem006 extends Applet 
    implements AdjustmentListener {
      Scrollbar schieberrot, schiebergruen, schieberblau;
      int currentX, currentY, startX, startY, schieberwertrot, schieberwertgruen, schieberwertblau;
      boolean dragMode = false;
      //Boolean ist ein primitiver Datentypen wie "int", "float", etc., 
      //  die 1-Byte-Speicher - von denen nur ein Bit vom Compiler verwendet - zuweist. 
      //  Es kann nur den Wert Wahr (true) oder Flasch (false) zugeordnet werden.    
     
      public void init() {
        setBackground(Color.lightGray);
        setSize(2000, 2000);
        add(new Label("Rot - Anteil"));
        schieberrot = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 24);
        add(schieberrot);
        schieberrot.addAdjustmentListener(this);
     
        add(new Label("Grün - Anteil"));
        schiebergruen = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 24);
        add(schiebergruen);
        schiebergruen.addAdjustmentListener(this);
     
        add(new Label("Blau - Anteil"));
        schieberblau = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 24);
        add(schieberblau);
        schieberblau.addAdjustmentListener(this);
      }
     
      public void adjustmentValueChanged (AdjustmentEvent e) {
        schieberwertrot = schieberrot.getValue();
        schieberwertgruen = schiebergruen.getValue();
        schieberwertblau = schieberblau.getValue();
        if (schieberwertrot >= 24) {
          schieberwertrot = 24;
        }
        if (schieberwertgruen >= 24) {
          schieberwertgruen = 24;
        }
        if (schieberwertblau >= 24) {
          schieberwertblau = 24;
        }
        repaint();
      }
     
     
      public void paint(Graphics g) {
        int beginX, beginY, width, height;
        g.setColor(new Color(schieberwertrot*10+1, schieberwertgruen*10+1, schieberwertblau*10+1));
        if (dragMode == true) {
          beginX = Math.min(startX, currentX);
          beginY = Math.min(startY, currentY);
          width = Math.abs(currentX - startX);
          height = Math.abs(currentY - startY);
     
          g.drawRect(beginX, beginY, width, height);
        }
        else {
          g.setColor (Color.black);
          g.drawString("Draw a box by dragging the mouse", 10, 25);
        }
      }
      public boolean mouseDown(Event event, int x, int y) {
        dragMode = true;
        startX = x;
        startY = y;
        return true;
      }
      public boolean mouseUp(Event event, int x, int y) {
        dragMode = false;
        return true;
      }
      public boolean mouseDrag(Event event, int x, int y) {
        if (dragMode == true) {
          currentX = x;
          currentY = y;
          repaint();
        }
        return true;
      }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,853
    My Mood
    Mellow
    Thanks
    178
    Thanked 631 Times in 618 Posts

    Default Re: Trying to Change the custom Color of a Rectangle made through DragMode and change type of Rectangle

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    It almost sounds like you're asking someone to finish it for you, and that's probably why you haven't had much response. We don't do that here. If you can ask specific questions or describe what you're confused about, we can be more helpful.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to Change the custom Color of a Rectangle made through DragMode and change type of Rectangle

    Quote Originally Posted by GregBrannon View Post
    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    It almost sounds like you're asking someone to finish it for you, and that's probably why you haven't had much response. We don't do that here. If you can ask specific questions or describe what you're confused about, we can be more helpful.
    The problem is just that I don't know how to do this and haven't found any other way on the internet to solve this problem.

    I've already got the rest down so it isn't finishing, I've just pretty much hit a wall and am asking for directions.
    Any chance that somebody could say where I need to look or rather, at what?
    It'd be awesome

Similar Threads

  1. How do I change the color of my cursor?
    By Chokladmunken in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 17th, 2014, 09:54 AM
  2. Trying To Change The Color Of JScrollBar
    By Damian3395 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 4th, 2013, 06:40 PM
  3. How to change color of JMenuItem when mouse is over it
    By Bagzli in forum AWT / Java Swing
    Replies: 5
    Last Post: April 2nd, 2012, 02:09 PM
  4. How to shift a String in a rectangle which is type of Canvas
    By elenora in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: April 4th, 2011, 07:39 AM
  5. Change font color and size
    By javanovice in forum AWT / Java Swing
    Replies: 2
    Last Post: April 20th, 2010, 09:57 AM

Tags for this Thread