简体   繁体   中英

How can I change the color of my Graphics in one class by preforming an action from another class?

I'm new to java and desperate, so here we go!!! I am trying to change the color of my GUI contents when I click a certain option in my menu, but I am unsure how to. Here is the menu with its ActionPreform methods

    public JMenuBar makeMenuBar(DrawHere drawHTree) {

    JMenuBar menuBar = new JMenuBar();

    JMenu menSize = new JMenu("Color");
    menuBar.add(menSize);

    JMenuItem mitSmall = new JMenuItem("Black");
    mitSmall.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {

            //Do stuff here...
        }

    });
    menSize.add(mitSmall);

    JMenuItem mitMedium = new JMenuItem("Red");
    mitMedium.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {

            // Do stuff here...
        }

    });
    menSize.add(mitMedium);

    JMenuItem mitLarge = new JMenuItem("Cyan");
    mitLarge.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {

            // Do stuff here...
        }

    });
    menSize.add(mitLarge);

    return menuBar;
}

And here is a snippet of the class whose graphics color I wish to change

    private void drawHTree(Point location) {

    int x = (int)location.getX();

    int y = (int)location.getY();

    int boxSize = (int)(this.getHeight()*HTREE_SIZE);

    // Rough draft test: 13:50.55 for n = 13
    // Optimized test: 11:42.42 for n = 13
    int n = 5;
    _drawHTree(n,x,y,boxSize);
}

private void _drawHTree(int n, int x, int y, int boxSize) {
    if (n == 0) {
        return;
    }

    Graphics brush = this.getGraphics();

    //brush.setColor(Color.getHSBColor(1,1,1));

    brush.setColor(Color.RED);// <------- NEED HELP HERE! I WANT TO  
                                      // BE ABLE OT CHANGE THIS COLOR 

    ((Graphics2D) brush).draw(new Line2D.Double
            (x, y, x + boxSize, y));
    ((Graphics2D) brush).draw(new Line2D.Double
            (x + boxSize, y - boxSize/2, x + boxSize, y + boxSize/2));
    ((Graphics2D) brush).draw(new Line2D.Double
            (x , y - boxSize/2, x, y + boxSize/2));

    // compute x- and y-coordinates of the 4 half-size H-trees
    int x1 = x - boxSize/2;
    int x2 = x + boxSize/2;
    int y1 = y - boxSize/2;
    int y2 = y + boxSize/2;

    // recursively draw 4 half-size H-trees of order n-1
    _drawHTree(n-1, x1, y1, boxSize/2);
    _drawHTree(n-1, x1, y2, boxSize/2);   
    _drawHTree(n-1, x2 + boxSize/2, y1, boxSize/2);    
    _drawHTree(n-1, x2 + boxSize/2, y2, boxSize/2);    
}

So, anyone have any ideas? Thanks ;D

Why not jus decleare a Color variable and write a setter for that variable. Then instead of writing direct color in the

brush.setColor(Color.RED);

use

 brush.setColor(myColor);

Decleare your variable

Color myColor;

public void setMyColor(Color myColor){
     this.mayCOlor = myColor;

in the actionPerfomed just call this method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM