awt

Java AWT生成滑动验证码

谁都会走 提交于 2019-12-01 22:16:59
最近工作需要在登录时使用滑动验证码做登录校验,在生成验证码图片的时候碰到了不小的麻烦 : (。 网上能查到的做法基本上都是使用一张已存在的实际图片作为滑块的图形模板,然后按照此模板做二重循环逐像素地从源图像抠出滑块以及新图。 这种方式优点就是可以控制每个像素,如改变局部的 Alpha 值、做局部的高斯模糊等。缺点也显而易见——太麻烦,且二重循环对内存也是不小的负担。 笔者的想法是:首先生成滑块 1)根据规定好的路径生成闭合区间;2) 设置 Graphics2D 的渲染区间,限制仅渲染此闭合区间的内部 ( setClip 方法);3)使用原图来渲染当前 Image( drawImage 方法)。最后在源图扣掉滑块的地方加上阴影。 研究了一下 Java 的 awt 图像处理,这里对这几天的成果做一个记录,本文仅涉及图像处理方面,去掉了其它业务逻辑。 生成闭合区间 Java awt中提供了 Shape 接口,来代表任意一种几何图形。考虑我们要实现如下滑块图形: 上图左边的滑块可以先简化为右侧,原理是一样的。右边的图像一共有 A、B、C、D、E、F 六个顶点。其中 CD 之间是一段圆弧。Graphics2D的坐标系从左上角开始,X轴往下、Y轴往右是正方向。这里先算出各顶点坐标:A(0,0)、B(0,50)、C(15,50)、D(35,50)、E(50,50)、F(50,0)。 注意到H1

MouseEvent on JPanel - wrong coordinate

我只是一个虾纸丫 提交于 2019-12-01 21:45:07
问题 I have written the following micro-paintbrush program in Java: import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; class AuxClass1 extends JFrame

Serialize a java.awt.geom.Area

人盡茶涼 提交于 2019-12-01 21:26:32
I have the need to serialize an Area object (java.awt.geom.Area) in a socket. However it doesn't seem to be serializable. Is there a way to do such thing? Maybe by converting it into a different object? Thanks in advance kieste I found this workaround: AffineTransform.getTranslateInstance(0,0).createTransformedShape(myArea) This results in a shape that can be serialized. Use XStream to trivially convert it to/from XML. You don't need your object to implement particular interfaces, and the serialisation is customisable. From kieste's answer, this work-around can be derived. import java.awt

Java swing. How to wait for other Jframes

坚强是说给别人听的谎言 提交于 2019-12-01 21:24:02
In a JFrame, when I click on 'login', I pop up another Jframe which is the login window. How do I make my main Jframe wait for my login Jframe to exit, before doing anything else? Just use a modal dialog in stead of a frame, that way you cannot do anything else until it'is closed see http://mindprod.com/jgloss/modal.html for explanation and see http://www.java2s.com/Tutorial/Java/0240__Swing/ASimpleModalDialog.htm for code example If you insist on using a JFrame, you could use a workaround by cover the other frame by a glassframe.. Not too a nice solution, I admit.. I agree that a modal dialog

How to refresh the JComboBox data?

我是研究僧i 提交于 2019-12-01 21:14:02
I am having trouble refreshing the data inside the JComboBox . There is a button " Create " which has ActionListener , which adds the item to JComboBox . But the changes are not reflected in the GUI: I still don't see the new added item. repaint() doesn't help. Update: Here is a (almost) full GUI code: public class Main extends JFrame implements ActionListener { static Connection conn; static PreparedStatement ps = null; static ResultSet res; static Statement sta; private final static int ITERATION_NUMBER = 1000; public void GUI () throws SQLException { setBounds(0, 0, 320, 240);

Getting Height and Width of Image in Java without an ImageObserver

我是研究僧i 提交于 2019-12-01 19:03:40
I am trying to get the height and width of images (via a url) in Java without an ImageObserver. My current code is: public static void main(String[] args) throws IOException { // TODO Auto-generated method stub File xmlImages = new File("C:\\images.xml"); BufferedReader br = new BufferedReader(new FileReader(xmlImages)); File output = new File("C:\\images.csv"); BufferedWriter bw = new BufferedWriter(new FileWriter(output)); StringBuffer sb = new StringBuffer(); String line = null; String newline = System.getProperty("line.separator"); while((line = br.readLine()) != null){ if(line.contains(

MouseEvent on JPanel - wrong coordinate

不打扰是莪最后的温柔 提交于 2019-12-01 18:46:28
I have written the following micro-paintbrush program in Java: import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; class AuxClass1 extends JFrame implements MouseListener, MouseMotionListener{ private JPanel panel1 = new JPanel(); private JPanel panel2 =

Java: use Robot while running fullscreen app

假装没事ソ 提交于 2019-12-01 18:24:31
I have a problem with Robot class in Java: it sometimes doesn't work when i run a fullscreen game. Here's my code: Toolkit tlkt = Toolkit.getDefaultToolkit(); Robot bot = new Robot(); while(true) { Thread.sleep(3000); tlkt.beep(); //make sure that program still runs bot.mousePress(MouseEvent.BUTTON1_DOWN_MASK); bot.mouseRelease(MouseEvent.BUTTON1_DOWN_MASK); } I want to left-click every 3 seconds and beep for sure. It work fine but when I run some full-screen application, then it just beeps but not click. I already tried to move the Robot declaration into the loop, so new Robot is created

“Buffers have not been created” … whilst creating buffers

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 18:21:21
I have (what I thought was) a straightforward BufferStrategy for a JFrame. It is created like so: // Buffer container.createBufferStrategy(2); strategy = container.getBufferStrategy(); However, occassionally I receive the following error (which points to the first line of the preceeding snippet as the offending item) : java.lang.IllegalStateException: Buffers have not been created This error is peculiar as it comes and goes - sometimes it is triggered, sometimes not. I suspect this means it's a threading issue. Does anyone have any pointers as to what might be going on here? I'm a little at a

Java bitmap font: blitting 1-bit image with different colors

懵懂的女人 提交于 2019-12-01 18:12:44
I'd like to implement a simple bitmap font drawing in Java AWT-based application. Application draws on a Graphics object, where I'd like to implement a simple algorithm: 1) Load a file (probably using ImageIO.read(new File(fileName)) ), which is 1-bit PNG that looks something like that: I.e. it's 16*16 (or 16*many, if I'd like to support Unicode) matrix of 8*8 characters. Black corresponds to background color, white corresponds to foreground. 2) Draw strings character-by-character, blitting relevant parts of this bitmap to target Graphics . So far I've only succeeded with something like that: