Java - Only Two Rounded Corners on a JFrame - java

I would like to round the top two corners on a JFrame for a project I am currently working on. I am currently rounding all four corners using setShape(new RoundRectangle2D.Double(0, 0, 200, 252, 30, 30)); but I do not want the bottom two rounded I want the to be a normal corner.

you can combine shapes to get this.By combining roundered rectangle with a normal rectangle you can make a rectangle without bottom two rounded corners.
for example
public class example extends JFrame{
public example() {
this.setUndecorated(true);
this.getContentPane().setBackground(Color.red);
Area shape1 = new Area(new RoundRectangle2D.Double(0, 0, 200, 252, 30, 30));
Area shape2 = new Area(new Rectangle(0, 252-30, 200, 100));
shape1.add(shape2);
this.setShape(shape1);
this.setSize(300, 400);
}
public static void main(String[] args) {
new example().setVisible(true);
}
}
alternatively you can give smaller height to the frame than RoundRectangle rectangle .so you can't see bottom of the RoundRectangle .and then you can get desired output

Related

why isn't an oval displayed in my jPanel? [duplicate]

This question already has answers here:
Can not draw oval on a JPanel
(2 answers)
Closed 2 years ago.
I'm trying to add a circle to my JPanel, but it won't draw the cricle.
the code below creates a JFrame, creates a JPanle and calls a function to add a circle to the JPanel(pgame), but it doesn't actually add it.
Help appreciated
fgame = new JFrame("Backgammon");
fgame.setSize(1000, 1000);
pgame = new JPanel();
pgame.setPreferredSize(new Dimension(1000, 687));
pgame.setLayout(new GridLayout(3, 10));
pgame.setBorder(BorderFactory.createEmptyBorder(309,460,150,460));
Circle Circlepanel = new Circle();
pgame.add(Circlepanel);
Circlepanel.setVisible(true);
fgame.add(pgame,BorderLayout.CENTER);
fgame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fgame.setTitle("Backgammon");
fgame.pack();
fgame.setVisible(true);
public class Circle extends JPanel {
public void paint(Graphics g) {
g.drawOval(500, 500, 100, 100);
g.setColor(Color.RED);
g.fillOval(500, 500, 100, 100);
}
}
First of all variable names should NOT start with an upper case character. Most of you names are correct, but not all. Learn Java conventions and be consistent!
Your create a GridLayout
pgame.setLayout(new GridLayout(3, 10));
Which will attempt to allocate space for 3 components vertically in the frame.
Then you create a Border:
pgame.setBorder(BorderFactory.createEmptyBorder(309,460,150,460));
which will give your component a height of 459 and a width of 920.
Finally you try to draw the oval at (500, 500) from the top left of the panel.
g.drawOval(500, 500, 100, 100);
Well, the problem is that you have weird random numbers and the size of your component isn't large enough to paint the oval in the space of the component.
To demonstrate this add and retest:
Circlepanel.setBackground( Color.YELLOW );
You will see a yellow panel. Next change:
//pgame.setLayout(new GridLayout(3, 10));
pgame.setLayout(new GridLayout(1, 0));
and you will see a taller yellow panel in the middle of the frame because you are only allocating space for a single component.
Next change:
//pgame.setBorder(BorderFactory.createEmptyBorder(309,460,150,460));
pgame.setBorder(BorderFactory.createEmptyBorder(50,50,50,50));
and you will see part of the oval because you have reserved less space for the border.
Next change:
//g.fillOval(500, 500, 100, 100);
g.fillOval(0, 0, 100, 100);
and you will see the oval at the top of the panel.
The point is that specifying the:
grid size
border size
oval location
all affect the size of the component and how it is painted.
Other issues:
override the getPreferredSize() method of your Circle class to return the desired size of the panel
custom painting is done by overriding paintComponent(), not paint();
you need to invoke super.paintComponent(..) at the start of the method.
Read the section from the Swing tutorial on Custom Painting for more information and working examples.

JavaFX - Maintain Shape Borders after using Shape.union()

