简体   繁体   English

java 中的图形和图形 2D

[英]Graphics and Graphics 2D in java

I am having a problem changing the color of the diamond I had created when I am using methods in java.当我使用 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.我只是希望它成为面向 object 的我不知道如何正确地做到这一点。 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.我只是想更正它以组织起来,这样我就不会在paint方法中声明所有局部变量我只想用不同的方法将它分开,问题是颜色不会改变。 Please help me thanks a lot.请帮助我,非常感谢。 :) :)

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.不幸的是,我没有找到在线 swing 跑步者来测试它。

I modified Adder's answer (which worked as advertised) so it would work without using JApplet (which is also tagged as deprecated).我修改了Adder 的答案(它像宣传的那样工作),所以它可以在不使用 JApplet 的情况下工作(它也被标记为已弃用)。 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);
       }
    }


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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