【问题标题】:maven import java swing awt working with jdk17 and the version of the library (org.eclipse.swt:org.eclipse.swt.win32.win32.x86_64:4.3)maven import java swing awt 使用 jdk17 和库版本(org.eclipse.swt:org.eclipse.swt.win32.win32.x86_64:4.3)
【发布时间】:2021-11-24 08:11:21
【问题描述】:

我从 maven 导入了问题的标题 问题是我从画布(awt one)中获取图形的地步 并将其添加到 jpanel 这似乎没有被绘制

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;


public class CELSIUS {
    private JButton button1;
    private JPanel panel1;
    public java.awt.image.BufferedImage bf;
    public Canvas cnv;
    public JFrame jf;
    private boolean annadido=false;
    public CELSIUS() {
        panel1.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                super.mouseMoved(e);
                System.out.println("Hallo\n");
                
            }
        });


    }
    private void createUIComponents() {
        panel1 = new JPanel();
        button1 = new JButton("OK");
        bf=new BufferedImage(100,100,BufferedImage.TYPE_BYTE_BINARY);
        Graphics gc=bf.getGraphics();
       gc.setColor(Color.BLACK);

       gc.fillRect(0,0,100,100);
        cnv=new Canvas();
        cnv.setSize(100,100);
        gc.setColor(Color.white);
        gc.drawString("francisco",10,10);
        gc.dispose();




    }
        public static void main(String []args){

       JFrame jf=new JFrame();
       CELSIUS mycell=new CELSIUS();
       mycell.jf=jf;
       mycell.jf.setSize(500,500);
       jf.setContentPane(mycell.panel1);
       mycell.cnv.getGraphics().drawImage(mycell.bf,0,0,jf);



       jf.getContentPane().add(mycell.cnv);

       jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


       jf.setVisible(true);

    }


}

所以会发生什么 ??? 注意:如果我在鼠标移动处理程序中多次添加它,有时会按预期工作 在黑底白字上显示闪烁的弗朗西斯科

【问题讨论】:

  • 1) 不要使用 getGraphics() 进行自定义绘画。 2) 不要使用 Canvas 进行自定义绘画。 3) 而是覆盖JPanelpaintComponent(...) 方法。阅读 Custom Painting 上的 Swing 教程中的部分,了解更多信息和工作示例,以帮助您入门。请注意,如果您只是绘制图像,那么一种简单的方法是使用JLabelImageIcon。该教程还有一个关于How to Use Icons的部分。
  • 向您提供建议,但总之,我将添加新功能,通过 xor 操作与位图相结合,以便在丰富的色彩环境中获得图像的部分反转,但现在需要第一阶段的工作
  • org.eclipse.swt:org.eclipse.swt.win32.win32.x86_64:4.3swt - 与 Swing 或 AWT 无关
  • 谢谢我会删除它
  • 但现在需要第一阶段的工作 - 这就是为什么你得到了教程的链接。如果您不了解进行自定义绘画的基础知识,就无法编写复杂的绘画程序。

标签: java eclipse swing


【解决方案1】:

这是正确的方法:

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.plaf.PanelUI;

import static javax.swing.border.BevelBorder.*;

public class main12 {
    public static void main(String [] a){
     mijpanel a1=new mijpanel();
     JFrame jmain =new JFrame("Hello world");
     jmain.add(a1);
     jmain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     a1.setVisible(true);
     jmain.pack();
     jmain.setVisible(true);
        System.out.println("Created GUI on EDT? "+
                SwingUtilities.isEventDispatchThread());
        a1.add(new Label("This is"));
        a1.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                super.mouseMoved(e);
                jmain.setTitle("Moving "+e.getX()+" "+e.getY());
            }
        });
    return;
    }





}
class mijpanel extends JPanel{
public
BufferedImage inner;
    public mijpanel(){
        super();
        setBorder(BorderFactory.createBevelBorder(RAISED));
         inner=new BufferedImage(100,100,BufferedImage.TYPE_BYTE_BINARY);
    }
    @Override
 public Dimension getPreferredSize(){
      return new Dimension(500,500);



 }
@Override
    public void paint(Graphics g){
        super.paint(g);
    /*System.out.println("Created GUI on EDT? "+
            SwingUtilities.isEventDispatchThread());*/

        //System.out.println(getUI());
    if(SwingUtilities.isEventDispatchThread())
    {
        Graphics g3=inner.getGraphics();

        g3.setColor(Color.black);
        g3.fillRect(00,0,100,100);
        g3.setColor(Color.white);
        Font myfont1=g3.getFont();
       //System.out.println( myfont1.getFontName());
        g3.drawString("My text",0,20);

        g3.dispose();

    }
      g.setXORMode(Color.white);
      System.out.println(g.getColor());
      g.drawImage(inner,0,0,this);
      g.drawString("Custom painting",50,50);

     g.setColor(Color.RED);
     g.setXORMode(Color.white);
    g.drawString("To see ...and given color",150,150);
    g.setColor(Color.green);

     g.fillRect(50,80,200,200);


    //g.fillRect(50,80,200,200);
}
}

如您所见,我已删除所有不必要的库依赖项

【讨论】:

  • 当我在背景上以异或模式绘制时,似乎没有考虑颜色 rgb=51(背景灰色)你只做三个异或组合(异或颜色背景和我绘制的像素)当它在顶部绘制另一个东西时。为什么?
猜你喜欢
  • 1970-01-01
  • 2013-09-25
  • 1970-01-01
  • 2014-06-14
  • 2021-12-05
  • 1970-01-01
  • 1970-01-01
  • 2019-04-26
  • 1970-01-01
相关资源
最近更新 更多