How to write text inside a rectangle - java

Hi I am creating a java desktop application where I am drawing rectangle. I want to write some text inside rectangle.
How can I achieve this?
Here is my code:
class DrawPanel extends JPanel {
private void doDrawing(Graphics g) {
int a=90;
int b=60;
int c=10;
int d=15;
ArrayList<Graphics2D> g1 = new ArrayList<Graphics2D>();
for(int i=0;i<=9;i++){
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(new Color(212, 212, 212));
g2d.drawRect(c, d, a, b);
d+=65;
}
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
}

Use your Graphics2D object and call drawString(String str, int x, int y). Something like
g2d.drawRect(c, d, a, b);
g2d.drawString("Hi", (a+c)/2, (b+d)/2);
Note that the Javadoc specifies
Draws the text given by the specified string, using this graphics context's current font and color. The baseline of the leftmost character is at position (x, y) in this graphics context's coordinate system.
so you would need to take into account the space the font takes on screen. Use FontMetrics for that.

Related

Swift: Double Buffering like in Java

In Java, I can write pixel data to an image that is then printed to the screen by overridden methods (paint and paintComponent). I can refresh the screen easily by updating the image and calling refresh(), which calls the paint/paintComponent cycle.
I'm wanting to do this in swift (3) with a UIImage. I can update the image, but I can't figure out how to repeatedly project the image onto the screen, print the screen, and repeat as I need to refresh the screen.
Firstly, what type of swift project (Single view app., Game, etc.) is best for repeatedly refreshing the screen?
And secondly how do I go about creating a (regulated) loop in Swift that refreshes the screen every so often?
In java this would look like (in a class that extends Frame):
static int width = 1440;
static int height = 900;
private BufferedImage canvas;
static Color[][] RGBMap = new Color[width][height];
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setBackground(Color.WHITE);
g2.clearRect(0, 0, width, height);
g2.drawImage(this.canvas, null, null);
paint();
}
public void paint() {
int i = 0;
while (i < width) {
int t = 0;
while (t < height) {
this.canvas.setRGB(i, t, RGBMap[i][t].getRGB());
t++;
}
i++;
}
//Refreshes RGBMap
iterate();
repaint();
}

Java Animation Rotation

I have very little experience with Java, and I am an amateur programmer. So mind my vocabulary.
I want to be able to stick a static rectangle on top of a rotating rectangle.
So far when I try to add another object it spins with the other image. I have tried setting the rotation to zero but that doesn't seem to work. I have also tried to create another class that draws components separately and added them to the frame using frame.add. I have also tried creating another part to the Draw class that has no effect on the GUI. Here is my current Draw class. Any help is appreciated.
class DrawRectangle extends JPanel {
#Override
public void paintComponent(Graphics g) {
int h = this.getHeight();
int w = this.getWidth();
Graphics2D g2 = (Graphics2D) g;
//draw background
g2.setColor(Color.WHITE);
g2.fillRect(0, 0, w, h);
//draw roatiing rectangle
g2.setColor(Color.CYAN);
Rectangle rRec = new Rectangle(w / 4, h / 4, 2 * w / 4, 2 * h / 4);
double wr = rRec.getX() + rRec.getWidth() / 2;
double hr = rRec.getY() + rRec.getHeight() / 2;
g2.rotate(Math.toRadians(count), wr, hr);
g2.fill(rRec);
g2.fillRect(w / 3, h / 3, 2 * w / 3, 2 * h / 3);
}
public void paintComponent2(Graphics g) {
int h = this.getHeight();
int w = this.getWidth();
Graphics2D g2 = (Graphics2D) g;
}
}
So far when I try to add another object it spins with the other image.
Create a separate Graphics object to do the rotation so you don't affect the properties of the Graphics object passed into the painting method:
//Graphics2D g2 = (Graphics2D) g;
Graphics2D g2 = (Graphics2D)g.create();
// painting code
g2.dispose();
Move your g2.fill(rRec); BEFORE the rotate call, and it should work (I just tested it out).
This way, you will draw your static rectangle before the rotation, perform the rotation, THEN draw your second rectangle. Assuming your count variable is incremented somewhere, it should show the second rectangle being rotated.

how to make ellipse in java swing JPanel TitledBorder?

how to draw this in java swing TitledBorder or any other panel ?
i tried the following code and didn't work, kept drawing half ellipse and the other half was cut.
#Override
public void paintBorder(Component c, Graphics g, int x, int y, int width,
int height) {
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.BLUE);
FontMetrics m = c.getFontMetrics(getTitleFont());
Ellipse2D shape = new java.awt.geom.Ellipse2D.Float(2, -20,m.stringWidth(title)+10, m.getHeight()+40);
g2.fill(shape);
g2.draw(shape);
super.paintBorder(c, g, x, y, width, height);
}

Java Swing drawImage sometimes slow in XORMode

I have extended ImageIcon to colorize my icons for different purposes as needed.
I've tried quite a lot now, but when resizing the window it needs > 3 sec. to repaint about 60 32x32 Icons.
First off, this only happens if I color them in XOR Mode. If I leave them as they are it's a matter of milliseconds.
I tried converting the images to a better format with GraphicsConfiguaration.createCompatibleImage but that didn't help.
Now a strange thing: if I draw onto the component.getGraphics instead to the Graphics provided in the paintIcon method, it does so lightning fast even in XOR Mode, but only shows it in a short flicker until it gets overdrawn.
I draw them by adding JButtons into a JPanel with a FlowLayout.
Anything else I can try?
Slow:
public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
g.setXORMode(color);
g.drawImage(getImage(), x, y, null);
}
Fast but flickering:
public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
gr = c.getGraphics();
gr.setXORMode(color);
gr.drawImage(getImage(), x, y, null);
}
Fast but without Xor
public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
g.drawImage(getImage(), x, y, null);
}
Edit:
Found a solution now with a combination of a BufferedImage and AlphaComposite:
public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
int w = getIconWidth();
int h = getIconHeight();
BufferedImage bimage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D bg2 = bimage.createGraphics();
bg2.setColor(co);
bg2.fillRect(0, 0, w, h);
bg2.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_IN));
bg2.drawImage(getImage(),0,0,null);
bg2.dispose();
g.drawImage(bimage,0,0,null);
return;
}
Leaving the question open for some hours to see if someone comes up with a better solution though.

Get position image after Rotation with AffineTransform

I use an AffineTransform class to transform my image and I need to have a position of image after translation and rotation. How to achieve that?
public void rotateImage(ImageObserver o, Graphics g, int x, int y) {
AffineTransform at = new AffineTransform();
at.translate(x, y);
at.rotate(Math.toRadians(deegres),ramie1.getWidth()/2,0);
Graphics2D g2 =(Graphics2D) g;
g2.drawImage(image, at, null);
}

Categories

Resources