How to make holes in a shape in Piccolo2D? - java

In main Java it supports "winding rule" which may help to make holes in the shapes.
Unfortunately, this concept is ignored in Piccolo2D:
public class Try_Holes_01 {
#SuppressWarnings("serial")
public static void main(String[] args) {
final Path2D path = new Path2D.Double(Path2D.WIND_EVEN_ODD);
//final Path2D path = new Path2D.Double(Path2D.WIND_NON_ZERO);
path.append(new Ellipse2D.Double(100,100,200,200), false);
path.append(new Ellipse2D.Double(120,120,100,100), false);
JPanel panel = new JPanel() {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.fill(path);
}
};
final PPath path_p = new PPath(path);
path_p.setPaint(Color.BLACK);
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.add(panel, BorderLayout.CENTER);
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
new PFrame() {
#Override
public void initialize() {
getCanvas().getLayer().addChild(path_p);
}
};
}
}
So how to make holes inside Piccolo2D paths?

PPath maintains a private GeneralPath member for its operations. It is initialized with WIND_NON_ZERO. Luckily it can be accessed with PPath.getPathReference(). So this should do the trick:
path_p.getPathReference().setWindingRule(Path2D.WIND_EVEN_ODD);
Here is a result:

Related

Graphics2d making a box

I am new to java, I wanted to make a program that draws a box on screen, everything is correct except for the paintComponent(); which is not working
import java.awt.*;
import javax.swing.*;
import java.awt.Graphics2D.*;
public class Frame extends JPanel
{
#Override //this section creates the box
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawRect(150,150,20,20);
}
public static void createWindow() //this section creates the frame
{
JFrame frame = new JFrame("Simple GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //set closing bebavior
frame.setSize(400, 400); //set the size of jframe
frame.setLocationRelativeTo(null); //center the jframe
frame.setVisible(true); //show the frame
}
//main method
public static void main(String[] args)
{
createWindow();//launch your creaWindow method
paintComponent();
}
}
You have to review the basics of Java. Your mistake in this is that you have to create an object of this class Frame and add it to the JFrame which you have created. Below is the correct code #Hh000.
import java.awt.*;
import javax.swing.*;
import java.awt.Graphics2D.*;
public class Frame extends JPanel{
#Override //this section creates the box
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.BLACK);
g.drawRect(150,150,20,20);
}
//main method
public static void main(String[] args){
JFrame frame = new JFrame("Simple GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //set closing bebavior
Frame frameObject = new Frame(); //Making a class object
frame.add(frameObject); //Adding the object into the JFrame
frame.setSize(400, 400); //set the size of jframe
frame.setLocationRelativeTo(null); //center the jframe
frame.setVisible(true);
}
}

I want to add background in JFrame how could I? [duplicate]

This question already has answers here:
Setting background color for a JFrame
(14 answers)
Closed 2 years ago.
Here is my code how can I add a background in it I tried every possible solution but the picture won't show up in the background.
what should I do can anyone help me?
public class Triangle {
JLabel l1;
public static void main(String[] args) {
new Triangle();
}
public Triangle() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.setSize(400,400);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private Polygon poly;
{
poly = new Polygon(
new int[]{110, 150, 50},
new int[]{200, 0, 0},
3);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.blue);
g2.fill(poly);
g2.setColor(Color.red);
g2.fillRect(0,0,25,75);
g2.setColor(Color.green);
g2.fillOval(200,100,100,50);
g2.setColor(Color.green);
g2.fillOval(300,300,250,250);
}
}
}
Here is my code how can I add a background in it I tried every possible solution but the picture won't show up in the background.
what should I do can anyone help me?
Set background color to the JPanel
Try this:
TestPane myPane = new TestPane();
myPane.setBackground(Color.green);
frame.add(myPane);

Wrong Output with the following code

I tried to implement a simple GUI application,having a class extend JPanel and then adding it to a frame and adding a button,but nothing happens when I click on the button.What is wrong?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class dup extends JPanel {
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.green);
g2d.fillRect(0, 0, this.WIDTH, this.HEIGHT);
System.out.println("inside paint component class");
}
}
public class drawing implements ActionListener {
JFrame frame;
dup d1;
public static void main(String args[]) {
drawing d2 = new drawing();
d2.go();
}
public void go() {
frame = new JFrame();
JButton button = new JButton("click me");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
d1 = new dup();
button.addActionListener(this);
frame.getContentPane().add(BorderLayout.WEST, button);
frame.getContentPane().add(BorderLayout.CENTER, d1);
frame.setSize(300, 300);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
frame.repaint();
}
}
What is wrong with this?
Width and height is wrong. It should be
g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
You were using constants from ImageObserver class instead of width and height properties of the component.

Swing Canvas not painting as expected (or at all)

