[英]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)
I have imported from maven what title of the question says the problem is to the point where I get graphics from my canvas(awt one) and add it to jpanel this does not seem to be drawed我已经从 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);
}
}
so what happens???那么会发生什么??? note: if I add it multiple times in the mouse move handler sometimes works as intended showing a blinking Francisco on white over black background
注意:如果我在鼠标移动处理程序中多次添加它,有时会按预期工作,在黑色背景上以白色显示闪烁的 Francisco
Here is the right way:这是正确的方法:
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);
}
}
as you can see i have removed all unneccesary library dependences如您所见,我已经删除了所有不必要的库依赖项
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.