Graphics and Graphics 2D in java

451 Views Asked by At

I am having a problem changing the color of the diamond I had created when I am using methods in java. I don't know how to inherit or to correct it by using methods. I just want it to become object oriented I don't know how to properly do it. Please help me.

package Trial;

import javax.swing.*;
import java.awt.*;

public class ColorRed extends JApplet {
    public GradientPaint gradientPaint;
    public void paint(Graphics g){
        super.paint(g);
        Graphics2D g2 = (Graphics2D)g;
        GradientPaint black = new GradientPaint(50,20,Color.BLACK,50,50,Color.BLACK);
        blackDiamond(g2,black);
        GradientPaint yellowOrange = new GradientPaint(50,20,Color.YELLOW,50,50,Color.RED);
        redDiamond(g2,yellowOrange);

    }

    public void blackDiamond(Graphics2D g2,GradientPaint gradientPaint){
        int a [] = {100,50,100,150,100};
        int b [] = {10,60,110,60,10};
        setGradientPaint(gradientPaint);
        g2.setPaint(getGradientPaint());
        fillPolygon(a,b,5,g2);
    }
    public void redDiamond(Graphics2D g2,GradientPaint gradientPaint){
        int a2 [] = {100,60,100,140,100};
        int b2 [] = {20,60,100,60,20};
        setGradientPaint(gradientPaint);
        g2.setPaint(getGradientPaint());
        fillPolygon(a2,b2,5,g2);
    }

    public void fillPolygon(int a [], int b [] ,int c,Graphics2D g2){

        getGraphics().fillPolygon(a,b,c);
    }

    public GradientPaint getGradientPaint() {
        return gradientPaint;
    }

    public void setGradientPaint(GradientPaint gradientPaint) {
        this.gradientPaint = gradientPaint;
    }
}

This is the code that I try to create object-oriented but it doesn't inherit the color of gradient paint.

enter image description here

That is the result of my code

This the code that not yet create multiple methods

import javax.swing.*;
import java.awt.*;

public class ColorRed extends JApplet {
    public GradientPaint gradientPaint;
    public void paint(Graphics g){
        super.paint(g);
        Graphics2D g2 = (Graphics2D)g;
        int a [] = {100,50,100,150,100};
        int b [] = {10,60,110,60,10};
        g.fillPolygon(a,b,5);
        GradientPaint red = new GradientPaint(50,10,Color.RED,10,70,Color.ORANGE);
        g2.setPaint(red);
        int a2 [] = {100,60,100,140,100};
        int b2 [] = {20,60,100,60,20};
        g.fillPolygon(a2,b2,5);

    }
}

This is the result that I want: enter image description here

I just want to correct it to organize so that I wont declare all the local variables in paint method I just want to seperate it in different methods the problem is that the Color doesn't change. Please help me thanks a lot. :)

2

There are 2 best solutions below

0
Adder On

Maybe organize it like this, so that the color is easily changed:

package Trial;

import javax.swing.*;
import java.awt.*;

public class ColorRed extends JApplet {
    private GradientPaint black;    
    private GradientPaint yellowOrange; 

    public void init() {
        setBlack(new GradientPaint(50,20,Color.BLACK,50,50,Color.BLACK));
        setYellowOrange(GradientPaint(50,20,Color.YELLOW,50,50,Color.RED));
    }

    public setBlack(GradientPaint black) {
        this.black = black;
    }

    public setYellowOrange(GradientPaint yellowOrange) {
        this.yellowOrange = yellowOrange;
    }

    public void paint(Graphics g){
        super.paint(g);
        Graphics2D g2 = (Graphics2D)g;
        blackDiamond(g2,black);
        redDiamond(g2,yellowOrange);
    }

    public void blackDiamond(Graphics2D g2,GradientPaint gradientPaint){
        int a [] = {100,50,100,150,100};
        int b [] = {10,60,110,60,10};
        g2.setPaint(gradientPaint);
        fillPolygon(a,b,5,g2);
    }
    public void redDiamond(Graphics2D g2,GradientPaint gradientPaint){
        int a2 [] = {100,60,100,140,100};
        int b2 [] = {20,60,100,60,20};
        g2.setPaint(gradientPaint);
        fillPolygon(a2,b2,5,g2);
    }

    public void fillPolygon(int a [], int b [] ,int c,Graphics2D g2){

        g2.fillPolygon(a,b,c);
    }
}

Unfortunately I didn't locate an online swing runner to test it.

0
WJS On

I modified Adder's answer (which worked as advertised) so it would work without using JApplet (which is also tagged as deprecated). I added some comments where different.


    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.GradientPaint;
    import java.awt.Graphics;
    import java.awt.Graphics2D;

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;

    public class ColorRed extends JPanel {
       private GradientPaint black;
       private GradientPaint yellowOrange;

       JFrame                frame = new JFrame();

       public static void main(String[] args) {
          SwingUtilities.invokeLater(() -> new ColorRed().init());
       }

       public void init() {

          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

          // set the panel size
          setPreferredSize(new Dimension(500, 500));
          // add panel to frame.
          frame.add(this);
          // adjust frame and subcomponents
          frame.pack();
          // center on screen
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
          setBlack(new GradientPaint(50, 20, Color.BLACK, 50, 50, Color.BLACK));
          setYellowOrange(new GradientPaint(50, 20, Color.YELLOW, 50, 50, Color.RED));
       }

       public void setBlack(GradientPaint black) {
          this.black = black;
       }

       public void setYellowOrange(GradientPaint yellowOrange) {
          this.yellowOrange = yellowOrange;
       }
       // use paintComponent(g) and not  paint(g)
       public void paintComponent(Graphics g) {
          super.paintComponent(g);
          Graphics2D g2 = (Graphics2D) g;
          blackDiamond(g2, black);
          redDiamond(g2, yellowOrange);
       }

       public void blackDiamond(Graphics2D g2, GradientPaint gradientPaint) {
          int a[] = { 100, 50, 100, 150, 100
          };
          int b[] = { 10, 60, 110, 60, 10
          };
          g2.setPaint(gradientPaint);
          fillPolygon(a, b, 5, g2);
       }
       public void redDiamond(Graphics2D g2, GradientPaint gradientPaint) {
          int a2[] = { 100, 60, 100, 140, 100
          };
          int b2[] = { 20, 60, 100, 60, 20
          };
          g2.setPaint(gradientPaint);
          fillPolygon(a2, b2, 5, g2);
       }

       public void fillPolygon(int a[], int b[], int c, Graphics2D g2) {

          g2.fillPolygon(a, b, c);
       }
    }