How can I set in the midst?
Without any evidence otherwise, I'd guess you've overriden the paint
method of something like a JFrame
and are painting directly to it.
The problem is, frames have decoration (a border and title bar for example), which takes up space inside the frame...
Technically, this is correct. The rectangle is painted in the center of frame, but the because of the frame's decorations, it looks like it's slightly high...
Instead, you should be painting onto the frame's content area instead.
Here the rectangle now looks correctly centered. In my tests, I set the first frame (bad) to 800x400, I made the second frame's content pane's preferred size 800x400, which made the frame size actually 816x438, as the frame's decorations are now outside of the paint area.
public class CenterOfFrame {
public static void main(String[] args) {
new CenterOfFrame();
}
public CenterOfFrame() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
new BadFrame().setVisible(true);
JFrame goodFrame = new JFrame();
goodFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
goodFrame.setContentPane(new PaintablePane());
goodFrame.pack();
goodFrame.setLocationRelativeTo(null);
goodFrame.setVisible(true);
}
});
}
public class BadFrame extends JFrame {
public BadFrame() {
setSize(800, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
public void paint(Graphics g) {
super.paint(g);
paintTest(g, getWidth() - 1, getHeight() - 1);
}
}
public void paintTest(Graphics g, int width, int height) {
g.setColor(Color.RED);
g.drawLine(0, 0, width, height);
g.drawLine(width, 0, 0, height);
g.drawRect(50, 50, width - 100, height - 100);
}
public class PaintablePane extends JPanel {
@Override
public Dimension getPreferredSize() {
return new Dimension(800, 400);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g); //To change body of generated methods, choose Tools | Templates.
paintTest(g, getWidth() - 1, getHeight() - 1);
}
}
}
This is, one of many reasons, why you should not override the paint
method of top level containers ;)
Using PHP in the midst of markup
you don't need to write <?php . . .?>
you just need to call the variable. so instead of writing "mystring".<?php print $myvar;?>;
you just need "mySting".$myvar;
How to set page content to the middle of screen?
I'm guessing you want to center the box both vertically and horizontally, regardless of browser window size. Since you have a fixed width and height for the box, this should work:
Markup:
<div></div>
CSS:
div {
height: 200px;
width: 400px;
background: black;
position: fixed;
top: 50%;
left: 50%;
margin-top: -100px;
margin-left: -200px;
}
The div should remain in the center of the screen even if you resize the browser. Just replace the margin-top and margin-left with half of the height and width of your table.
Edit: Credit goes to CSS-Tricks, where I got the original idea.
Change value of a param in the midst of a function
You need to invoke the switch each time to get a different color set. One way to do this is to put a function inside your function e.g.:
function Write-Custom
{
param($Say,$ThenSay,$Level,$ExtraLevel)
function GetColors([string]$level)
{
switch([array]$level)
{
none {'Black','White'}
name {'Cyan','DarkBlue'}
good {'White','DarkGreen'}
note {'Gray','White'}
info {'White','DarkGray'}
warn {'Yellow','Black'}
fail {'Black','Red'}
default { throw "Unrecognized level $level" }
}
}
$c = GetColors($Level)
Write-Host " $Say" -ForegroundColor $c[0] -BackgroundColor $c[1]
$c = GetColors($ExtraLevel)
Write-Host " $ThenSay " -ForegroundColor $c[0] -BackgroundColor $c[1]
}
JFrame repainting image blinking
Don't override paint
of top level containers like JFrame
there aren't double buffered, instead, use a JPanel
and override its paintComponent
method. JPanel
is double buffered by default.
Then, add the panel to what ever container you want
Don't forget to call it's super
method (super.paintComponent
) before you do any custom painting.
Take a closer look at Painting in AWT and Swing and Performing Custom Painting for more details about how painting works in Swing
This is just one reason why you shouldn't (extend from JFrame
and) override paint
of top level containers, take a look at
- How can I set in the midst?
- Graphics rendering in title bar
- Java JFrame .setSize(x, y) not working?
- How to get the EXACT middle of a screen, even when re-sized
for some more
Why this is not centered JFrame?
Start by using "actual" values and not "magic" values
getWidth
and getHeight
will tell you what the "actual" size of the component is
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.BLACK);
g2.setStroke(new BasicStroke(4,BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g.drawArc(getWidth()/2 -arcWidth/2, getHeight()/2 - arcHeight/2, arcWidth, arcHeight, 0, 360);
}
As to why you're having issues, see:
- How can I set in the midst?
- How to get the EXACT middle of a screen, even when re-sized
Window Size is smaller than it should be
This happens because the content needs to squeezed into the size of the frame minus its borders.
Checkout this question and this question for a more detailed explanation
The layout manager is also overriding the size property you set on the Screen
component. In either case, you should be overriding the getPreferredSize
method of the Screen
class
Also, you shouldn't be relying on magic numbers or assumptions about the actual size of the component, but should, instead, be using getWidth
and getHeight
instead (I know, it's just for demonstration purposes)
createElement - select boxes interspersed in the midst of text
There is also a function called createTextNode()
(MDN docu) for creating simple text as content. So one solution would be to split your text accordingly, transform it to textnodes and then append it as well:
var doc = document,
fr = doc.createDocumentFragment(),
td = doc.createElement("td"),
sel1 = doc.createElement("select"),
sel2 = doc.createElement("select"),
text1 = doc.createTextNode( 'Tiana is ' ),
text2 = doc.createTextNode( ' keen to go to the ' ),
text3 = doc.createTextNode( 'market' );
sel1.name = "U4";
sel2.name = "J4";
fr.appendChild(td);
td.appendChild( text1 );
td.appendChild(sel1);
td.appendChild( text2 );
td.appendChild(sel2);
td.appendChild( text3 );
Here you can find an example fiddle: link.
Java 'Background' opens to small to see anything
Edit:
A better way to achieve the same result is to call b.setPreferredSize(dimension); and then pack() your frame.
See this answer for more information about JFrame.pack() :
https://stackoverflow.com/a/22982334/5224040
Try this in your main method:
public static void main(String[] args) {
Background b = new Background();
Dimension d = new Dimension(900, 885); //Create a new Dimension
b.setTitle("Game");
b.setPreferredSize(d); //Set the PreferredSize;
b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.pack(); //Pack the frame
b.setVisible(true); //Setting Visible
}
You can also use the following methods to reposition your frame as necessary:
b.setLocationRelativeTo(null); //center the location on the screen.
b.setLocation(0, 0); //Set location explicitly
Call either of these methods below b.pack(); to reposition your frame.
Related Topics
Can You Recommend a Java Library for Reading (And Possibly Writing) CSV Files
How to Set the Classpath in Netbeans
Difference in Days Between Two Dates in Java
Which Overload Will Get Selected for Null in Java
How to Make an Image Move While Listening to a Keypress in Java
How to Configure Port for a Spring Boot Application
What Is @Modelattribute in Spring MVC
Simple Http Server in Java Using Only Java Se API
Why Should Java 8's Optional Not Be Used in Arguments
How to Tell If I'm Running in 64-Bit Jvm or 32-Bit Jvm (From Within a Program)
How to Turn Off the Eclipse Code Formatter for Certain Sections of Java Code
Java Class That Implements Map and Keeps Insertion Order