How to remove this blur at the edge of JTextField? - java

I'm Rounding my JTextField but after the field gets rounded the edges are blurry and don't look professional.
This is the code I'm using to round the JTextField. I'm extending the JTextFiled and changing the paintComponent values.
//Rouding the field
protected void paintComponent(Graphics g) {
g.setColor(getBackground());
g.fillRoundRect(0, 0, getWidth()-1, getHeight()-1, 15, 15);
super.paintComponent(g);
}
protected void paintBorder(Graphics g) {
g.setColor(getForeground());
g.drawRoundRect(0, 0, getWidth()-1, getHeight()-1, 15, 15);
}
public boolean contains(int x, int y) {
if (shape == null || !shape.getBounds().equals(getBounds())) {
shape = new RoundRectangle2D.Float(0, 0, getWidth()-1, getHeight()-1, 15, 15);
}
return shape.contains(x, y);
}

In your painting methods you can try using antialiasing for the Graphics.
You would set the painting properties on a separate Graphics object:
Graphics2D g2d = (Graphics2D)g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// add painting logic
g2d.dispose();

Related

How could I combine multiple different colored filled shape into a single shape (while keeping all the colors), in Java Applet

I have this much of the code for the shape and fill. I want the shape and fill to combine into one shape which will keep iits color and stroke, but could be transformed.
public class ShapeGraphics extends Applet {
public void init() {
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setStroke(new BasicStroke(4));
g2d.setColor(Color.BLACK);
Rectangle rect2= new Rectangle(47, 46, 127, 128);
Rectangle rect3=new Rectangle(35,72,34,68);
Color c1=new Color(255,0,0),c2=new Color(50,208,7),c3=new Color(40,166,243)
,c4=new Color(254,174,1);
g2d.draw(rect2);
//RED
g2d.setColor(c1);
g2d.fill(rect3);
//GREEN
g2d.setColor(c2);
//BLUE
g2d.setColor(c3);
g2d.fill(rect2);
/***********************************************************************************/
//START OF LAYER 2
//BLACK LINE!
g2d.setColor(Color.BLACK);
g2d.draw(rect3);
//RED
g2d.setColor(c1);
g2d.fill(rect3);
//Orange
g2d.setColor(c4);
}}
The output is a blue square with a red rectangle. Thanks in Advance

drawString in BufferedImage disappears