public class TerrisView extends JFrame {
public Canvas canvas;
public TerrisView(String title) {
super(title);
canvas = new Canvas();
canvas.setSize(300, 400);
canvas.setBackground(Color.WHITE);
// setSize(300, 400);
this.add(canvas, BorderLayout.CENTER);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
paint();
}
public void paint() {
// this.createBufferStrategy(2);
// Graphics gp=getBufferStrategy().getDrawGraphics();
// gp.setColor(Color.RED);
// gp.fillRect(100, 100, 50, 50);
// getBufferStrategy().show();
Graphics gp = canvas.getGraphics();
gp.setColor(Color.BLACK);
gp.fillRect(0, 0, 10, 10);
}
}
Why does it fail to draw the Rect on the canvas? What is wrong with the code?
Don't mix Swing with AWT components without good reason.
In this case:
Extend JComponent instead of Canvas
Override paintComponent(Graphics) instead of paint(Graphics)
Before I formatted that code, I failed to notice that paint() was not an override, and this classic..
Graphics gp = canvas.getGraphics();
Don't do that with components. Use the Graphics object that is passed to the methods mentioned in point 2, and paint when told to do so.
This answer is already accepted, but I could not resist reviewing and cleaning up the source to make an SSCCE, as well as add a few more tweaks. See comments in code for further tips.
import java.awt.*;
import javax.swing.*;
public class TerrisView {
public JComponent canvas;
public TerrisView(String title) {
// Don't extend frame, just use one
JFrame f = new JFrame(title);
canvas = new JComponent() {
#Override // check this is a real method
public void paintComponent(Graphics g) {
super.paintComponent(g);
// paint the BG - automatic for a JPanel
g.setColor(getBackground());
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(Color.BLACK);
// make it dynamic, changing with the size
int pad = 10;
g.fillRect(pad, pad, getWidth()-(2*pad), getHeight()-(2*pad));
}
};
// layout managers are more likely to respect the preferred size
canvas.setPreferredSize(new Dimension(300, 400));
canvas.setBackground(Color.ORANGE);
f.add(canvas, BorderLayout.CENTER);
f.pack();
// nice tweak
f.setMinimumSize(f.getSize());
// see http://stackoverflow.com/a/7143398/418556
f.setLocationByPlatform(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public static void main(String[] args) {
// start/alter Swing GUIs on the EDT
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TerrisView("Terris View");
}
});
}
}

Image on Custom JComponent is not Visible?

When I run my code it doesn't show up.
Basically I have a custom Jcomponent which I add to my JFrame or View and then create a View that makes the frame in my main method.
I already added to JFrame here is my code for the JComponent:
public class CardDisplay extends JComponent {
private Card card;
private Image cardImage;
public CardDisplay()
{
cardImage = Toolkit.getDefaultToolkit().createImage(("Phase10//res//Blue2.png"));
}
#Override
public void paint(Graphics g)
{
g.drawImage(cardImage, 125 ,200, this);
}
public class View {
public View(){
}
public void makeFrame()
{
JFrame frame = new JFrame("Phase 10");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JPanel handPanel = new JPanel();
CardDisplay cd = new CardDisplay();
handPanel.setLayout(new FlowLayout());
frame.add(handPanel, BorderLayout.SOUTH);
handPanel.add(cd);
frame.pack();
frame.setSize(600,500);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args){
View view = new View();
Game game = new Game();
view.makeFrame();
//game.run();
}
Here is the working version. The problem was mainly related to the preferred size of the component. Please note the implementation of method getPreferredSize().
If you would like to see what are the component boundaries I'd recommend using MigLayout layout manager in debug mode (the site has all necessary documentation).
public class CardDisplay extends JComponent {
private BufferedImage cardImage;
public CardDisplay() {
try {
cardImage = ImageIO.read(new File("Phase10//res//Blue2.png"));
} catch (final IOException e) {
e.printStackTrace();
}
}
#Override
public void paintComponent(final Graphics g) {
super.paintComponent(g);
g.drawImage(cardImage, 0, 0, null);
}
#Override
public Dimension getPreferredSize() {
if (cardImage == null) {
return new Dimension(100, 100);
} else {
return new Dimension(cardImage.getWidth(null), cardImage.getHeight(null));
}
}
public static class View {
public View() {}
public void makeFrame() {
final JFrame frame = new JFrame("Phase 10");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
final JPanel handPanel = new JPanel();
final CardDisplay cd = new CardDisplay();
handPanel.setLayout(new FlowLayout());
frame.add(handPanel, BorderLayout.SOUTH);
handPanel.add(cd);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
public static void main(final String[] args) {
final View view = new View();
view.makeFrame();
}
}
For JComponents, you use paintComponent instead of paint. Paint is actually used to draw components while paintComponent is used to draw images.

Categories