Thursday 15 August 2013

java - setColor in JFrame does not work -


My problem is following I have a touch sensor and want to draw on it with display. It gives me three values: X coordination, Y coordination and press force. My application works so far away that it is an oval (or better that appears as many oval lines) and it is different according to the oval force. But I want different colors according to the force.

So here is my code. There is no effect in the line setting the orange color. I have to be commented to work on the job too.

  import java.awt.Color; Import java.awt.Dimension; Import java.awt.Graphics; Import javax.swing.JFrame; The public class enhances GUI 2 Jaffraem (public GUI 2 ()) (this.setPreferredSize (new dimension (1200, 1000)); this.pack (); This.setLocation (300, 50); // X, Y. .set bizarre (true); this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);} @ Override PublicWide Paint (Graphics G) {SuperPaper (g);} Public Zero Drawing Page (int x, int y, int force) {int width = (Force * 2) / 1000; / * if (force <3000) {this.getGraphics (.) SetColor (Color.YELLOW);} and if (force <6000) {this.getGraphics (). SetColor (Color.ORANGE);} and if (force <9000) {this.getGraphics (.) SetColor (Color.RED);} Else {this.getGraphics (). SetColor (Colo R.BLUE);} * / This.getGraphics (). SetColor (Color.ORANGE); // - No effect system.out.println ("COLOR:" + this.getGraphics (.) GetColor () ToString ()); This.getGraphics () .Verify (X, Y, width, width); // <- work    

< Div class = "post-text" itemprop = "text">

There is a link for here. You should start by reading the section on Custom Painting .

To answer your question, I guess the GetGraphics () method gives a new object every time you call the method. So your code should be:

  Graphics G = meet (graphics); G.setColor (...); G.drawOval (...); Again, you should not use this approach to do custom painting, but I want to answer this question because it is usually a better style for coding. It does not apply the same method many times, instead use the method once and allocate it in one variable. This way you know for sure that you are implementing methods on the same object.   

No comments:

Post a Comment