I'm trying to draw vertical lines to separate days in a week on a JFrame. The code seems fine as no error but when I run it, it output a frame like the picture below. Am I missing anything?
public class WeekToView extends JFrame{
public WeekToView(){
setTitle("Sheffield Dental Care"); //set title
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenDimensions = toolkit.getScreenSize();
setLocation(new Point(screenDimensions.width*1/4, screenDimensions.height*1/4)); //set location based on screen size
JPanel container = new JPanel();
JScrollPane scrPane = new JScrollPane(container);
getContentPane().add(scrPane);
double size[][] = {{150, 150, 150, 150, 150}, // Columns
{100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100}}; // Rows
container.setLayout(new TableLayout(size));
String daysInWeek[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
JLabel daysInWeekLabels[] = new JLabel[daysInWeek.length];
for (int i = 0; i < daysInWeek.length; i++) {
daysInWeekLabels[i] = new JLabel(daysInWeek[i],SwingConstants.CENTER);
}
container.add(daysInWeekLabels[0], "0,0");
container.add(daysInWeekLabels[1], "1,0");
container.add(daysInWeekLabels[2], "2,0");
container.add(daysInWeekLabels[3], "3,0");
container.add(daysInWeekLabels[4], "4,0");
setSize(780,600); //set size based on screen size
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false); //unresizable
setVisible(true);
}
public void paintComponent(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.drawLine(getWidth()/5,0,getWidth()/5,getHeight());
g2.drawLine(getWidth()*2/5,0,getWidth()*2/5,getHeight());
g2.drawLine(getWidth()*3/5,0,getWidth()*3/5,getHeight());
g2.drawLine(getWidth()*4/5,0,getWidth()*5/5,getHeight());
}
}
There is no paintComponent() method in a JFrame. Whenever you attempt to override a method you should always use #Override before the method name. You will get a compile error if you don't override the method correctly.
You could override paint() but in general don't try to do custom painting in the paint() method of a JFrame.
Instead custom painting is done by overriding the paintComponent() method of the panel that you add to the frame.
Better yet you can use a JTable, which already provides you with a row/column based component.
Related
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();
}
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
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.
I am extending the JButton function so as to automate a couple of things, so I have only added this function:
public void addButton(Container container, int left, int top, int width, int height)
{
this.setBounds(left, top, width, height);
this.setSize(width, height);
container.add(this);
this.repaint();
}
and I am running this code in my pannel:
buttonLeft = new extendedButton("Left");
buttonMiddle = new extendedButton("Middle");
buttonRight = new extendedButton("Right");
Insets inset = this.getInsets();
setLayout(null);
Container c = getContentPane();
buttonLeft.addButton(c, inset.left, inset.top, 150, 50);
buttonMiddle.addButton(c, inset.left + buttonLeft.WIDTH, inset.top, 150, 50);
buttonRight.addButton(c, inset.left + buttonLeft.WIDTH + buttonRight.WIDTH, inset.top, 150, 50);
however, though I expect the buttons to appear one next to the other, they seem to appear one on top of the other and one pixel to the right. If however I replace the .WIDTH parameter with 150, it works as expected, any clues as to why .WIDTH denies to give the value I have set?
I am not using a layout, I gave it null as a parameter.
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);