Is there a way to maintain the borders/strokes on a JavaFX Shape after using the union function? For example here is my code:
Shape rect = new Rectangle(150, 150);
rect.setFill(Color.WHITE);
rect.setStroke(Color.BLACK);
rect.setStrokeWidth(4);
Line line = new Line(0, 40, 150, 40);
line.setStrokeWidth(2);
Shape combined = Shape.union(line, rect);
combined.setFill(Color.WHITE);
combined.setStroke(Color.BLACK);
pane.getChildren().add(combined);
Expected Output:
Actual Output:
Is there anyway that I can union the two together so that I can drag and drop them together?
your problem is combined.setFill(Color.WHITE); ,because it clear all previous shape changes .
Try something like this
Line line = new Line(0, 40, 150, 40);
Shape rect = new Rectangle(150, 150);
Shape combined = Shape.subtract(rect,line);
combined.setFill(Color.WHITE);
combined.setStroke(Color.BLACK);
rect.setFill(Color.WHITE);
rect.setStroke(Color.BLACK);
rect.setStrokeWidth(4);
line.setStrokeWidth(2);
line.setStroke(Color.BLACK);
line.setFill(Color.BLACK);
pane.getChildren().add(combined);
out put will like be this
For more info about shape.union,subtract,intersect go here

Have large pixels on java graphics

I am somewhat new to java, and I'm trying to make a game that has large "pixels" that are actually 5*5 pixels large. I have tried to turn the graphics into an image and then resize the image, but it always returns a huge amount of errors. I am implementing ActionListener and using timer to create a game loop, if that means anything important.
I have the variable screen set as a createVolatileImage(160, 160);
public void actionPerformed(ActionEvent e) {
timer.start();
Graphics draw = screen.getGraphics();
draw.setColor(new Color(50, 100, 50));
draw.fillRect(0, 0, 100, 100);
draw.setColor(Color.black);
draw.drawString("Text",10,10);
draw = getGraphics();
draw.drawImage(screen, 0, 0, 800, 800, 0, 0, 160, 160, null);
draw.dispose();
}

drawLine() method not drawing anything

So I have this class inside a class which is an implementation of JPanel.
private static class Line extends JComponent {
private static final long serialVersionUID = 1L;
#Override
public void paintComponent(Graphics g) {
System.out.println("Pozvan paintComponent()");
g.setColor(Color.YELLOW);
g.drawLine(20, 20, 100, 20);
super.paintComponent(g);
}
}
This is a snippet of code which creates a single instance of Line:
Line line = new Line();
line.setOpaque(true);
add(line);
I really don't know what I am doing wrong here. When I draw a rectangle, everything is nicely drawn.
when I set the height to remotely big number it works.
The default size of a Swing component is (0, 0). Since the size is 0, there is nothing to paint.
g.drawLine(20, 20, 100, 20);
Using the above information this means your component needs a size of (120, 40). That is, width = 20 + 100 and height = 20 + 20, in order for the component to be painted.
I added line.setBounds(20, 20, 80, 50);
Only part of your line will be painted, since you set the width to 80, not 120.
Read the section from the Swing tutorial on Custom Painting for more information and examples.

Graphics2D Scaling twice in PaintComponent()

Why does this code output two lines that are the same size?
import java.awt.*;
import javax.swing.*;
public class G2Scale extends JPanel{
public static void main(String args[]) {
G2Scale g = new G2Scale();
g.setPreferredSize(new Dimension(200, 200));
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(g);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLUE);
g2.scale(0.5, 1.0);
g2.drawLine(5, 50, 100, 50);
g2.setColor(Color.GREEN);
g2.scale(1.0, 1.0);
g2.drawLine(5, 100, 100, 100);
}
}
I would expect these lines to be different sizes because they are scaled differently. From what I am seeing I am thinking that the scale is based off of the previous scale. Am I right about this?
If this is true, how would I get the second line to be scaled to what I thought it should be?
Thanks
All methods you call on a Graphics object that don't output something but instead change a property of it (like setColor, setFont, and so on), are stored in the context of the graphics object. Actually, you should think of a Graphics instance as a graphics context that contains and abstract all the information you need to draw into the screen.
So basically, yes, your second scale is based on the first, since the first one changes the graphics context and the second one acts on top of it.
There're two ways to change this behavior:
Reset the state your Graphics instance by aplying the opposite of your first change (in this case, the inverse scalling).
Make a copy of the Graphics object before applying any context change.
I'm more inclined to the second option, but here're some examples of both:
Graphics2D g2 = (Graphics2D) g;
// resetting the context state
g2.scale(0.5, 1.0);
g2.drawLine(5, 50, 100, 50);
g2.scale(2, 1.0);
// using a copy of the context
// note: casting is mandatory since 'create' returns a Graphics object
Graphics2D g2copy = (Graphics2D)g2.create();
g2copy.scale(1.0, 1.0);
g2copy.drawLine(5, 100, 100, 100);
// this one doesn't have any scale applied
g2.drawLine(5, 150, 100, 150);

Categories

Resources