| |

VerySource

 Forgot password?
 Register
Search
View: 904|Reply: 8

Draw a picture, how to generate an image file, such as bmp format

[Copy link]

1

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-3-4 17:30:02
| Show all posts |Read mode
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestDraw extends Frame {
public TestDraw (String title) {
super (title);
addWindowListener (new WindowAdapter () {
public void windowClosing (WindowEvent evt) {
System.exit (0);
}
});
setSize (1024,768);
setVisible (true);
}
public void paint (Graphics g) {
int width2 = 120;
g.fillArc (512, 384, 2 * width2, 2 * width2, 0, 360);
}
}
The source code is as above, how to write next? Make a picture can generate a picture file? Thank you
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 United States

Post time: 2020-5-19 00:00:01
| Show all posts
Here is a screenshot of the code, you can refer to it to output the picture, but here is generated jpg

public static void drawImg () {
try {
int w = 0, h = 0, o_w = 0, o_h = 0, l = 0, t = 0;
// w- width of target image, h- height of target image, o_w- width of image to be cropped, o_h- height of image to be cropped, l- distance of cropped portion to the left of image to be cropped, t- distance of cropped portion to be cropped Image top margin
BufferedImage image = new BufferedImage (w, h,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics ();
Image img = Toolkit.getDefaultToolkit (). GetImage ("c:\\test.jpg"); // Get the source picture
System.out.println ("img-width =" + img.getWidth (null));
System.out.println ("img-height =" + img.getHeight (null));
g.setColor (Color.white); // The background color of the picture is white
g.fillRect (0, 0, w, h); // Draw a white rectangle first
g.drawImage (img, 0, 0, o_w, o_h, null); // First set the picture to the current size
g.dispose ();
g.drawImage (img, 0, 0, w, h, l, t, (l + w), (h + t), Color.WHITE,
null); // Crop
g.dispose ();
FileOutputStream outfile = new FileOutputStream ("d:\\test1.jpg"); // Output picture
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder (outfile);
encoder.encode (image); // Output picture
outfile.close ();
} catch (Exception e) {
e.printStackTrace ();
}
}
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 United States

Post time: 2020-5-19 13:45:01
| Show all posts
Java does not seem to support bmp format, jpg can. There are many online. Thejinm1976method is feasible. Or you can find the realization method of random verification code produced by java on the Internet.
Reply

Use magic Report

1

Threads

51

Posts

32.00

Credits

Newbie

Rank: 1

Credits
32.00

 China

Post time: 2020-5-20 12:30:01
| Show all posts
The method of ls is indeed very good
Reply

Use magic Report

0

Threads

23

Posts

17.00

Credits

Newbie

Rank: 1

Credits
17.00

 China

Post time: 2020-5-21 07:45:01
| Show all posts
Use javax.imageio.ImageIO class. Very simple. It has a static method to directly save the image, which can be bmp, jpg, png, etc., but it seems that gif does not work.
Reply

Use magic Report

1

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-5-21 20:00:01
| Show all posts
I was drawing in public void paint (Graphics g) {}, where should those codes be added?
Reply

Use magic Report

0

Threads

11

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-5-22 17:15:01
| Show all posts
bmp is a bitmap
You want to code ...
Record the information of each point ...
Reply

Use magic Report

1

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-5-23 17:45:01
| Show all posts
public void paint (Graphics g) {
int width2 = 120;
g.fillArc (512, 384, 2 * width2, 2 * width2, 0, 360);
}
I am drawing in public void paint (Graphics g) {}, where should those codes be added?
I tried it by the method ofjinm1976, but no picture was generated.
Reply

Use magic Report

1

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-5-31 11:00:02
| Show all posts
Still not allowed
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list