java - Actual look of rectangle is not working -


Below, you can see the small version of my code which is a mistake, I'm working on a program like MS Paint The problem is that when I want to draw a rectangle and I want to see how the painted rectangle will actually appear, then the last last rectangle is prepared by the program. Then, if you comment on some parts of the code and look at them with slight changes (do not use the pre-code), then it works the way I want it to work, however, then the program gets the picture stored on the PC I have to save in and I do not want it to work in this way. I am quite beginning right now in Java and I do not know exactly where the mistake can be, that means drawing the file drawImage and this is what I am doing.

You can hopefully understand what I am talking about, if there is a chance you can not do, it's easy to try to copy the code that try it out. When you try to drag the first object, you will see a mistake.

First Class:

  Import java.awt.BorderLayout; Import javax.swing.JFrame; Public Class MyPaint JFrame {Personal Desk Paint Disco; Public static zero main (string [] args) {@SuppressWarnings ("unused") MyPaint frame = new MyPaint (); } MyPaint () {setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); Setbound (400, 100, 600, 600); setResizable (wrong); Set layout (new border layout ()); Paintdesk = new desk (); Add this.getContentPane () (paintDesk, BorderLayout.CENTER). SetVisible (true); }}   

Second Class:

  Import java.awt. *; Import java.awt.event. *; Import java.awt.image.RenderedImage; Import java.io.IOException; Import java.io.file; Import javax.imageio.ImageIO; Import javax.swing.JComponent; Import javax.swing.SwingUtilities; Public Classroom Desk JComponent {Personal image enhanced image; Private image newImage; Personal Graphics 2D Graphics; Private Point A; Private Point B; Public desk () {a = new point (); B = new point (); setDoubleBuffered (wrong); addMouseListener (New MouseAdapter) {Public Zero MousePressed (MouseEvent E) {if (SwingUtilities.isLeftMouseButton (E)) {try {ImageIO.write (RenderedImage) paintedImage, "jpg", new file ("actual.jpg" );} Grip (IOException E1) {// TODO Auto-built Cache Block e1.printStackTrace ();} * / newImage = paintedImage; ax = e.getX (); ay = e.getY ();}}} ); AddMouseMotionListener (New MouseMotionAdapter () {Public Zero MouseDragged (MouseEver E) {If (Swing Utilities.SecipherMobusButton (E)) {/ try {newImage = (ImageIO.read (New file ("real.je.gz")); } Hold (IOException E1) {// TODO Automatic Built Cache Block e1.printStackTrace ();} * / if (newImage = null!) {Graphics.drawImage (newImage, 0,0, faucet), repaint () ;} bx = e.getX (); by = e.getY (); if (graphics! = null) {drawRect ();} repaint ();}}}); addMouseListener (New MouseAdapter) {Public Zero mouseReleased (mouseEvent e) {if (SwingUtilities.isLeftMouseButton (E)) {bx = e.getX (); = e.getY (), if (graphics = null) {drawRect! ();} repaint ();}}}); } Public Zero Paint Component (Graphics G) {if (Painted Image == Blank) {Painted Image = CreateSize (). Width, getSize (.) Height); Graphics = (Graphics 2D) Painted Image. Gate Graphics (); graphics.setRenderingHint (Rendering Hicks. KEY_ANTIALASING, Rendering Hints. VALUE_ANTIALIAS_ON); graphics.setStroke (new basic stroke (3, basicstroke. CAP_ROUND, basicstroke.JOIN_Rund)); clear(); } G.drawImage (illustrated image, 0, 0, empty); } Private Zero Clear () {graphics.setPaint (Color.white); Graphics. Filter (0, 0, getSize (). Width, getSize (). Height); graphics.setPaint (Color.red); Repaint (); } Private Zero Drawing () {graphics.drawLine (a.x, ai, bx, ai); Graphicsdriveline (ax, ai, ax, bi); GraphicsDrideline (AX, BI, B.X, BI); Graphicsdriveline (b.x, ai, bx, bi); }}    

The basic view of painting the image is always wrong Want to attract until the user releases the mouse button. Until then you need to make a floating rectangle on the component in the paintComponent () method.

DrawOnImage Examples from a work example for the above example uses.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -