java中的图形和图形2D

Posted

技术标签:

【中文标题】java中的图形和图形2D【英文标题】:Graphics and Graphics 2D in java 【发布时间】:2020-02-24 19:56:54 【问题描述】:

当我在 java 中使用方法时,我在更改创建的钻石颜色时遇到问题。我不知道如何通过使用方法来继承或纠正它。我只是希望它成为面向对象的我不知道如何正确地做到这一点。请帮帮我。

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;
    

这是我尝试创建面向对象的代码,但它不继承渐变绘制的颜色。

enter image description here

这是我的代码的结果

这是尚未创建多个方法的代码

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);

    

这是我想要的结果: enter image description here

我只是想更正它以组织起来,这样我就不会在paint方法中声明所有局部变量我只想用不同的方法将它分开,问题是颜色不会改变。请帮助我,非常感谢。 :)

【问题讨论】:

您的fillPolygon 方法没有使用它的g2 参数。 即使我使用它并将其放入 fillPolygon 也没有任何反应 Component.getGraphics() 永远不应该被调用。 (我希望它的文件表明这一点。) 不要使用小程序。它们已经过时了。 我也不想使用它,但我的主题 OOP 必须通过。 【参考方案1】:

或许可以这样组织,这样颜色很容易改变:

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);
    

很遗憾,我没有找到在线摇摆跑步者来测试它。

【讨论】:

【参考方案2】:

我修改了 Adder's 的答案(如宣传的那样工作),因此它可以在不使用 JApplet 的情况下工作(它也被标记为已弃用)。我添加了一些不同的 cmets。


    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);
       
    


【讨论】:

以上是关于java中的图形和图形2D的主要内容,如果未能解决你的问题,请参考以下文章

将 Java 2d 图形图像保存为 .png 文件

图形 2d Java 7 问题的开放式字体

Unity中的Mesh网格编程——绘制基本2D图形

Unity中的Mesh网格编程——绘制基本2D图形

《Java游戏编程原理与实践教程》读书笔记(第3章——Java图形处理和Java 2D)

JAVA也可以用于图像的设计吗?