swing画矩形该怎么repaint()

kidbei 发布于 2012/10/12 17:01
阅读 1K+
收藏 0
package kidbei.learn.flv.jflvlib;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;


public class TansluFrame extends JFrame implements ActionListener{
private JButton exitButton;
int orgx,orgy,endx,endy;
Graphics g;
BufferedImage tempImage;
public static void main(String[] args){
      new TansluFrame();
}

public TansluFrame(){
        com.sun.awt.AWTUtilities.setWindowOpacity(this, 0.7f);  //设置窗体半透明 
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);//设置窗口最大化
        this.setUndecorated(true);
        this.setVisible(true);
        this.setLayout(null);
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        this.setAlwaysOnTop(true);
        
        exitButton = new JButton("退出");
        exitButton.setBackground(Color.blue);
        exitButton.setForeground(Color.red);
        exitButton.addActionListener(this);
        exitButton.setBounds(1, 1, 70, 30);
        this.add(exitButton);
        
        
        this.addMouseListener(new MouseAdapter(){
        @Override
        public void mousePressed(MouseEvent e) {
        orgx = e.getX();
        orgy = e.getY();
        }
        });
        this.addMouseMotionListener(new MouseAdapter(){
        @Override
        public void mouseDragged(MouseEvent e) {
        endx = e.getX();
        endy = e.getY();
        g = getGraphics();
//         g.drawImage(tempImage, 0, 0, TansluFrame.this);
        int x = Math.min(orgx, endx);
int y = Math.min(orgy, endy);
int width = Math.abs(endx - orgx)+1;
int height = Math.abs(endy - orgy)+1;
       
g.setColor(Color.BLUE);
g.drawRect(x-1, y-1, width+1, height+1);
        }
        });
    }

@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(exitButton)){
int a = JOptionPane.showConfirmDialog(this, "是否退出?", "提示", JOptionPane.YES_NO_OPTION);
if(a == 0){
System.exit(0);
}
}
}
}
这样出来的矩形会有很多次线条,该怎么repaint()?
加载中
0
xianwu13
xianwu13

在 一个 Jpanel 里面的 画矩形 ,具体的 画的工作交给 paint 方法,reapaint 调用 paint。我修改了一下你的代码

import java.awt.Color;

import java.awt.Graphics;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.awt.image.BufferedImage;

import java.awt.image.RescaleOp;

import javax.swing.JButton;
import javax.swing.JPanel;

import javax.swing.JFrame;

import javax.swing.JOptionPane;
public class TansluFrame extends JFrame implements ActionListener {
 private JButton exitButton;
 BufferedImage tempImage;
 private BackPanel backPanel = null;
 public static void main(String[] args) {
  new TansluFrame();
 }
 public TansluFrame() {
  com.sun.awt.AWTUtilities.setWindowOpacity(this, 0.7f); // 设置窗体半透明
  this.setExtendedState(JFrame.MAXIMIZED_BOTH);// 设置窗口最大化
  this.setUndecorated(true);
  this.setVisible(true);
  this.setLayout(null);
  this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  this.setAlwaysOnTop(true);
  this.backPanel = new BackPanel();
  exitButton = new JButton("退出");
  exitButton.setBackground(Color.blue);
  exitButton.setForeground(Color.red);
  exitButton.addActionListener(this);
  exitButton.setBounds(1, 1, 70, 30);
  this.backPanel.setSize(this.getWidth(), this.getHeight());
  this.backPanel.add(exitButton);
  this.add(backPanel);
 }

 public void actionPerformed(ActionEvent e) {

  if (e.getSource().equals(exitButton)) {
   int a = JOptionPane.showConfirmDialog(this, "是否退出?", "提示",
     JOptionPane.YES_NO_OPTION);
   if (a == 0) {
    System.exit(0);
   }
  }
 }
}

class BackPanel extends JPanel {
 static int orgx, orgy, endx, endy,x,y,width,height;
 Graphics g;
 public BackPanel(){
  this.addMouseListener(new MouseAdapter() {
   @Override
   public void mousePressed(MouseEvent e) {
    orgx = e.getX();
    orgy = e.getY();
   }
  });
  this.addMouseMotionListener(new MouseAdapter() {
   @Override
   public void mouseDragged(MouseEvent e) {
    endx = e.getX();
    endy = e.getY();
    x = Math.min(orgx, endx);
    y = Math.min(orgy, endy);
    width = Math.abs(endx - orgx) + 1;
    height = Math.abs(endy - orgy) + 1;
    repaint();
   }
  });
  
 }
 @Override
 public void paint(Graphics g) {
  super.paint(g);
  g.setColor(Color.BLUE);
  g.drawRect(x - 1, y - 1, width + 1, height + 1);
 }

}

 

0
xianwu13
xianwu13
swing 的组件是 自动实现了 上缓冲的,我之前看过你的雪花,风吹来的时候,雪花会抖动,也是双缓冲的问题。你在swing 里面实现的绘画根本没有利用swing 的原理。
返回顶部
顶部