使用JApplet / Swing中的图形和小部件绘图? [英] Drawing with graphics and with widgets in JApplet/Swing?

查看:129
本文介绍了使用JApplet / Swing中的图形和小部件绘图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我有一个JApplet试图绘制图形(例如,g.drawOval(10,10,100,100),还包括JCompotents(即JButton))。重绘会变得非常古怪。



有时图形会绘制小部件或相反,它不可靠并会导致不可预知的行为。也一直在这些图形的顶部)



我玩过它尝试覆盖或手动绘制组件,更改顺序等,但认为我错过了这里有一些非常基本的东西,任何人都有模板或使用g.drawXXX和JCompotents的正确方法? 请遵循我所推荐的,


确保永远不要直接在JApplet中绘制,而是在JPanel或其contentPane中(它是一个JPanel )。确保绘制这个JPanel的paintComponent(...)方法。


它的工作原理:

  import java.awt。*; 
import java.awt.event。*;
import java.lang.reflect.InvocationTargetException;

import javax.swing。*;

public class Test2扩展JApplet {


public void init(){
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run(){
Test2BPanel panel = new Test2BPanel();
setContentPane(panel);
}
});
} catch(InvocationTargetException e){
e.printStackTrace();
} catch(InterruptedException e){
e.printStackTrace();






class Test2BPanel继承了JPanel {
private String [] backgroundImageFileNames = {test,test,test};

私人JButton refreshButton;
私人JComboBox backgroundList;

public Test2BPanel(){

setBackground(Color.white);

setLayout(new FlowLayout());

refreshButton = new JButton(replant new forest);
refreshButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){

}

});
add(refreshButton);

backgroundList =新的JComboBox(backgroundImageFileNames);
backgroundList.setSelectedIndex(2);
add(backgroundList);


@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
paintIt(g); (int i = 0; i <200; i ++){
for(int j = 0)(


public void paintIt ; j <200; j ++){
g.setColor(Color.red);
g.drawOval(10 * i,j,10,10);
}
}
}
}

另外,请检查Swing绘画教程,包括基本绘画教程高级绘画教程

有关这本书和其他更多的精彩书籍,请考虑购买Chet Haase和Romain的盖伊。你不会后悔购买!这是我拥有的最好的Java书籍之一。


Basically I have a JApplet that tries to draw with the graphics (ie, g.drawOval(10,10,100,100) and also include JCompotents (ie. JButton). What happens is that the repaint can get really wacky.

Sometimes the graphics will draw over widgets or vis-versa. It's not reliable and leads to unpredictable behavior.

(Button also always be on top of those graphics)

I've played around with it trying to override or manually draw components, changing orders, etc, but think I'm missing something very fundamental here. Anyone have a template or the correct way to use both g.drawXXX and JCompotents?

解决方案

Again, just follow what I recommended,

be sure never to draw directly in the JApplet but rather in a JPanel or in its contentPane (which is a JPanel). Make sure to draw in this JPanel's paintComponent(...) method.

and it works:

import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.InvocationTargetException;

import javax.swing.*;

public class Test2 extends JApplet {


   public void init() {
      try {
         SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
               Test2BPanel panel = new Test2BPanel();
               setContentPane(panel);
            }
         });
      } catch (InvocationTargetException e) {
         e.printStackTrace();
      } catch (InterruptedException e) {
         e.printStackTrace();
      }

   }


}

class Test2BPanel extends JPanel {
   private String[] backgroundImageFileNames = { "test", "test", "test" };

   private JButton refreshButton;
   private JComboBox backgroundList;

   public Test2BPanel() {

      setBackground(Color.white);

      setLayout(new FlowLayout());

      refreshButton = new JButton("replant new forest");
      refreshButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {

         }

      });
      add(refreshButton);

      backgroundList = new JComboBox(backgroundImageFileNames);
      backgroundList.setSelectedIndex(2);
      add(backgroundList);
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      paintIt(g);
   }

   public void paintIt(Graphics g) {
      for (int i = 0; i < 200; i++) {
         for (int j = 0; j < 200; j++) {
            g.setColor(Color.red);
            g.drawOval(10 * i, j, 10, 10);
         }
      }
   }
}

Also, please check the Swing painting tutorials including the Basic Painting Tutorial and the Advanced Painting Tutorial.

For a great book on this and more, please look into buying Filthy Rich Clients by Chet Haase and Romain Guy. You won't regret the purchase! It's one of the best Java books that I own.

这篇关于使用JApplet / Swing中的图形和小部件绘图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