I hope you can help me concerning a problem in my drawString.
I do some drawing within a Factory class and return a BufferedImage with a String on it.
public class Factory {
public BufferedImage create() {
TempPanel tempPanel = new TempPanel(new Dimension(100, 100));
return tempPanel.createImage();
}
private class TempPanel extends JPanel {
public TempPanel(Dimension d) {
this.setVisible(true);
this.setSize(d);
}
public BufferedImage createImage() {
BufferedImage bi = new BufferedImage(this.getSize().getWidth(), this.getSize().getHeight(), BufferedImage.TRANSLUCENT);
this.paintComponent(bi.createGraphics());
return bi;
}
#Override
public void paintComponent(Graphics g) {
super.paintComponents(g);
// General setup
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// Transparent Background
g2.setComposite(AlphaComposite.Clear);
g2.fillRect(0, 0, graphicalObject.width, graphicalObject.height);
g2.setComposite(AlphaComposite.Src);
// Different settings for a certain graphical object
g2.setFont(new Font("TimesRoman", Font.PLAIN, 12);
// Actual drawing
g2.drawString("Test", 0, 0);
}
}
}
And then I also have a JPanel in which this image should be drawn:
public class Label extends JPanel {
// ...
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Factory factory = new Factory();
BufferedImage img = factory.create();
g2.drawImage(img, 0, 0, null);
}
}
The problem ist that the text only appears if i minimize and restore the window, but as soon as I start moving the mouse within the window, the text disappears again.
When I call drawRect() instead of drawString(), everything works fine?!
Can someone tell me why?
Thanks!
There are a number of things that "weird" me out...
Firstly, you're creating a new Graphics context, but you're not disposing of it. This could be leaving resources open and may actually prevent the contents from been rendered on some OSs...
So instead of...
BufferedImage bi = new BufferedImage(this.getSize().getWidth(), this.getSize().getHeight(), BufferedImage.TRANSLUCENT);
this.paintComponent(bi.createGraphics());
You should be doing something more like...
BufferedImage bi = new BufferedImage(getWidth(), getHeight(), BufferedImage.TRANSLUCENT);
Graphics2D g2d = bi.createGraphics();
this.paintComponent(g2d);
g2d.dispose();
Secondly, text is not render the same way that most other elements are, that is, it doesn't start at x/y and render down/right, in most cases it starts at x/y and renders, slightly, up/right. The actual coordinate is based on the Fonts FontMetrics getAscent value...
So, instead of...
g2.setFont(new Font("TimesRoman", Font.PLAIN, 12));
// Actual drawing
g2.drawString("Test", 0, 0);
You should be doing something more like...
g2.setFont(new Font("TimesRoman", Font.PLAIN, 12));
FontMetrics fm = g2.getFontMetrics();
// Actual drawing
g2.drawString("Test", 0, fm.getAscent());
See Working with Text APIs or more details...
Thirdly, JComponent and by extension, anything that extends from it, is an ImageObsever, this is important, as not everything that is painted is ready for display immediately, sometimes it needs further processing, rather than having to deal with this annoyance yourself, you can delegate the responsibility, so instead of...
g2.drawImage(img, 0, 0, null);
You should be doing something more like...
g2.drawImage(img, 0, 0, this);
This allows the component to listen to the underlying image and respond to any changes that might happen to the image data and update itself.
Forthly...
private class TempPanel extends JPanel {
public TempPanel(Dimension d) {
this.setVisible(true);
this.setSize(d);
}
public BufferedImage createImage() {
BufferedImage bi = new BufferedImage(this.getSize().getWidth(), this.getSize().getHeight(), BufferedImage.TRANSLUCENT);
this.paintComponent(bi.createGraphics());
return bi;
}
#Override
public void paintComponent(Graphics g) {
super.paintComponents(g);
// General setup
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// Transparent Background
g2.setComposite(AlphaComposite.Clear);
g2.fillRect(0, 0, graphicalObject.width, graphicalObject.height);
g2.setComposite(AlphaComposite.Src);
// Different settings for a certain graphical object
g2.setFont(new Font("TimesRoman", Font.PLAIN, 12);
// Actual drawing
g2.drawString("Test", 0, 0);
}
}
Is freaking me out...Swing components are meant to be attached to a native peer onto which they are rendered, they are also optimised not to paint when they aren't displayable, this could be throwing off your rendering...
Infact, you could achieve the same thing using something like...
public static class Factory {
private static Renderer renderer;
public BufferedImage create() {
if (renderer == null) {
renderer = new Renderer(new Dimension(100, 100));
}
return renderer.createImage();
}
private class Renderer {
private Dimension size;
public Renderer(Dimension size) {
this.size = size;
}
public BufferedImage createImage() {
BufferedImage bi = new BufferedImage(size.width, size.height, BufferedImage.TRANSLUCENT);
Graphics2D g2 = bi.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// Transparent Background
g2.setComposite(AlphaComposite.Clear);
g2.fillRect(0, 0, graphicalObject.width, graphicalObject.height);
g2.setComposite(AlphaComposite.Src);
// Different settings for a certain graphical object
g2.setFont(new Font("TimesRoman", Font.PLAIN, 12));
FontMetrics fm = g2.getFontMetrics();
// Actual drawing
g2.drawString("Test", 0, fm.getAscent());
g2.dispose();
return bi;
}
}
}
Which would divorce the actual painting from anything else and provide you with control over it...

Java Jung draw ovals

i am using jung (java) try to draw circles to the Background. I dont get any output and wondering why it is not working.
Iam using PreRenderPaintable and PostRenderPaintable but still get no results.
vv.addPreRenderPaintable(new VisualizationViewer.Paintable() {
public void paint(Graphics g)
{
System.out.println("PRE RENDER");
System.out.println("vv.getComponentCount()= " + vv.getComponentCount());
Graphics2D g2d = (Graphics2D)g;
AffineTransform oldXform = g2d.getTransform();
AffineTransform lat = vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.LAYOUT).getTransform();
AffineTransform vat = vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.VIEW).getTransform();
AffineTransform at = new AffineTransform();
at.concatenate(g2d.getTransform());
at.concatenate(vat);
at.concatenate(lat);
g2d.setTransform(at);
g.drawOval(100, 100, 150, 150);
g.drawString("adsadd", 200, 200);
g2d.setTransform(oldXform);
}
public boolean useTransform() { return false; }
});
vv.addPostRenderPaintable(new VisualizationViewer.Paintable()
{
public void paint(Graphics g)
{
System.out.println("POST RENDER");
System.out.println("vv.getComponentCount()= " + vv.getComponentCount());
Component comp = vv.getComponent(0);
System.out.println(comp);
System.out.println(comp.getClass());
g.drawString("adasasdsadasdsadsadasdasdsadd", 300, 300);
}
public boolean useTransform()
{
return false;
}
});
The Problem was not setting the color of the output.
For Example:
g.setFont(font);
Color oldColor = g.getColor();
g.setColor(Color.lightGray);

Change the JButton's shape

I'm making the game Master Mind, I've filled my matrix up with JButtons so people can click them to change the color.
Now I want to change the shape of my rectangular buttons to circles, is there a way I can change them all at once since I worked with a loop to create all them.
Here are some methods that have to be overwritten to edit the shape of a component. (Including sample code)
protected void paintComponent(Graphics g)
{
if (getModel().isArmed()) {
g.setColor(Color.lightGray);
} else {
g.setColor(getBackground());
}
g.fillOval(0, 0, getSize().width-1,getSize().height-1);
super.paintComponent(g);
}
protected void paintBorder(Graphics g) {
g.setColor(getForeground());
g.drawOval(0, 0, getSize().width-1, getSize().height-1);
}
Shape shape;
public boolean contains(int x, int y) {
if (shape == null ||
!shape.getBounds().equals(getBounds())) {
shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight());
}
return shape.contains(x, y);
}
You can search Google for a tutorial about this.
This is an easy tutorial: How to change the shape of a JButton

TabbedPaneUI paint selected tab

i would like to paint the selected tab in a different way than other tab, it works the first time but then the repaint doesn't work.
Here is the code i did inside paintTabBackground :
Graphics2D g2d = (Graphics2D) g.create();
if (isSelected) {
BufferedImage background = tabSelected;
Insets insets = getTabInsets(tabPlacement, tabIndex);
Rectangle tabBound = getTabBounds(tabPane, tabIndex);
tileStretchPaint(g2d, tabBound, background, insets);
} else {
g2d.setColor(new Color(0, 0, 0, 0));
super.paintTabBackground(g2d, tabPlacement, tabIndex, x, y, w, h, isSelected);
}
g2d.dispose();
Thanks for help.
Just cast the graphics
Graphics2D g2d = (Graphics2D) g;
and don't dispose it

Categories

Resources